Skip to content

Commit 6f0c307

Browse files
shdqdreamorosi
andauthored
feat(logger): disable logs while testing with jest --silent in dev env (#1165)
* feat(logger): disable logs while testing with jest --silent in dev env * refactor(logger): rename method * docs(logger): add testing section with jest --silent option, which is also suppresses logs (#1165) * Update docs/core/logger.md Co-authored-by: Andrea Amorosi <[email protected]> * test(logger): add test for setConsole() method * test(logger): add comment for assertion * docs(logger): add an example of using POWERTOOLS_DEV with jest --silent * Update packages/logger/src/Logger.ts Co-authored-by: Andrea Amorosi <[email protected]> Co-authored-by: Andrea Amorosi <[email protected]>
1 parent c37932d commit 6f0c307

File tree

8 files changed

+87
-34
lines changed

8 files changed

+87
-34
lines changed

Diff for: docs/core/logger.md

+8
Original file line numberDiff line numberDiff line change
@@ -971,3 +971,11 @@ This is a Jest sample that provides the minimum information necessary for Logger
971971

972972
!!! tip
973973
If you don't want to declare your own dummy Lambda Context, you can use [`ContextExamples.helloworldContext`](https://github.com/awslabs/aws-lambda-powertools-typescript/blob/main/packages/commons/src/samples/resources/contexts/hello-world.ts#L3-L16) from [`@aws-lambda-powertools/commons`](https://www.npmjs.com/package/@aws-lambda-powertools/commons).
974+
975+
### Suppress logs with Jest
976+
977+
When unit testing your code with [Jest](https://jestjs.io) you can use the `POWERTOOLS_DEV` environment variable in conjunction with the Jest `--silent` CLI option to suppress logs from Logger.
978+
979+
```bash title="Disabling logs while testing with Jest"
980+
export POWERTOOLS_DEV=true && npx jest --silent
981+
```

Diff for: packages/logger/src/Logger.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ import type {
113113
*/
114114
class Logger extends Utility implements ClassThatLogs {
115115

116-
private console = new Console({ stdout: process.stdout, stderr: process.stderr });
116+
// console is initialized in the constructor in setOptions()
117+
private console!: Console;
117118

118119
private customConfigService?: ConfigServiceInterface;
119120

@@ -572,7 +573,7 @@ class Logger extends Utility implements ClassThatLogs {
572573

573574
return <number> this.powertoolLogData.sampleRateValue;
574575
}
575-
576+
576577
/**
577578
* It returns true if the provided log level is valid.
578579
*
@@ -640,6 +641,21 @@ class Logger extends Utility implements ClassThatLogs {
640641
};
641642
}
642643

644+
/**
645+
* It initializes console property as an instance of the internal version of Console() class (PR #748)
646+
* or as the global node console if the `POWERTOOLS_DEV' env variable is set and has truthy value.
647+
*
648+
* @private
649+
* @returns {void}
650+
*/
651+
private setConsole(): void {
652+
if (!this.getEnvVarsService().isDevMode()) {
653+
this.console = new Console({ stdout: process.stdout, stderr: process.stderr });
654+
} else {
655+
this.console = console;
656+
}
657+
}
658+
643659
/**
644660
* Sets the Logger's customer config service instance, which will be used
645661
* to fetch environment variables.
@@ -697,7 +713,7 @@ class Logger extends Utility implements ClassThatLogs {
697713
* @returns {void}
698714
*/
699715
private setLogIndentation(): void {
700-
if (this.getEnvVarsService().getDevMode()) {
716+
if (this.getEnvVarsService().isDevMode()) {
701717
this.logIndentation = LogJsonIndent.PRETTY;
702718
}
703719
}
@@ -764,6 +780,8 @@ class Logger extends Utility implements ClassThatLogs {
764780
} = options;
765781

766782
this.setEnvVarsService();
783+
// order is important, it uses EnvVarsService()
784+
this.setConsole();
767785
this.setCustomConfigService(customConfigService);
768786
this.setLogLevel(logLevel);
769787
this.setSampleRateValue(sampleRateValue);

Diff for: packages/logger/src/config/ConfigServiceInterface.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ interface ConfigServiceInterface {
2222
*/
2323
getCurrentEnvironment(): string
2424

25-
/**
26-
* It returns the value of the POWERTOOLS_DEV environment variable.
27-
*
28-
* @returns {boolean}
29-
*/
30-
getDevMode(): boolean
31-
3225
/**
3326
* It returns the value of the POWERTOOLS_LOGGER_LOG_EVENT environment variable.
3427
*
@@ -57,6 +50,13 @@ interface ConfigServiceInterface {
5750
*/
5851
getServiceName(): string
5952

53+
/**
54+
* It returns the value of the POWERTOOLS_DEV environment variable.
55+
*
56+
* @returns {boolean}
57+
*/
58+
isDevMode(): boolean
59+
6060
/**
6161
* It returns true if the string value represents a boolean true value.
6262
*

Diff for: packages/logger/src/config/EnvironmentVariablesService.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,6 @@ class EnvironmentVariablesService extends CommonEnvironmentVariablesService impl
4646
return this.get(this.currentEnvironmentVariable);
4747
}
4848

49-
/**
50-
* It returns the value of the POWERTOOLS_DEV environment variable.
51-
*
52-
* @returns {boolean}
53-
*/
54-
public getDevMode(): boolean {
55-
const value = this.get(this.devModeVariable);
56-
57-
return this.isValueTrue(value);
58-
}
59-
6049
/**
6150
* It returns the value of the AWS_LAMBDA_FUNCTION_MEMORY_SIZE environment variable.
6251
*
@@ -117,6 +106,17 @@ class EnvironmentVariablesService extends CommonEnvironmentVariablesService impl
117106
return (value && value.length > 0) ? Number(value) : undefined;
118107
}
119108

109+
/**
110+
* It returns true if the POWERTOOLS_DEV environment variable is set to truthy value.
111+
*
112+
* @returns {boolean}
113+
*/
114+
public isDevMode(): boolean {
115+
const value = this.get(this.devModeVariable);
116+
117+
return this.isValueTrue(value);
118+
}
119+
120120
/**
121121
* It returns true if the string value represents a boolean true value.
122122
*

Diff for: packages/logger/tests/unit/Logger.test.ts

+33-6
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,7 @@ describe('Class: Logger', () => {
12391239
test('when called, it returns a DISTINCT clone of the logger instance', () => {
12401240

12411241
// Prepare
1242+
const INDENTATION = LogJsonIndent.COMPACT;
12421243
const parentLogger = new Logger();
12431244

12441245
// Act
@@ -1257,7 +1258,10 @@ describe('Class: Logger', () => {
12571258

12581259
// Assess
12591260
expect(parentLogger === childLogger).toBe(false);
1260-
expect(parentLogger).toEqual(childLogger);
1261+
expect(childLogger).toEqual({
1262+
...parentLogger,
1263+
console: expect.any(Console),
1264+
});
12611265
expect(parentLogger === childLoggerWithPermanentAttributes).toBe(false);
12621266
expect(parentLogger === childLoggerWithSampleRateEnabled).toBe(false);
12631267
expect(parentLogger === childLoggerWithErrorLogLevel).toBe(false);
@@ -1269,7 +1273,7 @@ describe('Class: Logger', () => {
12691273
defaultServiceName: 'service_undefined',
12701274
envVarsService: expect.any(EnvironmentVariablesService),
12711275
logEvent: false,
1272-
logIndentation: 0,
1276+
logIndentation: INDENTATION,
12731277
logFormatter: expect.any(PowertoolLogFormatter),
12741278
logLevel: 'DEBUG',
12751279
logLevelThresholds: {
@@ -1295,7 +1299,7 @@ describe('Class: Logger', () => {
12951299
defaultServiceName: 'service_undefined',
12961300
envVarsService: expect.any(EnvironmentVariablesService),
12971301
logEvent: false,
1298-
logIndentation: 0,
1302+
logIndentation: INDENTATION,
12991303
logFormatter: expect.any(PowertoolLogFormatter),
13001304
logLevel: 'DEBUG',
13011305
logLevelThresholds: {
@@ -1323,7 +1327,7 @@ describe('Class: Logger', () => {
13231327
defaultServiceName: 'service_undefined',
13241328
envVarsService: expect.any(EnvironmentVariablesService),
13251329
logEvent: false,
1326-
logIndentation: 0,
1330+
logIndentation: INDENTATION,
13271331
logFormatter: expect.any(PowertoolLogFormatter),
13281332
logLevel: 'DEBUG',
13291333
logLevelThresholds: {
@@ -1349,7 +1353,7 @@ describe('Class: Logger', () => {
13491353
defaultServiceName: 'service_undefined',
13501354
envVarsService: expect.any(EnvironmentVariablesService),
13511355
logEvent: false,
1352-
logIndentation: 0,
1356+
logIndentation: INDENTATION,
13531357
logFormatter: expect.any(PowertoolLogFormatter),
13541358
logLevel: 'ERROR',
13551359
logLevelThresholds: {
@@ -1461,4 +1465,27 @@ describe('Class: Logger', () => {
14611465
});
14621466
});
14631467

1464-
});
1468+
describe('Method: setConsole()', () => {
1469+
1470+
test('When the `POWERTOOLS_DEV` env var is SET console object is set to the global node console otherwise to the instance of the internal version of console', () => {
1471+
1472+
// Prepare
1473+
const logger = new Logger();
1474+
process.env.POWERTOOLS_DEV = 'true';
1475+
const devLogger = new Logger();
1476+
1477+
// Assess
1478+
expect(devLogger).toEqual({
1479+
...devLogger,
1480+
console: console,
1481+
});
1482+
// since instances of a class are not equal objects,
1483+
// we assert the opposite – console is not the global node object
1484+
expect(logger).not.toEqual({
1485+
...logger,
1486+
console: console,
1487+
});
1488+
});
1489+
});
1490+
1491+
});

Diff for: packages/logger/tests/unit/config/EnvironmentVariablesService.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('Class: EnvironmentVariablesService', () => {
181181

182182
});
183183

184-
describe('Method: getDevMode', () => {
184+
describe('Method: isDevMode', () => {
185185

186186
test('It returns true if the environment variable POWERTOOLS_DEV is "true"', () => {
187187

@@ -190,7 +190,7 @@ describe('Class: EnvironmentVariablesService', () => {
190190
const service = new EnvironmentVariablesService();
191191

192192
// Act
193-
const value = service.getDevMode();
193+
const value = service.isDevMode();
194194

195195
// Assess
196196
expect(value).toEqual(true);
@@ -203,7 +203,7 @@ describe('Class: EnvironmentVariablesService', () => {
203203
const service = new EnvironmentVariablesService();
204204

205205
// Act
206-
const value = service.getDevMode();
206+
const value = service.isDevMode();
207207

208208
// Assess
209209
expect(value).toEqual(false);
@@ -216,7 +216,7 @@ describe('Class: EnvironmentVariablesService', () => {
216216
const service = new EnvironmentVariablesService();
217217

218218
// Act
219-
const value = service.getDevMode();
219+
const value = service.isDevMode();
220220

221221
// Assess
222222
expect(value).toEqual(false);
@@ -229,7 +229,7 @@ describe('Class: EnvironmentVariablesService', () => {
229229
const service = new EnvironmentVariablesService();
230230

231231
// Act
232-
const value = service.getDevMode();
232+
const value = service.isDevMode();
233233

234234
// Assess
235235
expect(value).toEqual(false);

Diff for: packages/logger/tests/unit/helpers.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ describe('Helper: createLogger function', () => {
314314
getServiceName(): string {
315315
return 'my-backend-service';
316316
},
317-
getDevMode(): boolean {
317+
isDevMode(): boolean {
318318
return false;
319319
},
320320
isValueTrue(): boolean {

Diff for: packages/logger/tests/unit/middleware/middy.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ describe('Middy middleware', () => {
336336
getServiceName(): string {
337337
return 'my-backend-service';
338338
},
339-
getDevMode(): boolean {
339+
isDevMode(): boolean {
340340
return false;
341341
},
342342
isValueTrue(): boolean {

0 commit comments

Comments
 (0)