You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/logger.md
+60-1Lines changed: 60 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,7 @@ You can enrich your structured logs with key Lambda context information via `inj
71
71
72
72
@logger.inject_lambda_context
73
73
def handler(event, context):
74
-
logger.info("Collecting payment")
74
+
logger.info("Collecting payment")
75
75
76
76
# You can log entire objects too
77
77
logger.info({
@@ -742,6 +742,65 @@ By default, Logger uses StreamHandler and logs to standard output. You can overr
742
742
logger.info("Collecting payment")
743
743
```
744
744
745
+
#### Bring your own formatter
746
+
747
+
By default, Logger uses a custom Formatter that persists its custom structure between non-cold start invocations. There could be scenarios where the existing feature set isn't sufficient to your formatting needs.
748
+
749
+
For this, you can subclass `BasePowertoolsFormatter`, implement `append_keys` method, and override `format` standard logging method. This ensures the current feature set of Logger like injecting Lambda context and sampling will continue to work.
750
+
751
+
!!! info
752
+
You might need to implement `remove_keys` method if you make use of the feature too.
753
+
754
+
=== "collect.py"
755
+
756
+
```python hl_lines="2 4 7 12 16 27"
757
+
from aws_lambda_powertools import Logger
758
+
from aws_lambda_powertools.logging.formatter import BasePowertoolsFormatter
759
+
760
+
class CustomFormatter(BasePowertoolsFormatter):
761
+
custom_format = {} # will hold our structured keys
762
+
763
+
def append_keys(self, **additional_keys):
764
+
# also used by `inject_lambda_context` decorator
765
+
self.custom_format.update(additional_keys)
766
+
767
+
# Optional unless you make use of this Logger feature
0 commit comments