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.