Skip to content

Commit 66e16f0

Browse files
committed
add examples to doc
1 parent a83fcc5 commit 66e16f0

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

docs/core/logger.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,17 @@ You can append your own keys to your existing Logger via `append_keys(**addition
194194

195195
The append_context_keys method allows temporary modification of a Logger instance's context without creating a new logger. It's useful for adding context keys to specific workflows while maintaining the logger's overall state and simplicity.
196196

197-
* Add examples
197+
=== "append_context_keys.py"
198+
199+
```python hl_lines="7 8"
200+
--8<-- "examples/logger/src/append_context_keys.py"
201+
```
202+
203+
=== "append_context_keys_output.json"
204+
205+
```json hl_lines="8 9"
206+
--8<-- "examples/logger/src/append_context_keys.json"
207+
```
198208

199209
#### ephemeral metadata
200210

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"level": "INFO",
4+
"location": "lambda_handler:8",
5+
"message": "Log with context",
6+
"timestamp": "2024-03-21T10:30:00.123Z",
7+
"service": "example_service",
8+
"user_id": "123",
9+
"operation": "process"
10+
},
11+
{
12+
"level": "INFO",
13+
"location": "lambda_handler:10",
14+
"message": "Log without context",
15+
"timestamp": "2024-03-21T10:30:00.124Z",
16+
"service": "example_service"
17+
}
18+
]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.utilities.typing import LambdaContext
3+
4+
logger = Logger(service="example_service")
5+
6+
7+
def lambda_handler(event: dict, context: LambdaContext) -> str:
8+
with logger.append_context_keys(user_id="123", operation="process"):
9+
logger.info("Log with context")
10+
11+
logger.info("Log without context")
12+
13+
return "hello world"

0 commit comments

Comments
 (0)