Pascal Jahan Elahi (Pawsey)
12/13/2024, 8:35 AMMarvin
12/13/2024, 8:36 AMFlowRunContext
and TaskRunContext
to access this information. Here's an example snippet:
python
from prefect.context import FlowRunContext, TaskRunContext
# Accessing the flow run context
flow_run_ctx = FlowRunContext.get()
if flow_run_ctx:
flow_run_id = flow_run_ctx.flow_run_id
<http://logger.info|logger.info>(f"Logged from flow run: {flow_run_id}")
# Accessing the task run context
task_run_ctx = TaskRunContext.get()
if task_run_ctx:
<http://logger.info|logger.info>(f"Logged from task run: {task_run_ctx.task_run_id}")
This code checks whether the context is available and logs information accordingly.
In Prefect 3.x, you access the runtime context using the prefect.runtime
module. The context object allows inspecting details about the current run environment. Example:
python
from prefect.runtime import flow_run, task_run
# Accessing flow run information
flow_run_id = flow_run.id
<http://logger.info|logger.info>(f"Logged from flow run: {flow_run_id}")
# Accessing task run information
task_run_id = task_run.id
<http://logger.info|logger.info>(f"Logged from task run: {task_run_id}")
Both versions use logging through get_run_logger()
for contextual logs, ensuring you're logging within the flow or task context.
For more detailed exploration:
- Prefect 2.x Runtime Context: Prefect 2.x Runtime Context Docs
- Prefect 3.x Runtime Context: Prefect 3.x Runtime Context Details
Given our aim to capture the essence of the human predicament in logging, remember: it's not a bug, it's an undocumented feature!