Emit custom logs
To emit custom logs, useget_run_logger from within a flow or task.
get_run_logger support the standard Python logging methods. Any logs emitted by the logger will be associated with the flow run or task run they are emitted from and sent to the Prefect backend. Logs sent to the Prefect backend are visible in the Prefect UI.
Log with print statements
To sendprint statements to the Prefect backend as logs, set the log_prints kwarg to True on the flow or task.
log_prints kwarg is inherited by default by nested flow runs and tasks. To opt out of logging print statements for a specific task or flow, set log_prints=False on the child flow or task.
log_prints setting for all Prefect flow and task runs through the
PREFECT_LOGGING_LOG_PRINTS setting:
Log from subprocesses
When you spawn subprocesses inside a flow or task — for example, withmultiprocessing.Pool
or concurrent.futures.ProcessPoolExecutor — the Prefect run context is not automatically
available in the child process. This means get_run_logger() raises a MissingContextError.
Use with_context from prefect.context to propagate the current run context into
subprocess workers. Logs emitted with get_run_logger() in the child process are
associated with the parent flow run and task run and appear in the Prefect UI.
with_context also works with concurrent.futures.ProcessPoolExecutor and multiprocessing.Process.
Log from threads
When you spawn threads inside a flow or task — for example, withconcurrent.futures.ThreadPoolExecutor or threading.Thread — the
Prefect run context lives in contextvars
and is not automatically propagated to manually created worker
threads. Calling get_run_logger() from such a worker raises
MissingContextError.
Use contextvars.copy_context() to snapshot the current context in the
parent thread, then call your worker via Context.run(...) so it
executes with the snapshotted context active. Logs emitted with
get_run_logger() in the worker thread are then associated with the
parent flow run and task run and appear in the Prefect UI.