diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 34d5bb6e18..29a0c20fb4 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -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 = 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. @@ -976,6 +982,7 @@ class Logger extends Utility implements LoggerInterface { if (this.isValidLogLevel(constructorLogLevel)) { this.logLevel = this.logLevelThresholds[constructorLogLevel]; + this.#initialLogLevel = this.logLevel; return; } @@ -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; } @@ -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; diff --git a/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts b/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts index 89c7e4833f..2ccedbe94f 100644 --- a/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts +++ b/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts @@ -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'; @@ -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; @@ -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 { diff --git a/packages/logger/tests/unit/Logger.test.ts b/packages/logger/tests/unit/Logger.test.ts index f5ead4bb6c..72dc761056 100644 --- a/packages/logger/tests/unit/Logger.test.ts +++ b/packages/logger/tests/unit/Logger.test.ts @@ -3443,7 +3443,6 @@ describe('Class: Logger', () => { logger.refreshSampleRateCalculation(); if (logger.getLevelName() === 'DEBUG') { logLevelChangedToDebug++; - logger.setLogLevel('ERROR'); } }