Skip to content

fix(logger): fix exception on flush without buffer #6794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,10 @@ def flush_buffer(self) -> None:

tracer_id = get_tracer_id()

# no buffer config? return
if not self._buffer_config:
return

# Flushing log without a tracer id? Return
if not tracer_id:
return
Expand All @@ -1206,9 +1210,6 @@ def flush_buffer(self) -> None:
if not buffer:
return

if not self._buffer_config:
return

# Check ALC level against buffer level
lambda_log_level = self._get_aws_lambda_log_level()
if lambda_log_level:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,30 @@ def handler(event, context):
assert len(log) == 0


def test_flush_buffer_log_output_without_buffer_config(stdout, service_name, lambda_context, monkeypatch):
# Set initial trace ID for first Lambda invocation
monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "1-67c39786-5908a82a246fb67f3089263f")

# GIVEN A logger without buffer configuration
logger = Logger(level="DEBUG", service=service_name, stream=stdout)

@logger.inject_lambda_context(flush_buffer_on_uncaught_error=True)
def handler(event, context):
# Log messages are not buffered and should be output immediately
logger.debug("debug message - 1")
logger.debug("debug message - 2")
raise ValueError("Test error")

# WHEN Invoking the handler and expecting a ValueError
# AND flush_buffer_on_uncaught_error is True but there is no logger buffer configuration
with pytest.raises(ValueError):
handler({}, lambda_context)

# THEN Verify that log messages are flushed without any exception
log = capture_multiple_logging_statements_output(stdout)
assert len(log) == 2, "Expected two log messages"


def test_buffer_configuration_and_buffer_propagation_across_logger_instances(stdout, service_name, monkeypatch):
monkeypatch.setenv(constants.XRAY_TRACE_ID_ENV, "1-67c39786-5908a82a246fb67f3089263f")

Expand Down
Loading