Skip to content

docs(logger): document enriching logs with logrecord attributes #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,25 @@ By default all registered loggers will be modified. You can change this behavior
---8<-- "examples/logger/src/cloning_logger_config.py"
```

**How can I add standard library logging attributes to a log record?**

The Python standard library log records contains a [large set of atttributes](https://docs.python.org/3/library/logging.html#logrecord-attributes), however only a few are included in the powertools log record by default.

If you need to add additional records, these can be included as `kwargs` to `Logger`, or to `LambdaPowertoolsFormatter` when they are being instantiated, or to managed later with the `append_keys` or `remove_keys` methods.

=== "collect.py"

```python hl_lines="3 8 10"
---8<-- "examples/logger/src/append_and_remove_keys.py"
```
=== "Example CloudWatch Logs excerpt"

```json hl_lines="6 15-16"
---8<-- "examples/logger/src/append_and_remove_keys.json"
```

For log records originating from powertools `Logger`, the `name` attribute will be the same as `service`, for log records coming from standard library logger, it will be the name of the logger (i.e. what was used as name argument to `logging.getLogger`).

**What's the difference between `append_keys` and `extra`?**

Keys added with `append_keys` will persist across multiple log messages while keys added via `extra` will only be available in a given log message operation.
Expand Down
20 changes: 20 additions & 0 deletions examples/logger/src/append_and_remove_keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"level": "INFO",
"location": "<module>:16",
"message": "Name should be equal service value",
"name": "payment",
"service": "payment",
"timestamp": "2022-07-01 07:09:46,330+0000"
},
{
"level": "INFO",
"location": "<module>:23",
"message": "This will include process ID and name",
"name": "payment",
"process": "9",
"processName": "MainProcess",
"service": "payment",
"timestamp": "2022-07-01 07:09:46,330+0000"
}
]
12 changes: 12 additions & 0 deletions examples/logger/src/append_and_remove_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from aws_lambda_powertools import Logger

logger = Logger(service="payment", name="%(name)s")

logger.info("Name should be equal service value")

additional_log_attributes = {"process": "%(process)d", "processName": "%(processName)s"}
logger.append_keys(**additional_log_attributes)
logger.info("This will include process ID and name")
logger.remove_keys(["processName"])

# further messages will not include processName