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
If you're migrating from other Loggers, there are few key points to be aware of: [Service parameter](#the-service-parameter), [Inheriting Loggers](#inheriting-loggers), [Overriding Log records](#overriding-log-records), and [Logging exceptions](#logging-exceptions).
@@ -645,7 +671,6 @@ Logger allows you to either change the format or suppress the following keys alt
645
671
}
646
672
```
647
673
648
-
649
674
#### Reordering log keys position
650
675
651
676
You can change the order of [standard Logger keys](#standard-structured-keys) or any keys that will be appended later at runtime via the `log_record_order` parameter.
@@ -744,9 +769,30 @@ By default, Logger uses StreamHandler and logs to standard output. You can overr
744
769
745
770
#### Bring your own formatter
746
771
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.
772
+
By default, Logger uses [LambdaPowertoolsFormatter](#lambdapowertoolsformatter) 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.
773
+
774
+
For **minor changes like remapping keys** after all log record processing has completed, you can override `serialize` method from [LambdaPowertoolsFormatter](#lambdapowertoolsformatter):
775
+
776
+
=== "custom_formatter.py"
777
+
778
+
```python
779
+
from aws_lambda_powertools import Logger
780
+
from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter
781
+
782
+
from typing import Dict
783
+
784
+
class CustomFormatter(LambdaPowertoolsFormatter):
785
+
def serialize(self, log: Dict) -> str:
786
+
"""Serialize final structured log dict to JSON str"""
787
+
log["event"] = log.pop("message") # rename message key to event
788
+
return self.json_serializer(log) # use configured json serializer
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.
795
+
For **replacing the formatter entirely**, 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](#capturing-lambda-context-info) and [sampling](#sampling-debug-logs) will continue to work.
750
796
751
797
!!! info
752
798
You might need to implement `remove_keys` method if you make use of the feature too.
@@ -758,7 +804,7 @@ For this, you can subclass `BasePowertoolsFormatter`, implement `append_keys` me
758
804
from aws_lambda_powertools.logging.formatter import BasePowertoolsFormatter
759
805
760
806
class CustomFormatter(BasePowertoolsFormatter):
761
-
custom_format = {} # will hold our structured keys
807
+
custom_format = {} # arbitrary dict to hold our structured keys
0 commit comments