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

fix(logger): reset log level after sampling refresh #2673

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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