How to add logging to a workflow
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.
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:
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: