Skip to content

Commit e8f9ad1

Browse files
kbakkheitorlessa
andauthored
docs(logger): document enriching logs with logrecord attributes (#1271)
Co-authored-by: Heitor Lessa <[email protected]>
1 parent 94de494 commit e8f9ad1

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

docs/core/logger.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ POWERTOOLS_LOG_DEDUPLICATION_DISABLED="1" pytest -o log_cli=1
585585

586586
## FAQ
587587

588-
**How can I enable boto3 and botocore library logging?**
588+
### How can I enable boto3 and botocore library logging?
589589

590590
You can enable the `botocore` and `boto3` logs by using the `set_stream_logger` method, this method will add a stream handler
591591
for the given name and level to the logging module. By default, this logs all boto3 messages to stdout.
@@ -594,7 +594,7 @@ for the given name and level to the logging module. By default, this logs all bo
594594
---8<-- "examples/logger/src/enabling_boto_logging.py"
595595
```
596596

597-
**How can I enable powertools logging for imported libraries?**
597+
### How can I enable Powertools logging for imported libraries?
598598

599599
You can copy the Logger setup to all or sub-sets of registered external loggers. Use the `copy_config_to_registered_logger` method to do this.
600600

@@ -604,7 +604,28 @@ By default all registered loggers will be modified. You can change this behavior
604604
---8<-- "examples/logger/src/cloning_logger_config.py"
605605
```
606606

607-
**What's the difference between `append_keys` and `extra`?**
607+
### How can I add standard library logging attributes to a log record?
608+
609+
The Python standard library log records contains a [large set of atttributes](https://docs.python.org/3/library/logging.html#logrecord-attributes){target="_blank"}, however only a few are included in Powertools Logger log record by default.
610+
611+
You can include any of these logging attributes as key value arguments (`kwargs`) when instantiating `Logger` or `LambdaPowertoolsFormatter`.
612+
613+
You can also add them later anywhere in your code with `append_keys`, or remove them with `remove_keys` methods.
614+
615+
=== "collect.py"
616+
617+
```python hl_lines="3 8 10"
618+
---8<-- "examples/logger/src/append_and_remove_keys.py"
619+
```
620+
=== "Example CloudWatch Logs excerpt"
621+
622+
```json hl_lines="6 15-16"
623+
---8<-- "examples/logger/src/append_and_remove_keys.json"
624+
```
625+
626+
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`).
627+
628+
### What's the difference between `append_keys` and `extra`?
608629

609630
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.
610631

@@ -622,6 +643,6 @@ Here's an example where we persist `payment_id` not `request_id`. Note that `pay
622643
---8<-- "examples/logger/src/append_keys_vs_extra_output.json"
623644
```
624645

625-
**How do I aggregate and search Powertools logs across accounts?**
646+
### How do I aggregate and search Powertools logs across accounts?
626647

627648
As of now, ElasticSearch (ELK) or 3rd party solutions are best suited to this task. Please refer to this [discussion for more details](https://github.com/awslabs/aws-lambda-powertools-python/issues/460)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"level": "INFO",
4+
"location": "<module>:16",
5+
"message": "Name should be equal service value",
6+
"name": "payment",
7+
"service": "payment",
8+
"timestamp": "2022-07-01 07:09:46,330+0000"
9+
},
10+
{
11+
"level": "INFO",
12+
"location": "<module>:23",
13+
"message": "This will include process ID and name",
14+
"name": "payment",
15+
"process": "9",
16+
"processName": "MainProcess",
17+
"service": "payment",
18+
"timestamp": "2022-07-01 07:09:46,330+0000"
19+
}
20+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from aws_lambda_powertools import Logger
2+
3+
logger = Logger(service="payment", name="%(name)s")
4+
5+
logger.info("Name should be equal service value")
6+
7+
additional_log_attributes = {"process": "%(process)d", "processName": "%(processName)s"}
8+
logger.append_keys(**additional_log_attributes)
9+
logger.info("This will include process ID and name")
10+
logger.remove_keys(["processName"])
11+
12+
# further messages will not include processName

examples/logger/src/cloning_logger_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
logger = Logger()
77

8-
external_logger = logging.logger()
8+
external_logger = logging.getLogger()
99

1010
utils.copy_config_to_registered_loggers(source_logger=logger)
1111
external_logger.info("test message")

0 commit comments

Comments
 (0)