Summary
We are excited to announce a new feature in Logger: Logger buffering. This new feature allows you to buffer logs for a specific invocation, and flush them automatically on error or manually as needed.
A special thanks to Ollie Saul and James Saull from Dotelastic for their instrumental input on this new feature!
⭐ Also, huge thanks to our new contributors: @tiagohconte and @speshak.
New Log Buffering feature
You can now enable log buffering by passing buffer_config
when initializing a new Logger instance. This feature allows you to:
- Buffer logs at the
WARNING
,INFO
, andDEBUG
levels - Automatically flush logs on error or manually as needed
- Reduce CloudWatch costs by decreasing the number of emitted log messages
Configuration options
Option | Description | Default |
---|---|---|
max_bytes
| Maximum size of the buffer in bytes | 20480
|
buffer_at_verbosity
| Minimum log level to buffer (more verbose levels are also buffered) | DEBUG
|
flush_on_error_log
| Whether to flush buffer when an error is logged | True |
When log buffering is enabled, you can now pass a new opt-in flush_buffer_on_uncaught_error
flag to the inject_lambda_context()
decorator. When enabled, 1/ we'll intercept any raised exception, 2/ flush the buffer, and 3/ re-raise your original exception. This enables you to have detailed logs from your application when you need them the most.
For detailed explanations with diagrams, please refer to our comprehensive documentation.
Buffering FAQs
Q: Does the buffer persist across Lambda invocations?
A: No, each Lambda invocation has its own buffer. The buffer is initialized when the Lambda function is invoked and is cleared after the function execution completes or when flushed manually.
Q: Are my logs buffered during cold starts?
A: No. We never buffer logs during cold starts to ensure all logs from this phase are immediately available for debugging.
Q: How can I prevent log buffering from consuming excessive memory?
A: You can limit the size of the buffer by setting the max_bytes
option in the LoggerBufferConfig
constructor parameter. This will ensure that the buffer does not grow indefinitely and consume excessive memory.
Q: What happens if the log buffer reaches its maximum size?
A: Older logs are removed from the buffer to make room for new logs. This means that if the buffer is full, you may lose some logs if they are not flushed before the buffer reaches its maximum size. When this happens, we emit a warning when flushing the buffer to indicate that some logs have been dropped.
Q: What timestamp is used when I flush the logs?
A: The timestamp preserves the original time when the log record was created. If you create a log record at 11:00:10 and flush it at 11:00:25, the log line will retain its original timestamp of 11:00:10.
Q: What happens if I try to add a log line that is bigger than max buffer size?
A: The log will be emitted directly to standard output and not buffered. When this happens, we emit a warning to indicate that the log line was too big to be buffered.
Q: What happens if Lambda times out without flushing the buffer?
A: Logs that are still in the buffer will be lost. If you are using the log buffer to log asynchronously, you should ensure that the buffer is flushed before the Lambda function times out. You can do this by calling the logger.flush_buffer()
method at the end of your Lambda function.
Q: Do child loggers inherit the buffer?
A: No, child loggers do not inherit the buffer from their parent logger but only the buffer configuration. This means that if you create a child logger, it will have its own buffer and will not share the buffer with the parent logger.
Changes
- refactor(tracer): fix capture_lambda_handler return type annotation (#6197) by @tiagohconte
🌟New features and non-breaking changes
- feat(logger): add logger buffer feature (#6060) by @leandrodamascena
- feat(logger): add new logic to sample debug logs (#6142) by @leandrodamascena
📜 Documentation updates
🐛 Bug and hot fixes
- fix(event_handler): revert regression when validating response (#6234) by @leandrodamascena
🔧 Maintenance
- chore(deps-dev): bump boto3-stubs from 1.37.6 to 1.37.7 (#6231) by @dependabot[bot]
- chore(deps-dev): bump aws-cdk from 2.1002.0 to 2.1003.0 (#6232) by @dependabot[bot]
- chore(deps-dev): bump jinja2 from 3.1.5 to 3.1.6 (#6224) by @dependabot[bot]
- chore(deps-dev): bump boto3-stubs from 1.37.5 to 1.37.6 (#6227) by @dependabot[bot]
- chore(deps-dev): bump cfn-lint from 1.27.0 to 1.28.0 (#6228) by @dependabot[bot]
- chore(deps-dev): bump aws-cdk-aws-lambda-python-alpha from 2.181.1a0 to 2.182.0a0 (#6226) by @dependabot[bot]
- chore(deps-dev): bump testcontainers from 4.9.1 to 4.9.2 (#6225) by @dependabot[bot]
- chore(deps): bump jinja2 from 3.1.5 to 3.1.6 in /docs (#6223) by @dependabot[bot]
- chore(deps-dev): bump aws-cdk-lib from 2.181.1 to 2.182.0 (#6222) by @dependabot[bot]
- chore(deps-dev): bump boto3-stubs from 1.37.4 to 1.37.5 (#6217) by @dependabot[bot]
- chore(deps): bump datadog-lambda from 6.105.0 to 6.106.0 (#6218) by @dependabot[bot]
- chore(deps-dev): bump pytest from 8.3.4 to 8.3.5 (#6206) by @dependabot[bot]
- chore(deps-dev): bump mkdocs-material from 9.6.5 to 9.6.7 (#6208) by @dependabot[bot]
- chore(deps-dev): bump cdklabs-generative-ai-cdk-constructs from 0.1.294 to 0.1.295 (#6207) by @dependabot[bot]
- chore(deps-dev): bump aws-cdk from 2.1001.0 to 2.1002.0 (#6209) by @dependabot[bot]
- chore(deps): bump squidfunk/mkdocs-material from
2615302
to047452c
in /docs (#6210) by @dependabot[bot] - chore(deps-dev): bump boto3-stubs from 1.37.3 to 1.37.4 (#6205) by @dependabot[bot]
- chore(deps-dev): bump cfn-lint from 1.26.1 to 1.27.0 (#6192) by @dependabot[bot]
- chore(deps): bump docker/setup-qemu-action from 3.5.0 to 3.6.0 (#6190) by @dependabot[bot]
- chore(deps-dev): bump cdklabs-generative-ai-cdk-constructs from 0.1.293 to 0.1.294 (#6193) by @dependabot[bot]
- chore(deps-dev): bump aws-cdk-aws-lambda-python-alpha from 2.181.0a0 to 2.181.1a0 (#6194) by @dependabot[bot]
- chore(deps-dev): bump ruff from 0.9.8 to 0.9.9 (#6195) by @dependabot[bot]
- chore(ci): enable SAR deployment (#6104) by @leandrodamascena
- chore(deps-dev): bump boto3-stubs from 1.37.2 to 1.37.3 (#6181) by @dependabot[bot]
- chore(deps-dev): bump isort from 6.0.0 to 6.0.1 (#6183) by @dependabot[bot]
- chore(deps): bump pydantic-settings from 2.8.0 to 2.8.1 (#6182) by @dependabot[bot]
- chore(deps-dev): bump ruff from 0.9.7 to 0.9.8 (#6184) by @dependabot[bot]
- chore(deps): bump codecov/codecov-action from 5.3.1 to 5.4.0 (#6180) by @dependabot[bot]
- chore(deps): bump docker/setup-buildx-action from 3.9.0 to 3.10.0 (#6175) by @dependabot[bot]
- chore(deps-dev): bump boto3-stubs from 1.37.0 to 1.37.1 (#6170) by @dependabot[bot]
- chore(deps): bump docker/setup-qemu-action from 3.4.0 to 3.5.0 (#6176) by @dependabot[bot]
- chore(deps): bump actions/download-artifact from 4.1.8 to 4.1.9 (#6174) by @dependabot[bot]
- chore(deps-dev): bump aws-cdk from 2.1000.3 to 2.1001.0 (#6173) by @dependabot[bot]
- chore(deps-dev): bump cfn-lint from 1.26.0 to 1.26.1 (#6169) by @dependabot[bot]
- chore(deps-dev): bump aws-cdk-aws-lambda-python-alpha from 2.180.0a0 to 2.181.0a0 (#6172) by @dependabot[bot]
- chore(deps-dev): bump cfn-lint from 1.25.1 to 1.26.0 (#6164) by @dependabot[bot]
- chore(deps-dev): bump boto3-stubs from 1.36.26 to 1.37.0 (#6165) by @dependabot[bot]
- chore(deps-dev): bump mypy-boto3-appconfigdata from 1.36.0 to 1.37.0 in the boto-typing group (#6163) by @dependabot[bot]
- chore(deps-dev): bump aws-cdk from 2.1000.2 to 2.1000.3 (#6162) by @dependabot[bot]
This release was made possible by the following contributors:
@dependabot[bot], @github-actions[bot], @leandrodamascena, @speshak, @tiagohconte, dependabot[bot] and github-actions[bot]