fix: removing transformations in production

This commit is contained in:
Rainer Killinger
2022-11-08 15:04:53 +01:00
parent e0f4ce134d
commit 8d6ea040c1
2 changed files with 18 additions and 10 deletions

View File

@@ -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;
}
/**

View File

@@ -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;