-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger_example.py
More file actions
50 lines (36 loc) · 1.55 KB
/
logger_example.py
File metadata and controls
50 lines (36 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Example demonstrating logger usage in DurableContext."""
from typing import Any
from aws_durable_execution_sdk_python.context import (
DurableContext,
durable_with_child_context,
)
from aws_durable_execution_sdk_python.execution import durable_execution
@durable_with_child_context
def child_workflow(ctx: DurableContext) -> str:
"""Child workflow with its own logging context."""
# Child context logger has step_id populated with child context ID
ctx.logger.info("Running in child context")
# Step in child context has nested step ID
child_result: str = ctx.step(
lambda _: "child-processed",
name="child_step",
)
ctx.logger.info("Child workflow completed", extra={"result": child_result})
return child_result
@durable_execution
def handler(event: Any, context: DurableContext) -> str:
"""Handler demonstrating logger usage."""
# Top-level context logger: no step_id field
context.logger.info("Starting workflow", extra={"eventId": event.get("id")})
# Logger in steps - gets enriched with step ID and attempt number
result1: str = context.step(
lambda _: "processed",
name="process_data",
)
context.logger.info("Step 1 completed", extra={"result": result1})
# Child contexts inherit the parent's logger and have their own step ID
result2: str = context.run_in_child_context(child_workflow(), name="child_workflow")
context.logger.info(
"Workflow completed", extra={"result1": result1, "result2": result2}
)
return f"{result1}-{result2}"