powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Bug: setLevel() is undone on every warm invocation when sampling_rate is set

Open leandrodamascena opened this issue 2 weeks ago • 5 comments

[!WARNING] For contributors, human or agent. Comment here to claim this issue and wait for the assignment before opening a PR; if two PRs show up, the assignee's stays. AI-assisted work is welcome, say so in the PR and tell us how you verified it: run the tests yourself and paste what you saw, do not describe runs you did not do. Read AGENTS.md first, it has the commands and the rules the CI enforces. This goes into 3.42.0.

Expected Behaviour

A level set with logger.setLevel() after construction stays until changed again, whether or not debug sampling is configured.

Current Behaviour

refresh_sample_rate_calculation, called on every invocation by inject_lambda_context, resets the level to initial_log_level, the constructor value, before re-rolling the sample. logger.setLevel("WARNING") holds for the cold start only; from the second invocation INFO logs are back.

Code snippet

from aws_lambda_powertools import Logger

logger = Logger(service="x", level="INFO", sampling_rate=0.0)
logger.setLevel("WARNING")
logger.refresh_sample_rate_calculation()  # what every warm invocation does
logger.info("should be hidden")           # printed

Possible Solution

In aws_lambda_powertools/logging/logger.py, track the level set through setLevel as the new baseline the refresh falls back to, instead of the constructor value. Test: set a level after construction, call refresh_sample_rate_calculation, the level is unchanged; a sampled invocation still lowers to DEBUG and the next refresh restores the baseline.

Item 7 of the Logger audit in #8730, re-verified on develop today. Functional tests go through the public Logger API only.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

No response

leandrodamascena avatar Nov 15 '26 15:11 leandrodamascena

[!note] For those interested in contributing, please leave a comment below so that we can assign the issue to you and make sure we don't duplicate efforts. If you have any further questions, please don't hesitate to ask below or on our Discord server.

I'd like to take this one. Plan: in aws_lambda_powertools/logging/logger.py, have setLevel update the baseline level that refresh_sample_rate_calculation falls back to, instead of always resetting to the constructor value. Functional tests through the public Logger API: setLevel("WARNING") survives a refresh with sampling_rate=0.0, sampling still raises to DEBUG when it hits, and a level set at construction keeps working as before. I'll follow AGENTS.md, target develop, and paste the real test output in the PR.

saquibjawedbit avatar Nov 15 '26 17:11 saquibjawedbit

@saquibjawedbit the plan is right, but you already have #8828 and #8798, and two per person is the rule for this batch so the work spreads out. I'm leaving this one open for someone else. If it is still free when your two are merged, it's yours.

leandrodamascena avatar Nov 15 '26 17:11 leandrodamascena

I'd like to take this one.

Plan: in aws_lambda_powertools/logging/logger.py, update setLevel so that it also updates self.initial_log_level to the newly determined level. That way refresh_sample_rate_calculation falls back to the last explicit setLevel value instead of the constructor value.

Functional tests through the public Logger API only:

  • setLevel("WARNING") survives a refresh_sample_rate_calculation with sampling_rate=0.0
  • sampling still raises to DEBUG when it hits
  • a level set at construction keeps working as before

I'll follow AGENTS.md, target develop, and paste the real test output in the PR.

HarshRajSinghania avatar Nov 15 '26 17:11 HarshRajSinghania

It's yours @HarshRajSinghania, your second with #8831. One thing to watch: a sampled invocation lowers the level to DEBUG through the same path, so make sure the baseline you store is the one set explicitly, not the sampled one, or the next refresh will keep DEBUG forever. A test for that case next to the one in the issue.

leandrodamascena avatar Nov 15 '26 17:11 leandrodamascena

Hi @leandrodamascena @HarshRajSinghania, checking in on this issue — if Harsh is busy or unavailable, I'd be glad to take this over. I have the fix and tests ready to ensure setLevel() updates the baseline log level so warm invocations in refresh_sample_rate_calculation() don't reset it.

dhaiirryyaa avatar Nov 18 '26 16:11 dhaiirryyaa