Skip to content

Commit 30709fc

Browse files
tonnicoleandrodamascena
authored andcommitted
fix(logger): fix exception on flush without buffer (aws-powertools#6794)
* fix: exception on flush without buffer * fix(logger): add test for flush without buffer Co-authored-by: leandrodamascena <[email protected]> --------- Co-authored-by: Leandro Damascena <[email protected]> Co-authored-by: leandrodamascena <[email protected]>
1 parent a571fa2 commit 30709fc

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

aws_lambda_powertools/logging/logger.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,10 @@ def flush_buffer(self) -> None:
12001200

12011201
tracer_id = get_tracer_id()
12021202

1203+
# no buffer config? return
1204+
if not self._buffer_config:
1205+
return
1206+
12031207
# Flushing log without a tracer id? Return
12041208
if not tracer_id:
12051209
return
@@ -1209,9 +1213,6 @@ def flush_buffer(self) -> None:
12091213
if not buffer:
12101214
return
12111215

1212-
if not self._buffer_config:
1213-
return
1214-
12151216
# Check ALC level against buffer level
12161217
lambda_log_level = self._get_aws_lambda_log_level()
12171218
if lambda_log_level:

tests/functional/logger/required_dependencies/test_powertools_logger_buffer.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,30 @@ def handler(event, context):
391391
assert len(log) == 0
392392

393393

394+
def test_flush_buffer_log_output_without_buffer_config(stdout, service_name, lambda_context, monkeypatch):
395+
# Set initial trace ID for first Lambda invocation
396+
monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "1-67c39786-5908a82a246fb67f3089263f")
397+
398+
# GIVEN A logger without buffer configuration
399+
logger = Logger(level="DEBUG", service=service_name, stream=stdout)
400+
401+
@logger.inject_lambda_context(flush_buffer_on_uncaught_error=True)
402+
def handler(event, context):
403+
# Log messages are not buffered and should be output immediately
404+
logger.debug("debug message - 1")
405+
logger.debug("debug message - 2")
406+
raise ValueError("Test error")
407+
408+
# WHEN Invoking the handler and expecting a ValueError
409+
# AND flush_buffer_on_uncaught_error is True but there is no logger buffer configuration
410+
with pytest.raises(ValueError):
411+
handler({}, lambda_context)
412+
413+
# THEN Verify that log messages are flushed without any exception
414+
log = capture_multiple_logging_statements_output(stdout)
415+
assert len(log) == 2, "Expected two log messages"
416+
417+
394418
def test_buffer_configuration_and_buffer_propagation_across_logger_instances(stdout, service_name, monkeypatch):
395419
monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "1-67c39786-5908a82a246fb67f3089263f")
396420

0 commit comments

Comments
 (0)