Skip to content

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

Open
dropps07 wants to merge 1 commit into
aws-powertools:developfrom
dropps07:fix/8824-logger-buffer
Open

dropps07 wants to merge 1 commit into
aws-powertools:developfrom
dropps07:fix/8824-logger-buffer

Conversation

@dropps07

Copy link
Copy Markdown

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
@sonarqubecloud

Copy link
Copy Markdown

@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 15, 2026

This branch has not been deployed

No deployments
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