Skip to main content

Emit custom logs

To emit custom logs, use get_run_logger from within a flow or task.
The logger returned by 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.
get_run_logger() can only be used in the context of a flow or task run. Calling it outside that context — for example, inside a state change hook — raises a MissingContextError.To log from a state change hook and have logs appear in the Prefect UI, use flow_run_logger or task_run_logger from prefect.logging.loggers. See state change hook for more details.To use a normal Python logger anywhere with your same configuration, use get_logger() from prefect.logging. The logger retrieved with get_logger() will not send log records to the Prefect API.

Log with print statements

To send print statements to the Prefect backend as logs, set the log_prints kwarg to True on the flow or task.
The 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.
You can configure the default 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, with multiprocessing.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, with concurrent.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.
If you control the task body but not the worker function (for example, you call into a third-party library), the same pattern applies: build a small wrapper that takes a copied context plus the worker arguments, and submit the wrapper to the thread pool.
Prefect’s built-in ThreadPoolTaskRunner already propagates the run context for you, so prefer it when you can express your parallelism as Prefect tasks. The pattern above is for cases where you need a raw ThreadPoolExecutor — for example, when parallelizing inside a single task to avoid per-task overhead, or when calling library code that manages its own thread pool.

Access logs from the command line

You can retrieve logs for a specific flow run ID using Prefect’s CLI:
This can be particularly helpful if you want to access the logs as a local file: