Skip to content

Bug: Log pollution with the same warning on every warm execution when using injectLambdaContext middleware #3815

Closed
@kallependers

Description

@kallependers

Expected Behavior

No warning logged that the current log level does not match AWS Lambda Advanced Logging Controls minimum log level when injectLambdaContext Middy middleware is used.

Current Behavior

On every warm execution with injectLambdaContext Middy middleware a warning is logged that the current log level does not match AWS Lambda Advanced Logging Controls minimum log level.

Problem:

  • injectLambdaContext calls refreshSampleRateCalculation on every execution;
  • refreshSampleRateCalculation callsthis.setLogLevel with the #initialLogLevel on every warm call when debug sampling is not enabled;
  • #initialLogLevel contains the default value INFO, because AWS Lambda Advanced Logging Controls minimum log level is set (to a higher value than INFO).

Since the following commit 8c8d6b2, because refreshSampleRateCalculation was added to middy.ts.

Code snippet

import { Logger } from '@aws-lambda-powertools/logger';
import middy from '@middy/core';

const logger = new Logger();

export const handler = middy()
  .use(injectLambdaContext(logger))
  .handler(async () => ({}));

Steps to Reproduce

  1. Set application log level to WARN (AWS Lambda Advanced Logging Controls | systemLogLevelV2);
  2. Set environment variable POWERTOOLS_LOG_LEVEL=WARN;
  3. Don't configure any debug sampling;
  4. Use injectLambdaContext Middy middleware;
  5. From a warm execution (coldStart=false) onwards every time a WARN is logged.

Possible Solution

setInitialLogLevel in Logger.ts assigns the #initialLogLevel regardless if awsLogLevelShortCircuit is true and another check is added (or replaced for the awsLogLevelShortCircuit condition in setInitialLogLevel) if AWS Lambda Advanced Logging Controls is configured and sets the initial log level with this value.

Another solution would be documenting that Powertool's log level should be combined with AWS Lambda's application log level. The downside of this would be possible log pollution coming from libraries that don't use the Powertool logger, but something else (console eg).

Powertools for AWS Lambda (TypeScript) version

V2.16.0 - V2.18.0 (latest currently)

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

{
    "level": "WARN",
    "message": "Current log level (INFO) does not match AWS Lambda Advanced Logging Controls minimum log level (WARN). This can lead to data loss, consider adjusting them.",
    "timestamp": "X",
    "service": "X",
    "cold_start": false,
    "function_arn": "X",
    "function_memory_size": "128",
    "function_name": "X",
    "function_request_id": "X",
    "sampling_rate": 0,
    "xray_trace_id": "X"
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcompletedThis item is complete and has been merged/shippedloggerThis item relates to the Logger Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions