Skip to content

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

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

Closed
kallependers opened this issue Apr 8, 2025 · 6 comments · Fixed by #3816
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped logger This item relates to the Logger Utility

Comments

@kallependers
Copy link

kallependers commented Apr 8, 2025

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"
}
@kallependers kallependers added bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels Apr 8, 2025
Copy link

boring-cyborg bot commented Apr 8, 2025

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

@kallependers kallependers changed the title Bug: Log pollution with the same warnings on every warm execution Bug: Log pollution with the same warning on every warm execution when using injectLambdaContext middleware Apr 8, 2025
@dreamorosi dreamorosi moved this from Triage to Working on it in Powertools for AWS Lambda (TypeScript) Apr 8, 2025
@dreamorosi dreamorosi added logger This item relates to the Logger Utility confirmed The scope is clear, ready for implementation and removed triage This item has not been triaged by a maintainer, please wait labels Apr 8, 2025
@dreamorosi dreamorosi self-assigned this Apr 8, 2025
@dreamorosi
Copy link
Contributor

Hi @kallependers, thank you for opening this issue and for taking the time to share the detailed report.

I was able to reproduce the issue and as you pointed out, when the we detect that Advanced Logging Controls is enabled we fall back to that, but never store that value. Because of this, even if ApplicationLogLevel.WARN and POWERTOOLS_LOG_LEVEL=WARN are aligned, the Logger thinks there's a mismatch as it uses the default log level (INFO).

I'm working to see if I can fix the bug and will propose a PR, hopefully soon.

We do have a section dedicated to ALC and how it interacts with our logger here, if you think there's any specific improvement we can do there please let us know.

@dreamorosi
Copy link
Contributor

Besides fixing the bug described above, I think we should also address the fact that these warnings should not be spammed.

I'm going to see if I can implement a mechanism to emit this type of warnings only once per execution environment.

@kallependers
Copy link
Author

Sounds great! Thanks a lot for the quick response.

Copy link
Contributor

github-actions bot commented Apr 8, 2025

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Apr 8, 2025
Copy link
Contributor

This is now released under v2.19.0 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Apr 24, 2025
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped logger This item relates to the Logger Utility
Projects
Development

Successfully merging a pull request may close this issue.

2 participants