Skip to content

Commit a77bd1c

Browse files
dreamorosiam29d
authored andcommitted
fix(logger): reset log level after sampling refresh (#2673)
1 parent 2b08253 commit a77bd1c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ class Logger extends Utility implements LoggerInterface {
194194
* type. We then use this map at log preparation time to pick the last one.
195195
*/
196196
#keys: Map<string, 'temp' | 'persistent'> = new Map();
197+
/**
198+
* This is the initial log leval as set during the initialization of the logger.
199+
*
200+
* We keep this value to be able to reset the log level to the initial value when the sample rate is refreshed.
201+
*/
202+
#initialLogLevel = 12;
197203

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

977983
if (this.isValidLogLevel(constructorLogLevel)) {
978984
this.logLevel = this.logLevelThresholds[constructorLogLevel];
985+
this.#initialLogLevel = this.logLevel;
979986

980987
return;
981988
}
@@ -984,12 +991,14 @@ class Logger extends Utility implements LoggerInterface {
984991
?.toUpperCase();
985992
if (this.isValidLogLevel(customConfigValue)) {
986993
this.logLevel = this.logLevelThresholds[customConfigValue];
994+
this.#initialLogLevel = this.logLevel;
987995

988996
return;
989997
}
990998
const envVarsValue = this.getEnvVarsService()?.getLogLevel()?.toUpperCase();
991999
if (this.isValidLogLevel(envVarsValue)) {
9921000
this.logLevel = this.logLevelThresholds[envVarsValue];
1001+
this.#initialLogLevel = this.logLevel;
9931002

9941003
return;
9951004
}
@@ -1019,6 +1028,10 @@ class Logger extends Utility implements LoggerInterface {
10191028
if (value && randomInt(0, 100) / 100 <= value) {
10201029
this.setLogLevel('DEBUG');
10211030
this.debug('Setting log level to DEBUG due to sampling rate');
1031+
} else {
1032+
this.setLogLevel(
1033+
this.getLogLevelNameFromNumber(this.#initialLogLevel)
1034+
);
10221035
}
10231036

10241037
return;

Diff for: packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Logger } from '../../src/index.js';
2-
import type { TestEvent, TestOutput } from '../helpers/types';
2+
import type { TestEvent, TestOutput } from '../helpers/types.js';
33
import type { Context } from 'aws-lambda';
44
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
55

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

1314
class Lambda implements LambdaInterface {
1415
private readonly logMsg: string;
@@ -19,9 +20,12 @@ class Lambda implements LambdaInterface {
1920

2021
// Decorate your handler class method
2122
@logger.injectLambdaContext()
22-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
23-
// @ts-ignore
24-
public async handler(event: TestEvent, context: Context): TestOutput {
23+
public async handler(_event: TestEvent, context: Context): TestOutput {
24+
if (firstInvocation) {
25+
firstInvocation = false;
26+
} else {
27+
logger.refreshSampleRateCalculation();
28+
}
2529
this.printLogInAllLevels();
2630

2731
return {

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

-1
Original file line numberDiff line numberDiff line change
@@ -3443,7 +3443,6 @@ describe('Class: Logger', () => {
34433443
logger.refreshSampleRateCalculation();
34443444
if (logger.getLevelName() === 'DEBUG') {
34453445
logLevelChangedToDebug++;
3446-
logger.setLogLevel('ERROR');
34473446
}
34483447
}
34493448

0 commit comments

Comments
 (0)