Skip to content

fix(logger): warn customers when the ALC log level is less verbose than log buffer #3834

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

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
39 changes: 35 additions & 4 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ class Logger extends Utility implements LoggerInterface {
* Log level used internally by the current instance of Logger.
*/
private logLevel: number = LogLevelThreshold.INFO;

/**
* Advanced Logging Control Log Level
* If not a valid value this will be left undefined, even if the
* environment variable AWS_LAMBDA_LOG_LEVEL is set
*/
#alcLogLevel?: Uppercase<LogLevel>;
/**
* Persistent log attributes that will be logged in all log items.
*/
Expand Down Expand Up @@ -819,16 +826,15 @@ class Logger extends Utility implements LoggerInterface {
}

private awsLogLevelShortCircuit(selectedLogLevel?: string): boolean {
const awsLogLevel = this.getEnvVarsService().getAwsLogLevel();
if (this.isValidLogLevel(awsLogLevel)) {
this.logLevel = LogLevelThreshold[awsLogLevel];
if (this.#alcLogLevel !== undefined) {
this.logLevel = LogLevelThreshold[this.#alcLogLevel];

if (
this.isValidLogLevel(selectedLogLevel) &&
this.logLevel > LogLevelThreshold[selectedLogLevel]
) {
this.#warnOnce(
`Current log level (${selectedLogLevel}) does not match AWS Lambda Advanced Logging Controls minimum log level (${awsLogLevel}). This can lead to data loss, consider adjusting them.`
`Current log level (${selectedLogLevel}) does not match AWS Lambda Advanced Logging Controls minimum log level (${this.#alcLogLevel}). This can lead to data loss, consider adjusting them.`
);
}

Expand Down Expand Up @@ -1306,6 +1312,11 @@ class Logger extends Utility implements LoggerInterface {
);

// configurations that affect Logger behavior
const AlcLogLevel = this.getEnvVarsService().getAwsLogLevel();
if (this.isValidLogLevel(AlcLogLevel)) {
this.#alcLogLevel = AlcLogLevel;
}

this.setLogEvent();
this.setInitialLogLevel(logLevel);
this.setInitialSampleRate(sampleRateValue);
Expand Down Expand Up @@ -1378,6 +1389,16 @@ class Logger extends Utility implements LoggerInterface {
this.#bufferConfig.bufferAtVerbosity =
LogLevelThreshold[bufferAtLogLevel];
}

if (
this.#alcLogLevel !== undefined &&
LogLevelThreshold[this.#alcLogLevel] >
this.#bufferConfig.bufferAtVerbosity
) {
this.#warnOnce(
'Advanced Loggging Controls (ALC) Log Level is less verbose than Log Buffering Log Level. Buffered logs will be filtered by ALC'
);
}
}

/**
Expand Down Expand Up @@ -1444,6 +1465,16 @@ class Logger extends Utility implements LoggerInterface {
);
}

if (
this.#alcLogLevel !== undefined &&
LogLevelThreshold[this.#alcLogLevel] >
this.#bufferConfig.bufferAtVerbosity
) {
this.#warnOnce(
'Advanced Loggging Controls (ALC) Log Level is less verbose than Log Buffering Log Level. Some logs might be missing.'
);
}

this.#buffer?.delete(traceId);
}

Expand Down
45 changes: 45 additions & 0 deletions packages/logger/tests/unit/logBuffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,51 @@ describe('Buffer logs', () => {
);
});

it('outputs a warning when the Advanced Logging Configuration Log Level is less verbose than the Log Buffering Log Level', () => {
// Assemble
process.env.AWS_LAMBDA_LOG_LEVEL = 'INFO';
const logger = new Logger({
logLevel: LogLevel.DEBUG,
logBufferOptions: { enabled: true, bufferAtVerbosity: 'DEBUG' },
});

// Act
logger.debug('This is a debug');

// Assess
expect(console.warn).toHaveLogged(
expect.objectContaining({
message: expect.stringContaining(
'Advanced Loggging Controls (ALC) Log Level is less verbose than Log Buffering Log Level. Buffered logs will be filtered by ALC'
),
level: LogLevel.WARN,
})
);
});

it('When the buffer is flushed it outputs a warning if the Advanced Logging Configuration Log Level is less verbose than the Log Buffering Log Level', () => {
// Assemble
process.env.AWS_LAMBDA_LOG_LEVEL = 'INFO';
const logger = new Logger({
logLevel: LogLevel.DEBUG,
logBufferOptions: { enabled: true, bufferAtVerbosity: 'DEBUG' },
});

// Act
logger.debug('This is a debug');
logger.flushBuffer();

// Assess
expect(console.warn).toHaveLogged(
expect.objectContaining({
message: expect.stringContaining(
'Advanced Loggging Controls (ALC) Log Level is less verbose than Log Buffering Log Level. Some logs might be missing.'
),
level: LogLevel.WARN,
})
);
});

it('outputs a warning when there is an error buffering the log', () => {
// Prepare
const logger = new Logger({ logBufferOptions: { maxBytes: 100 } });
Expand Down