Skip to content
Open
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 35 additions & 26 deletions aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,36 +1162,45 @@ def _add_log_record_to_buffer(
between different tracer contexts.
"""

# Determine tracer ID, defaulting to first invoke marker
# Get the current X-Ray trace ID
tracer_id = get_tracer_id()

if tracer_id and self._buffer_config:
if not self._buffer_cache.get(tracer_id):
# Detect new Lambda invocation context and reset buffer to maintain log isolation
# Ensures logs from previous invocations do not leak into current execution
# Prevent memory excessive usage
self._buffer_cache.clear()

log_record: dict[str, Any] = _create_buffer_record(
level=level,
msg=msg,
args=args,
exc_info=exc_info,
stack_info=stack_info,
extra=extra,
if not self._buffer_config:
return

log_record: dict[str, Any] = _create_buffer_record(
level=level,
msg=msg,
args=args,
exc_info=exc_info,
stack_info=stack_info,
extra=extra,
)

if not tracer_id:
# Without a tracer ID, buffer isolation is not possible.
# Emit immediately to avoid losing the log record.
self._create_and_flush_log_record(log_record)
return

if not self._buffer_cache.get(tracer_id):
# Detect new Lambda invocation context and reset buffer to maintain log isolation
# Ensures logs from previous invocations do not leak into current execution
# Prevent memory excessive usage
self._buffer_cache.clear()

try:
self._buffer_cache.add(tracer_id, log_record)
except BufferError:
warnings.warn(
message="Cannot add item to the buffer. "
f"Item size exceeds total cache size {self._buffer_config.max_bytes} bytes",
category=PowertoolsUserWarning,
stacklevel=2,
)
try:
self._buffer_cache.add(tracer_id, log_record)
except BufferError:
warnings.warn(
message="Cannot add item to the buffer. "
f"Item size exceeds total cache size {self._buffer_config.max_bytes} bytes",
category=PowertoolsUserWarning,
stacklevel=2,
)

# flush this log to avoid data loss
self._create_and_flush_log_record(log_record)
# flush this log to avoid data loss
self._create_and_flush_log_record(log_record)

def flush_buffer(self) -> None:
"""
Expand Down
2 changes: 2 additions & 0 deletions docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ The following environment variables are available to configure Logger at a globa

Log buffering enables you to buffer logs for a specific request or invocation. Enable log buffering by passing `logger_buffer` when initializing a Logger instance. You can buffer logs at the `WARNING`, `INFO` or `DEBUG` level, and flush them automatically on error or manually as needed.

When an X-Ray trace ID is unavailable, log records at the configured buffering level are emitted immediately instead of being buffered. This ensures logs generated during initialization or outside a Lambda invocation are not lost.

!!! tip "This is useful when you want to reduce the number of log messages emitted while still having detailed logs when needed, such as when troubleshooting issues."

=== "getting_started_with_buffering_logs.py"
Expand Down
4 changes: 4 additions & 0 deletions examples/logger/src/getting_started_with_buffering_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
logger_buffer_config = LoggerBufferConfig(max_bytes=20480, flush_on_error_log=True)
logger = Logger(level="INFO", buffer_config=logger_buffer_config)

# This initialization-phase log is emitted immediately because no
# X-Ray trace ID is available yet.
logger.debug("Logger initialized")


def lambda_handler(event: dict, context: LambdaContext):
logger.debug("a debug log") # this is buffered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,42 @@ def test_copy_config_with_buffering_uses_buffering_handler(stdout, logger, log_l
assert "Test message from external logger" in output


def test_copy_config_with_buffering_logs_immediately_without_tracer_id(stdout, logger, log_level):
# GIVEN an external logger and Powertools logger with buffer_config
ext_logger = logger()
buffer_config = LoggerBufferConfig(max_bytes=10240, buffer_at_verbosity="WARNING")
powertools_logger = Logger(
service=service_name(),
level=log_level.INFO.value,
stream=stdout,
buffer_config=buffer_config,
)

# WHEN configuration is copied with include_buffering=True
utils.copy_config_to_registered_loggers(
source_logger=powertools_logger,
include_buffering=True,
include={ext_logger.name},
)

# AND the external logger emits a message without a tracer ID
with patch("aws_lambda_powertools.logging.logger.get_tracer_id", return_value=None):
ext_logger.info("Test message from external logger")

# THEN the message is emitted immediately
log = capture_logging_output(stdout)
assert log["message"] == "Test message from external logger"

# AND a subsequent flush with a tracer ID does not emit it again
with patch(
"aws_lambda_powertools.logging.logger.get_tracer_id",
return_value="1-67c39786-5908a82a246fb67f3089263f",
):
powertools_logger.flush_buffer()

assert len(stdout.getvalue().strip().splitlines()) == 1


def test_copy_config_buffering_without_config_uses_normal_handlers(stdout, logger, log_level):
# GIVEN an external logger and Powertools logger WITHOUT buffer_config
ext_logger = logger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,35 @@ def test_flush_buffer_preserves_percent_style_formatting(stdout, service_name, m
# THEN The flushed log should contain the interpolated message
log = capture_multiple_logging_statements_output(stdout)
assert log[0]["message"] == "user=alice count=42"


def test_logger_buffer_logs_immediately_without_trace_id(stdout, service_name, monkeypatch):
monkeypatch.delenv(constants.XRAY_TRACE_ID_ENV, raising=False)

logger_buffer_config = LoggerBufferConfig(
max_bytes=10240,
buffer_at_verbosity="DEBUG",
)
logger = Logger(
level="DEBUG",
service=service_name,
stream=stdout,
buffer_config=logger_buffer_config,
)

logger.debug("This message should be logged immediately")

log = capture_multiple_logging_statements_output(stdout)

assert len(log) == 1
assert log[0]["message"] == "This message should be logged immediately"

# Simulate the trace ID becoming available when the Lambda invocation starts
monkeypatch.setenv(
constants.XRAY_TRACE_ID_ENV,
"1-67c39786-5908a82a246fb67f3089263f",
)

logger.flush_buffer()

assert len(capture_multiple_logging_statements_output(stdout)) == 1