Skip to content

Commit

Permalink
fix(logger): reset log level after sampling refresh (#2673)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi authored Jun 19, 2024
1 parent d544e8f commit 618faec
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
13 changes: 13 additions & 0 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ class Logger extends Utility implements LoggerInterface {
* type. We then use this map at log preparation time to pick the last one.
*/
#keys: Map<string, 'temp' | 'persistent'> = new Map();
/**
* This is the initial log leval as set during the initialization of the logger.
*
* We keep this value to be able to reset the log level to the initial value when the sample rate is refreshed.
*/
#initialLogLevel = 12;

/**
* Log level used by the current instance of Logger.
Expand Down Expand Up @@ -976,6 +982,7 @@ class Logger extends Utility implements LoggerInterface {

if (this.isValidLogLevel(constructorLogLevel)) {
this.logLevel = this.logLevelThresholds[constructorLogLevel];
this.#initialLogLevel = this.logLevel;

return;
}
Expand All @@ -984,12 +991,14 @@ class Logger extends Utility implements LoggerInterface {
?.toUpperCase();
if (this.isValidLogLevel(customConfigValue)) {
this.logLevel = this.logLevelThresholds[customConfigValue];
this.#initialLogLevel = this.logLevel;

return;
}
const envVarsValue = this.getEnvVarsService()?.getLogLevel()?.toUpperCase();
if (this.isValidLogLevel(envVarsValue)) {
this.logLevel = this.logLevelThresholds[envVarsValue];
this.#initialLogLevel = this.logLevel;

return;
}
Expand Down Expand Up @@ -1019,6 +1028,10 @@ class Logger extends Utility implements LoggerInterface {
if (value && randomInt(0, 100) / 100 <= value) {
this.setLogLevel('DEBUG');
this.debug('Setting log level to DEBUG due to sampling rate');
} else {
this.setLogLevel(
this.getLogLevelNameFromNumber(this.#initialLogLevel)
);
}

return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from '../../src/index.js';
import type { TestEvent, TestOutput } from '../helpers/types';
import type { TestEvent, TestOutput } from '../helpers/types.js';
import type { Context } from 'aws-lambda';
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';

Expand All @@ -9,6 +9,7 @@ const LOG_MSG = process.env.LOG_MSG || 'Hello World';
const logger = new Logger({
sampleRateValue: SAMPLE_RATE,
});
let firstInvocation = true;

class Lambda implements LambdaInterface {
private readonly logMsg: string;
Expand All @@ -19,9 +20,12 @@ class Lambda implements LambdaInterface {

// Decorate your handler class method
@logger.injectLambdaContext()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public async handler(event: TestEvent, context: Context): TestOutput {
public async handler(_event: TestEvent, context: Context): TestOutput {
if (firstInvocation) {
firstInvocation = false;
} else {
logger.refreshSampleRateCalculation();
}
this.printLogInAllLevels();

return {
Expand Down
1 change: 0 additions & 1 deletion packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3443,7 +3443,6 @@ describe('Class: Logger', () => {
logger.refreshSampleRateCalculation();
if (logger.getLevelName() === 'DEBUG') {
logLevelChangedToDebug++;
logger.setLogLevel('ERROR');
}
}

Expand Down

0 comments on commit 618faec

Please sign in to comment.