diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 358f735f6..139839abc 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -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; /** * Persistent log attributes that will be logged in all log items. */ @@ -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.` ); } @@ -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); @@ -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' + ); + } } /** @@ -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); } diff --git a/packages/logger/tests/unit/logBuffer.test.ts b/packages/logger/tests/unit/logBuffer.test.ts index 4f75b939c..ebe7d838c 100644 --- a/packages/logger/tests/unit/logBuffer.test.ts +++ b/packages/logger/tests/unit/logBuffer.test.ts @@ -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 } });