Skip to content

Commit a5a12a5

Browse files
Adding documentation
1 parent 46a9ab9 commit a5a12a5

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

docs/core/logger.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,31 @@ Use the `@logger.inject_lambda_context` decorator to automatically flush buffere
558558
--8<-- "examples/logger/src/working_with_buffering_logs_uncaught_exception.py"
559559
```
560560

561+
#### Reutilizing same buffer instance
562+
563+
Reuse the same logger instance across multiple files within a service. Doing this you can centralize logger instance creation and prevent buffer configuration drift.
564+
565+
!!! note "Buffer Inheritance"
566+
Loggers created with the same `service_name` automatically inherit the buffer configuration from the first initialized logger with a buffer configuration.
567+
568+
=== "working_with_buffering_logs_creating_instance.py"
569+
570+
```python hl_lines="5 8"
571+
--8<-- "examples/logger/src/working_with_buffering_logs_creating_instance.py"
572+
```
573+
574+
=== "working_with_buffering_logs_reusing_handler.py"
575+
576+
```python hl_lines="5 8"
577+
--8<-- "examples/logger/src/working_with_buffering_logs_reusing_handler.py"
578+
```
579+
580+
=== "working_with_buffering_logs_reusing_function.py"
581+
582+
```python hl_lines="5 8"
583+
--8<-- "examples/logger/src/working_with_buffering_logs_reusing_function.py"
584+
```
585+
561586
#### Buffering workflows
562587

563588
##### Flushing manually
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.logging.buffer import LoggerBufferConfig
3+
4+
logger_buffer_config = LoggerBufferConfig(max_size=20480, minimum_log_level="WARNING")
5+
logger = Logger(level="INFO", logger_buffer=logger_buffer_config)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from working_with_buffering_logs_creating_instance import logger # reusing same instance
2+
3+
4+
def my_function():
5+
logger.debug("This will be buffered")
6+
# do stuff
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from working_with_buffering_logs_creating_instance import logger # reusing same instance
2+
from working_with_buffering_logs_reusing_function import my_function
3+
4+
from aws_lambda_powertools.utilities.typing import LambdaContext
5+
6+
7+
def lambda_handler(event: dict, context: LambdaContext):
8+
logger.debug("a debug log") # this is buffered
9+
10+
my_function()
11+
12+
logger.flush_buffer()

0 commit comments

Comments
 (0)