How to log output something like a structured dict object instead of a serialized string #2086
-
I am currently working on Rust on Lambda. However, when I started learning about loggers, I learned that normal logging is usually done with values in parallel directly under the I have been logging dictionaries whose contents I want to look at as shown below, which is actually convenient (in this case, the logs appear in the something_deep_dict = {
...
{
...
}
}
logger.info(something_deep_dict) How does PowerTools output structured logs? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
hey @mirumirumi, for Rust, I'd defer to @nmoutschen. For Python, we're not doing any special on that regard other than serializing to Here's a quick example I ran locally: from aws_lambda_powertools import Logger
from typing import Any
logger = Logger(service="discussion-2086")
nested_dict: dict[str, Any] = {
"products": [
{"id": "8dbc54d1-6e5c-4f9b-8f4c-2a5fd3e2b2a7", "description": "dummy"},
],
"customer_id": "95126722-4665-4a44-8150-8e12233c9bcd",
}
logger.info("Incoming products", some_key="some_value", **nested_dict) JSON output {
"level": "INFO",
"location": "<module>:13",
"message": "Incoming products",
"timestamp": "2023-04-05 17:14:46,838+0200",
"service": "discussion-2086",
"some_key": "some_value",
"products": [
{
"id": "8dbc54d1-6e5c-4f9b-8f4c-2a5fd3e2b2a7",
"description": "dummy"
}
],
"customer_id": "95126722-4665-4a44-8150-8e12233c9bcd"
} |
Beta Was this translation helpful? Give feedback.
hey @mirumirumi, for Rust, I'd defer to @nmoutschen. For Python, we're not doing any special on that regard other than serializing to
str
any non-serializable value. Also, note that you can have additional fields at the root (in parallel tomessage
key), when you pass additional key-value pairs after the logging message.Here's a quick example I ran locally: