From 8d6ea040c1e4ce177068e1a9fea5085e748f1110 Mon Sep 17 00:00:00 2001 From: Rainer Killinger Date: Tue, 8 Nov 2022 15:04:53 +0100 Subject: [PATCH] fix: removing transformations in production --- src/logger.ts | 9 +++++---- test/logger.spec.ts | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/logger.ts b/src/logger.ts index 3445ba0e..92fd00dd 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -106,9 +106,7 @@ export class Logger { } let transformedOutput = output; - for (const transformation of Logger.transformations.filter(transform => - !isProductiveEnvironment() ? true : transform.useInProduction === isProductiveEnvironment(), - )) { + for (const transformation of Logger.transformations) { transformedOutput = transformation.transform(logLevel, transformedOutput); } @@ -292,7 +290,10 @@ export class Logger { * @param transformations List of transformations */ public static setTransformations(transformations: Transformation[]) { - Logger.transformations = transformations; + const transforms = transformations.filter(transform => + isProductiveEnvironment() ? transform.useInProduction === true : true, + ); + Logger.transformations = transforms; } /** diff --git a/test/logger.spec.ts b/test/logger.spec.ts index dda76ee5..5294d3ec 100644 --- a/test/logger.spec.ts +++ b/test/logger.spec.ts @@ -239,20 +239,27 @@ export class LoggerSpec { @test 'is compatible with log aggregation in productive environment'() { - const nodeEnv = process.env.NODE_ENV; - process.env.NODE_ENV = 'production'; - process.env.ALLOW_NO_TRANSPORT = 'true'; - Logger.setTransformations([new AddLogLevel(), new Colorize()]); - const spy = LoggerSpec.sandbox.on(console, 'log', () => { + let spy = LoggerSpec.sandbox.on(console, 'log', () => { // noop }); Logger.log('Foo\nbar'); expect(spy).to.have.been.called.once; - expect(spy.__spy.calls[0][0]).to.equal('[LOG] Foo bar'); + expect(spy.__spy.calls[0][0]).to.equal('\u001B[37m[LOG] Foo\u001B[39m\n\u001B[37mbar\u001B[39m'); + + const nodeEnv = process.env.NODE_ENV; + process.env.NODE_ENV = 'production'; + process.env.ALLOW_NO_TRANSPORT = 'true'; + + Logger.setTransformations([new AddLogLevel(), new Colorize()]); + + Logger.log('Foo\nbar'); + + expect(spy).to.have.been.called.twice; + expect(spy.__spy.calls[1][0]).to.equal('[LOG] Foo bar'); process.env.NODE_ENV = nodeEnv; delete process.env.ALLOW_NO_TRANSPORT;