Skip to content

Commit 4b8f556

Browse files
committed
docs: add json_default parameter
1 parent 13a3196 commit 4b8f556

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

docs/core/logger.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ You can change the order of [standard Logger keys](#standard-structured-keys) or
676676
}
677677
```
678678

679-
#### Setting timestamp to UTC
679+
### Setting timestamp to UTC
680680

681681
By default, this Logger and standard logging library emits records using local time timestamp. You can override this behaviour via `utc` parameter:
682682

@@ -692,6 +692,37 @@ By default, this Logger and standard logging library emits records using local t
692692
logger_in_utc.info("GMT time zone")
693693
```
694694

695+
### Custom function for unserializable values
696+
697+
By default, Logger uses `str` to handle values non-serializable by JSON. You can override this behaviour via `json_default` parameter by passing a Callable:
698+
699+
=== "collect.py"
700+
701+
```python hl_lines="3-4 9 12"
702+
from aws_lambda_powertools import Logger
703+
704+
def custom_json_default(value):
705+
return f"<non-serializable: {type(value).__name__}>"
706+
707+
class Unserializable:
708+
pass
709+
710+
logger = Logger(service="payment", json_default=custom_json_default)
711+
712+
def handler(event, context):
713+
logger.info(Unserializable())
714+
```
715+
=== "Example CloudWatch Logs excerpt"
716+
```json hl_lines="4"
717+
{
718+
"level": "INFO",
719+
"location": "collect.handler:8",
720+
"message": ""<non-serializable: Unserializable>"",
721+
"timestamp": "2021-05-03 15:17:23,632+0200",
722+
"service": "payment"
723+
}
724+
```
725+
695726
## Built-in Correlation ID expressions
696727

697728
You can use any of the following built-in JMESPath expressions as part of [inject_lambda_context decorator](#setting-a-correlation-id).

0 commit comments

Comments
 (0)