Skip to content

Commit 3853963

Browse files
committed
docs(logger): document arbitrary kwargs support in logger
1 parent cc7d9b4 commit 3853963

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

docs/core/logger.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ To ease routine tasks like extracting correlation ID from popular event sources,
158158
You can append additional keys using either mechanism:
159159

160160
* Persist new keys across all future log messages via `append_keys` method
161-
* Add additional keys on a per log message basis via `extra` parameter
161+
* Add additional keys on a per log message basis as a keyword=value, or via `extra` parameter
162162

163163
#### append_keys method
164164

@@ -184,14 +184,33 @@ You can append your own keys to your existing Logger via `append_keys(**addition
184184

185185
This example will add `order_id` if its value is not empty, and in subsequent invocations where `order_id` might not be present it'll remove it from the Logger.
186186

187+
#### ephemeral metadata
188+
189+
You can pass an arbitrary number of keyword arguments (kwargs) to all log level's methods, e.g. `logger.info, logger.warning`.
190+
191+
Two common use cases for this feature is to enrich log statements with additional metadata, or only add certain keys conditionally.
192+
193+
!!! info "Any keyword argument added will not be persisted in subsequent messages."
194+
195+
=== "append_keys_kwargs.py"
196+
197+
```python hl_lines="8"
198+
--8<-- "examples/logger/src/append_keys_kwargs.py"
199+
```
200+
201+
=== "append_keys_kwargs_output.json"
202+
203+
```json hl_lines="7"
204+
--8<-- "examples/logger/src/append_keys_kwargs_output.json"
205+
```
206+
187207
#### extra parameter
188208

189209
Extra parameter is available for all log levels' methods, as implemented in the standard logging library - e.g. `logger.info, logger.warning`.
190210

191211
It accepts any dictionary, and all keyword arguments will be added as part of the root structure of the logs for that log statement.
192212

193-
???+ info
194-
Any keyword argument added using `extra` will not be persisted for subsequent messages.
213+
!!! info "Any keyword argument added using `extra` will not be persisted in subsequent messages."
195214

196215
=== "append_keys_extra.py"
197216

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.utilities.typing import LambdaContext
3+
4+
logger = Logger()
5+
6+
7+
def handler(event: dict, context: LambdaContext) -> str:
8+
logger.info("Collecting payment", request_id="1123")
9+
10+
return "hello world"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"level": "INFO",
3+
"location": "collect.handler:8",
4+
"message": "Collecting payment",
5+
"timestamp": "2022-11-26 11:47:12,494+0200",
6+
"service": "payment",
7+
"request_id": "1123"
8+
}

0 commit comments

Comments
 (0)