Skip to content

fix(logger): preserve caller location, exc_info and creation timestam… - #8837

Merged
leandrodamascena merged 5 commits into
aws-powertools:developfrom
dropps07:fix/8824-logger-buffer
Nov 25, 2026
Merged

leandrodamascena merged 5 commits into
aws-powertools:developfrom
dropps07:fix/8824-logger-buffer

Conversation

@dropps07

Copy link
Copy Markdown
Contributor

Issue number: closes #8824

Summary

Changes
Buffered log records now print exactly as they would have been without buffering. Three fixes in the same code path:

  • The location honours stacklevel. The buffer used to resolve the caller from a fixed frame, so any stacklevel above the default was ignored. The frame is now walked honouring stacklevel, and records from third-party loggers carry their own pathname, line, function and logger name instead of the location of the logging internals.
  • A caller-passed exc_info is kept. Only True is resolved to the current exception, mirroring logging.Logger._log. A tuple saved outside its except block, or an exception instance, now flushes with the exception instead of losing it.
  • The timestamp carries the creation time. The flushed record restores msecs and relativeCreated from the stored timestamp, not just created, so a record created at .653 no longer prints the flush milliseconds.
  • The internal buffer record keys were renamed from filename, line and function to pathname, lineno and funcName to match the LogRecord attributes they carry, and it now also carries name and sinfo. No public API changed and no existing test was modified.

User experience
Before: a buffered debug log from a wrapper with stacklevel=3 reported the wrapper instead of the business function, a third-party log showed location handle:1028 with the Powertools logger name, an exception logged outside its except block flushed with no exception, and every flushed record printed the flush time. After: each flushed record is identical to the same log without buffering, same location, name, stack info, exception and timestamp.

Verification
I ran the tests myself and pasted what I saw, I did not describe runs I did not do.

  • New tests, all through the public Logger API: test_buffer_honours_stacklevel_when_flushed, test_buffer_preserves_third_party_logger_name_location_and_stack_info, test_buffer_preserves_provided_exc_info, test_buffer_preserves_original_timestamp. 4 passed with the fix, all 4 failed on develop without it.
  • Full logger suite: 208 passed (tests/unit/logger and tests/functional/logger).
  • Ruff format check and ruff check clean on the touched files, mypy clean on aws_lambda_powertools/logging.
  • Full suite except tests needing optional dependencies missing from this machine: same 97 failures before and after the fix, all missing boto3, aws_xray_sdk and similar optional packages, none in logger.
  • Real run: the issue repro script, buffered and unbuffered side by side, now prints identical location, exception and timestamp for both records, and the third-party repro prints the library function and logger name.
  • This PR was prepared with the assistance of an AI coding assistant, and the verification above was done by me.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dropps07
dropps07 requested a review from a team as a code owner November 15, 2026 16:20
@dropps07
dropps07 requested a review from svozza November 15, 2026 16:20
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 15, 2026
@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 15, 2026
@dropps07

Copy link
Copy Markdown
Contributor Author

heyy @leandrodamascena pls review my PR and let me know if anythings needs to be added, changed or removed.

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dropps07! This is the fix I hoped for: one code path, and a buffered record now prints exactly like the unbuffered one. I wrote a side-by-side probe for stacklevel, a saved exception, a third-party logger with stack_info and the timestamp, and with your branch the two outputs are identical on every field. Keeping only True as "resolve now" is the right reading of exc_info. Goes out in 3.42.0, no need to rebase, you'll be in the release notes.

@dropps07

Copy link
Copy Markdown
Contributor Author

Thanks @dropps07! This is the fix I hoped for: one code path, and a buffered record now prints exactly like the unbuffered one. I wrote a side-by-side probe for stacklevel, a saved exception, a third-party logger with stack_info and the timestamp, and with your branch the two outputs are identical on every field. Keeping only True as "resolve now" is the right reading of exc_info. Goes out in 3.42.0, no need to rebase, you'll be in the release notes.

perfect. sounds good :)

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 64.28571% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.08%. Comparing base (7c4709b) to head (f148912).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
aws_lambda_powertools/logging/buffer/functions.py 61.53% 7 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8837      +/-   ##
===========================================
- Coverage    97.14%   97.08%   -0.07%     
===========================================
  Files          327      327              
  Lines        16839    16865      +26     
  Branches      1540     1545       +5     
===========================================
+ Hits         16359    16374      +15     
- Misses         337      345       +8     
- Partials       143      146       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dropps07

Copy link
Copy Markdown
Contributor Author

heyy @leandrodamascena do i have to resolve codecov issues ??
please let me know.

@dropps07

Copy link
Copy Markdown
Contributor Author

Hi @leandrodamascena, this PR is approved and ready on my side. Could you please merge it when you get a chance so it makes 3.42.0. Thanks.

@leandrodamascena

Copy link
Copy Markdown
Contributor

@dropps07 #8865 landed just ahead of you and it touches the same block in _add_log_record_to_buffer, so this now conflicts. It's a mechanical one: keep the new structure from develop (the record is built first, then emitted straight away when there is no trace id) and add your six keyword arguments to the _create_buffer_record call; in the test file keep both blocks, the new test from develop and your four. Once it's green I'll merge it, the approval stands.

Signed-off-by: Ajey Awasthi <115537579+dropps07@users.noreply.github.com>
The web conflict resolution dropped the buffer config guard, mis-indented
the six new keyword arguments, and nested the new test inside the previous
one so the file did not compile. This restores the guard, fixes the
indentation, and puts the new test back at module level.

Test plan: pytest tests/unit/logger tests/functional/logger, 259 passed.
Ruff format and check clean. Mypy clean on aws_lambda_powertools/logging.
@dropps07

Copy link
Copy Markdown
Contributor Author

hi @leandrodamascena, resolved the conflict the way you described. kept the new structure from develop, the record is built first and emitted straight away when there is no trace id, and added the six keyword arguments to the create buffer record call. kept both blocks in the test file, the new test from develop and my four. the branch is updated and the logger tests pass locally. your earlier approval got dismissed by the new pushes, could you take another look when you get a chance. thanks

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved after the rebase, thanks @dropps07. I rebuilt it on top of today's develop and the side-by-side probe still matches on every field, and the no-trace-id path from #8865 is intact. Merging now.

copy_config_to_registered_loggers without include= wired buffering into every registered logger, boto3 included, and thirteen DynamoDB provider tests running later failed with KeyError: 0 on flush.
@leandrodamascena

Copy link
Copy Markdown
Contributor

@dropps07 one more thing I found while merging: the third-party test calls copy_config_to_registered_loggers(include_buffering=True) without include=, so it wires buffering into every registered logger, boto3 included, and thirteen DynamoDB provider tests that run later fail. I pushed a one-line change adding include={external_logger.name} so I don't bounce it back to you for that. It also uncovered a real bug in develop when buffered third-party records carry a dict argument, that one I'm fixing separately. Merging now.

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved again on f148912, same change plus the one-line test scope.

@sonarqubecloud

Copy link
Copy Markdown

@leandrodamascena
leandrodamascena merged commit 3ff475d into aws-powertools:develop Nov 25, 2026
12 of 14 checks passed
leandrodamascena added a commit to dhaiirryyaa/powertools-lambda-python that referenced this pull request Nov 25, 2026
Resolves the overlap with aws-powertools#8837: the buffer record keeps the stacklevel-aware
frame walk from develop, _log passes stacklevel through to the buffer, and both
sets of buffer tests stay.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logger size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: buffered log records lose their caller location, drop a provided exc_info and print the flush-time milliseconds

2 participants