Skip to content

Commit 9634d04

Browse files
committed
fix: preserve percent-style formatting args in flush_buffer
- Fix _create_and_flush_log_record to use stored args from buffer - Add test for percent-style formatting in buffered logs - Resolves issue where format strings were not interpolated on flush Fixes #8005
1 parent 36a91b7 commit 9634d04

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

aws_lambda_powertools/logging/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ def _create_and_flush_log_record(self, log_line: dict) -> None:
11161116
fn=log_line["filename"],
11171117
lno=log_line["line"],
11181118
msg=log_line["msg"],
1119-
args=(),
1119+
args=log_line.get("args", ()),
11201120
exc_info=log_line["exc_info"],
11211121
func=log_line["function"],
11221122
extra=log_line["extra"],

tests/functional/logger/required_dependencies/test_powertools_logger_buffer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,3 +568,19 @@ def test_warning_when_alc_less_verbose_than_buffer(stdout, monkeypatch):
568568
# THEN another warning should be emitted about ALC and buffer level mismatch
569569
with pytest.warns(PowertoolsUserWarning, match="Advanced Logging Controls*"):
570570
logger.flush_buffer()
571+
572+
573+
def test_flush_buffer_preserves_percent_style_formatting(stdout, service_name, monkeypatch):
574+
monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "1-67c39786-5908a82a246fb67f3089263f")
575+
576+
# GIVEN A logger configured with a buffer
577+
logger_buffer_config = LoggerBufferConfig(max_bytes=10240)
578+
logger = Logger(level="DEBUG", service=service_name, stream=stdout, buffer_config=logger_buffer_config)
579+
580+
# WHEN Logging a message with percent-style formatting
581+
logger.debug("user=%s count=%d", "alice", 42)
582+
logger.flush_buffer()
583+
584+
# THEN The flushed log should contain the interpolated message
585+
log = capture_multiple_logging_statements_output(stdout)
586+
assert log[0]["message"] == "user=alice count=42"

0 commit comments

Comments
 (0)