Skip to content

Commit c3c652e

Browse files
fix(logger): fix exception on flush without buffer (#6794)
* fix: exception on flush without buffer * fix(logger): add test for flush without buffer Co-authored-by: leandrodamascena <leandro.damascena@gmail.com> --------- Co-authored-by: Leandro Damascena <lcdama@amazon.pt> Co-authored-by: leandrodamascena <leandro.damascena@gmail.com>
1 parent d0837c5 commit c3c652e

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
@@ -1197,6 +1197,10 @@ def flush_buffer(self) -> None:
11971197

11981198
tracer_id = get_tracer_id()
11991199

1200+
# no buffer config? return
1201+
if not self._buffer_config:
1202+
return
1203+
12001204
# Flushing log without a tracer id? Return
12011205
if not tracer_id:
12021206
return
@@ -1206,9 +1210,6 @@ def flush_buffer(self) -> None:
12061210
if not buffer:
12071211
return
12081212

1209-
if not self._buffer_config:
1210-
return
1211-
12121213
# Check ALC level against buffer level
12131214
lambda_log_level = self._get_aws_lambda_log_level()
12141215
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)