Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logger): disable logs while testing with jest --silent in dev env #1165

23 changes: 21 additions & 2 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ import type {
*/
class Logger extends Utility implements ClassThatLogs {

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

private customConfigService?: ConfigServiceInterface;

Expand Down Expand Up @@ -575,6 +576,21 @@ class Logger extends Utility implements ClassThatLogs {
return <number> this.powertoolLogData.sampleRateValue;
}

/**
* It initializes console property as an instance of the internal version of Console() class (PR #748)
* or as the global node console if the `POWERTOOLS_DEV' env variable is set and has truthy value.
*
* @private
* @returns {void}
*/
private initConsole(): void {
if (!this.getEnvVarsService().isDevMode()) {
this.console = new Console({ stdout: process.stdout, stderr: process.stderr });
} else {
this.console = console;
}
}

/**
* It returns true if the provided log level is valid.
*
Expand Down Expand Up @@ -699,7 +715,7 @@ class Logger extends Utility implements ClassThatLogs {
* @returns {void}
*/
private setLogIndentation(): void {
if (this.getEnvVarsService().getDevMode()) {
if (this.getEnvVarsService().isDevMode()) {
this.logIndentation = LogJsonIndent.PRETTY;
}
}
Expand Down Expand Up @@ -766,6 +782,9 @@ class Logger extends Utility implements ClassThatLogs {
} = options;

this.setEnvVarsService();
// order is important, it uses EngVarsService()
shdq marked this conversation as resolved.
Show resolved Hide resolved
this.initConsole();
shdq marked this conversation as resolved.
Show resolved Hide resolved

this.setCustomConfigService(customConfigService);
this.setLogLevel(logLevel);
this.setSampleRateValue(sampleRateValue);
Expand Down
14 changes: 7 additions & 7 deletions packages/logger/src/config/ConfigServiceInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ interface ConfigServiceInterface {
*/
getCurrentEnvironment(): string

/**
* It returns the value of the POWERTOOLS_DEV environment variable.
*
* @returns {boolean}
*/
getDevMode(): boolean

/**
* It returns the value of the POWERTOOLS_LOGGER_LOG_EVENT environment variable.
*
Expand Down Expand Up @@ -57,6 +50,13 @@ interface ConfigServiceInterface {
*/
getServiceName(): string

/**
* It returns the value of the POWERTOOLS_DEV environment variable.
*
* @returns {boolean}
*/
isDevMode(): boolean

/**
* It returns true if the string value represents a boolean true value.
*
Expand Down
22 changes: 11 additions & 11 deletions packages/logger/src/config/EnvironmentVariablesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ class EnvironmentVariablesService extends CommonEnvironmentVariablesService impl
return this.get(this.currentEnvironmentVariable);
}

/**
* It returns the value of the POWERTOOLS_DEV environment variable.
*
* @returns {boolean}
*/
public getDevMode(): boolean {
const value = this.get(this.devModeVariable);

return this.isValueTrue(value);
}

/**
* It returns the value of the AWS_LAMBDA_FUNCTION_MEMORY_SIZE environment variable.
*
Expand Down Expand Up @@ -117,6 +106,17 @@ class EnvironmentVariablesService extends CommonEnvironmentVariablesService impl
return (value && value.length > 0) ? Number(value) : undefined;
}

/**
* It returns true if the POWERTOOLS_DEV environment variable is set to truthy value.
*
* @returns {boolean}
*/
public isDevMode(): boolean {
const value = this.get(this.devModeVariable);

return this.isValueTrue(value);
}

/**
* It returns true if the string value represents a boolean true value.
*
Expand Down
14 changes: 9 additions & 5 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,7 @@ describe('Class: Logger', () => {
test('when called, it returns a DISTINCT clone of the logger instance', () => {

// Prepare
const INDENTATION = LogJsonIndent.COMPACT;
const parentLogger = new Logger();

// Act
Expand All @@ -1261,7 +1262,10 @@ describe('Class: Logger', () => {

// Assess
expect(parentLogger === childLogger).toBe(false);
expect(parentLogger).toEqual(childLogger);
expect(childLogger).toEqual({
...parentLogger,
console: expect.any(Console),
});
expect(parentLogger === childLoggerWithPermanentAttributes).toBe(false);
expect(parentLogger === childLoggerWithSampleRateEnabled).toBe(false);
expect(parentLogger === childLoggerWithErrorLogLevel).toBe(false);
Expand All @@ -1272,7 +1276,7 @@ describe('Class: Logger', () => {
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logEvent: false,
logIndentation: 0,
logIndentation: INDENTATION,
logFormatter: expect.any(PowertoolLogFormatter),
logLevel: 'DEBUG',
logLevelThresholds: {
Expand All @@ -1297,7 +1301,7 @@ describe('Class: Logger', () => {
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logEvent: false,
logIndentation: 0,
logIndentation: INDENTATION,
logFormatter: expect.any(PowertoolLogFormatter),
logLevel: 'DEBUG',
logLevelThresholds: {
Expand All @@ -1324,7 +1328,7 @@ describe('Class: Logger', () => {
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logEvent: false,
logIndentation: 0,
logIndentation: INDENTATION,
logFormatter: expect.any(PowertoolLogFormatter),
logLevel: 'DEBUG',
logLevelThresholds: {
Expand All @@ -1349,7 +1353,7 @@ describe('Class: Logger', () => {
customConfigService: undefined,
envVarsService: expect.any(EnvironmentVariablesService),
logEvent: false,
logIndentation: 0,
logIndentation: INDENTATION,
logFormatter: expect.any(PowertoolLogFormatter),
logLevel: 'ERROR',
logLevelThresholds: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('Class: EnvironmentVariablesService', () => {

});

describe('Method: getDevMode', () => {
describe('Method: isDevMode', () => {

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

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

// Act
const value = service.getDevMode();
const value = service.isDevMode();

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

// Act
const value = service.getDevMode();
const value = service.isDevMode();

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

// Act
const value = service.getDevMode();
const value = service.isDevMode();

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

// Act
const value = service.getDevMode();
const value = service.isDevMode();

// Assess
expect(value).toEqual(false);
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/tests/unit/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('Helper: createLogger function', () => {
getServiceName(): string {
return 'my-backend-service';
},
getDevMode(): boolean {
isDevMode(): boolean {
return false;
},
isValueTrue(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/tests/unit/middleware/middy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe('Middy middleware', () => {
getServiceName(): string {
return 'my-backend-service';
},
getDevMode(): boolean {
isDevMode(): boolean {
return false;
},
isValueTrue(): boolean {
Expand Down