Skip to content

Commit 452cf91

Browse files
Adding documentation
1 parent f30eed0 commit 452cf91

File tree

6 files changed

+222
-3
lines changed

6 files changed

+222
-3
lines changed

aws_lambda_powertools/logging/buffer/config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def __init__(
1717
max_size: int = 20480,
1818
minimum_log_level: LOG_LEVEL_BUFFER_VALUES = "DEBUG",
1919
flush_on_error: bool = True,
20-
compress: bool = False,
2120
):
2221
"""
2322
Initialize logger buffer configuration.
@@ -33,12 +32,11 @@ def __init__(
3332
compress : bool, optional
3433
Whether to compress buffered logs
3534
"""
36-
self._validate_inputs(max_size, minimum_log_level, flush_on_error, compress)
35+
self._validate_inputs(max_size, minimum_log_level, flush_on_error)
3736

3837
self._max_size = max_size
3938
self._minimum_log_level = minimum_log_level.upper()
4039
self._flush_on_error = flush_on_error
41-
self._compress = compress
4240

4341
def _validate_inputs(
4442
self,

docs/core/logger.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,153 @@ The following environment variables are available to configure Logger at a globa
514514

515515
## Advanced
516516

517+
### Buffering logs
518+
519+
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.
520+
521+
!!! tip "This is useful when you want to keep logs clean while still providing detailed diagnostic information when needed."
522+
523+
=== "getting_started_with_buffering_logs.py"
524+
525+
```python hl_lines="5 8"
526+
--8<-- "examples/logger/src/getting_started_with_buffering_logs.py"
527+
```
528+
529+
#### Customizing Buffer configuration
530+
531+
When configuring log buffering, you have options to fine-tune how logs are captured, stored, and emitted. You can configure the following parameters in the `LoggerBufferConfig` constructor:
532+
533+
| Parameter | Description | Configuration |
534+
|-------------------- |------------------------------------------------ |----------------------------- |
535+
| `max_size` | Maximum size of the log buffer in bytes | `int` (default: 20480 bytes) |
536+
| `minimum_log_level` | Minimum log level to buffer | `DEBUG`, `INFO`, `WARNING` |
537+
| `flush_on_error` | Automatically flush buffer when an error occurs | `True` (default), `False` |
538+
539+
=== "working_with_buffering_logs_different_levels.py"
540+
541+
```python hl_lines="5 8"
542+
--8<-- "examples/logger/src/working_with_buffering_logs_different_levels.py"
543+
```
544+
545+
=== "working_with_buffering_logs_disable_on_error.py"
546+
547+
```python hl_lines="5 8"
548+
--8<-- "examples/logger/src/working_with_buffering_logs_disable_on_error.py"
549+
```
550+
551+
#### Flushing on uncaught exceptions
552+
553+
Use the `@logger.inject_lambda_context` decorator to automatically flush buffered logs when an uncaught exception occurs in your Lambda function. The `flush_buffer_on_uncaught_error` parameter captures and flush all buffered logs records before the Lambda execution terminates.
554+
555+
=== "working_with_buffering_logs_uncaught_exception.py"
556+
557+
```python hl_lines="5 8"
558+
--8<-- "examples/logger/src/working_with_buffering_logs_uncaught_exception.py"
559+
```
560+
561+
#### Buffering workflows
562+
563+
##### Flushing manually
564+
565+
```mermaid
566+
sequenceDiagram
567+
participant Client
568+
participant Lambda
569+
participant Logger
570+
participant CloudWatch
571+
572+
Client->>Lambda: Invoke Lambda
573+
Lambda->>Logger: Initialize with DEBUG level buffering
574+
Logger-->>Lambda: Logger buffer ready
575+
576+
Lambda->>Logger: logger.debug("First debug log")
577+
Logger-->>Logger: Buffer first debug log
578+
579+
Lambda->>Logger: logger.info("Info log")
580+
Logger->>CloudWatch: Directly log info message
581+
582+
Lambda->>Logger: logger.debug("Second debug log")
583+
Logger-->>Logger: Buffer second debug log
584+
585+
Lambda->>Logger: logger.flush_buffer()
586+
Logger->>CloudWatch: Flush buffered debug logs to CloudWatch
587+
588+
Lambda->>Client: Return execution result
589+
590+
```
591+
592+
##### Flushing on error
593+
594+
```mermaid
595+
sequenceDiagram
596+
participant Client
597+
participant Lambda
598+
participant Logger
599+
participant CloudWatch
600+
601+
Client->>Lambda: Invoke Lambda
602+
Lambda->>Logger: Initialize with DEBUG level buffering
603+
Logger-->>Lambda: Logger buffer ready
604+
605+
Lambda->>Logger: logger.debug("First log")
606+
Logger-->>Logger: Buffer first debug log
607+
608+
Lambda->>Logger: logger.debug("Second log")
609+
Logger-->>Logger: Buffer second debug log
610+
611+
Lambda->>Logger: logger.debug("Third log")
612+
Logger-->>Logger: Buffer third debug log
613+
614+
Lambda->>Lambda: Exception occurs
615+
Lambda->>Logger: logger.error("Error details")
616+
Logger->>CloudWatch: Send buffered debug logs
617+
Logger->>CloudWatch: Send error log
618+
619+
Lambda->>Client: Raise exception
620+
```
621+
622+
##### Flushing when uncaught exception happens
623+
624+
```mermaid
625+
sequenceDiagram
626+
participant Client
627+
participant Lambda
628+
participant Logger
629+
participant CloudWatch
630+
631+
Client->>Lambda: Invoke Lambda
632+
Lambda->>Logger: Using decorator @logger.inject_lambda_context(flush_buffer_on_uncaught_error=True)
633+
Logger-->>Lambda: Logger context injected
634+
635+
Lambda->>Logger: logger.debug("First log")
636+
Logger-->>Logger: Buffer first debug log
637+
638+
Lambda->>Logger: logger.debug("Second log")
639+
Logger-->>Logger: Buffer second debug log
640+
641+
Lambda->>Lambda: Uncaught Exception
642+
Lambda->>CloudWatch: Automatically send buffered debug logs
643+
644+
Lambda->>Client: Raise uncaught exception
645+
646+
```
647+
648+
#### Common buffering questions
649+
650+
When using log buffering to control log emissions in your AWS Lambda functions, it's important to follow best practices to avoid introducing complexity or unnecessary overhead. Keep these guidelines in mind:
651+
652+
1. **How can I prevent log buffering from consuming excessive memory?** Set a `max_size` in `LoggerBufferConfig` to limit the buffer's memory footprint.
653+
654+
2. **What happens if the log buffer reaches its maximum size?** The oldest logs are discarded when the buffer is full, making room for new logs. You need to set an appropriate `max_size` configuration.
655+
656+
3. **Can I customize when logs are flushed?** Yes, use the `flush_on_error=True` in `LoggerBufferConfig` or use `flush_buffer_on_uncaught_error` in `@logger.inject_lambda_context` decorator.
657+
658+
4. **What timestamp is used when I flush the logs?** 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.
659+
660+
5. **What happens if I try to add a log line that is bigger than max buffer size?** It will not buffer, but log as a normal log.
661+
662+
6. **What happens if Lambda times out without flushing the buffer?** Buffer will be lost and no buffered logs will be flushed.
663+
517664
### Built-in Correlation ID expressions
518665

519666
You can use any of the following built-in JMESPath expressions as part of [inject_lambda_context decorator](#setting-a-correlation-id).
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.logging.buffer import LoggerBufferConfig
3+
from aws_lambda_powertools.utilities.typing import LambdaContext
4+
5+
logger_buffer_config = LoggerBufferConfig(max_size=20480, flush_on_error=True)
6+
logger = Logger(level="INFO", logger_buffer=logger_buffer_config)
7+
8+
9+
def lambda_handler(event: dict, context: LambdaContext):
10+
logger.debug("a debug log") # this is buffered
11+
logger.info("an info log") # this is not buffered
12+
13+
# do stuff
14+
15+
logger.flush_buffer()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.logging.buffer import LoggerBufferConfig
3+
from aws_lambda_powertools.utilities.typing import LambdaContext
4+
5+
logger_buffer_config = LoggerBufferConfig(max_size=20480, minimum_log_level="WARNING")
6+
logger = Logger(level="INFO", logger_buffer=logger_buffer_config)
7+
8+
9+
def lambda_handler(event: dict, context: LambdaContext):
10+
logger.warning("a warning log") # this is buffered
11+
logger.info("an info log") # this is buffered
12+
logger.debug("a debug log") # this is buffered
13+
14+
# do stuff
15+
16+
logger.flush_buffer()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.logging.buffer import LoggerBufferConfig
3+
from aws_lambda_powertools.utilities.typing import LambdaContext
4+
5+
logger_buffer_config = LoggerBufferConfig(max_size=20480, flush_on_error=False)
6+
logger = Logger(level="INFO", logger_buffer=logger_buffer_config)
7+
8+
9+
class MyException(Exception):
10+
pass
11+
12+
13+
def lambda_handler(event: dict, context: LambdaContext):
14+
logger.debug("a debug log") # this is buffered
15+
16+
# do stuff
17+
18+
try:
19+
raise MyException
20+
except MyException as error:
21+
logger.error("An error ocurrend", exc_info=error) # Logs won't be flushed here
22+
23+
# Need to flush logs manually
24+
logger.flush_buffer()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.logging.buffer import LoggerBufferConfig
3+
from aws_lambda_powertools.utilities.typing import LambdaContext
4+
5+
logger_buffer_config = LoggerBufferConfig(max_size=20480, flush_on_error=False)
6+
logger = Logger(level="INFO", logger_buffer=logger_buffer_config)
7+
8+
9+
class MyException(Exception):
10+
pass
11+
12+
13+
@logger.inject_lambda_context(flush_buffer_on_uncaught_error=True)
14+
def lambda_handler(event: dict, context: LambdaContext):
15+
logger.debug("a debug log") # this is buffered
16+
17+
# do stuff
18+
19+
raise MyException # Logs will be flushed here

0 commit comments

Comments
 (0)