INFO.
Customize logging configuration
Prefect provides several settings to configure the logging level and individual loggers. Any value in Prefect’s logging configuration file can be overridden through a Prefect setting of the formPREFECT_LOGGING_[PATH]_[TO]_[KEY]=value corresponding to the nested address of the field you are configuring.
For example, to change the default logging level for flow runs but not task runs, update your profile with:
WARNING level logs
to the console. As with other logging settings, you can override this from the environment or in the logging configuration
file. For example, you can change the level with the PREFECT_LOGGING_ROOT_LEVEL environment variable.
In some situations you may want to completely overhaul the Prefect logging configuration by providing your own logging.yml file.
You can create your own version of logging.yml in one of two ways:
- Create a
logging.ymlfile in yourPREFECT_HOMEdirectory (default is~/.prefect). - Specify a custom path to your
logging.ymlfile using thePREFECT_LOGGING_CONFIG_PATHsetting.
logging.yml file at the specified location, it will fall back to using the default logging configuration.
See the Python Logging configuration
documentation for more information about the configuration options and syntax used by logging.yml.
As with all Prefect settings, logging settings are loaded at runtime.
This means that to customize Prefect logging in a remote environment requires setting the appropriate environment variables and/or profile in that environment.
Formatters
Prefect log formatters specify the format of log messages. The default formatting for task and flow run records is"%(asctime)s.%(msecs)03d | %(levelname)-7s | Task run %(task_run_name)r - %(message)s" for tasks and
similarly "%(asctime)s.%(msecs)03d | %(levelname)-7s | Flow run %(flow_run_name)r - %(message)s" for flows.
The variables available to interpolate in log messages vary by logger.
In addition to the run context, message string,
and any keyword arguments, flow and task run loggers have access to additional variables.
The flow run logger has the following variables available for formatting:
flow_run_nameflow_run_idflow_namedeployment_name
task_run_idflow_run_idtask_run_nametask_nameflow_run_nameflow_name
logging.yml file as
described earlier.
For example, the following changes the formatting for the flow runs formatter:
Styles
By default, Prefect highlights specific keywords in the console logs with a variety of colors. You can toggle highlighting on/off with thePREFECT_LOGGING_COLORS setting:
styles
section of the Prefect logging configuration file for available keys.
Note that these style settings only impact the display within a terminal, not the Prefect UI.
- Copy and paste the following code into
my_package_or_module.py(rename as needed) in the same directory as the flow run script; or ideally as part of a Python package so it’s available insite-packagesand accessible anywhere within your environment.
- Update
~/.prefect/logging.ymlto usemy_package_or_module.CustomConsoleHandlerand additionally reference the base_style and named expression:log.email.
- On your next flow run, text that looks like an email is highlighted. For example,
my@email.comis colored in magenta below:
Apply markup in logs
To use Rich’s markup in Prefect logs, first configurePREFECT_LOGGING_MARKUP:
Include logs from other libraries
By default, Prefect won’t capture log statements from libraries that your flows and tasks use. You can tell Prefect to include logs from these libraries with thePREFECT_LOGGING_EXTRA_LOGGERS setting.
To use this setting, specify one or more Python library names to include,
separated by commas. For example, if you want Prefect to capture Dask
and SciPy logging statements with your flow and task run logs, use:
PREFECT_LOGGING_EXTRA_LOGGERS=dask,scipy
Configure this setting as an environment variable or in a profile. See
Settings for more details about how to use settings.
Disable logging
Prefect prints run logs to the console and sends them to the API. Use the settings in this section to turn off one or both outputs.Stop sending logs to the API
SetPREFECT_LOGGING_TO_API_ENABLED to false to keep logs out of the API.
This setting works with Prefect Cloud and with a self-hosted Prefect server.
false:
- Flow run logs, task run logs, Prefect Cloud worker logs, and logs from extra loggers are not sent to the API.
- Logs still print to the console.
- States, events, artifacts, and other run data are still sent to the API. Only log records are affected.
Where to set it
Prefect reads this setting in the process that runs your flow. Set it at the scope that matches how you run your flows:
Environment variables from these scopes are merged key by key, and the most specific scope wins.
A flow run’s
env overrides the deployment’s, the deployment’s overrides the work pool’s, and all of them override the worker’s own settings.
Silence a logger
To stop a logger from writing to the console and to the API, raise its level or disable it. To keep onlyWARNING and higher records from task runs, raise the level of the task run logger:
disable_run_logger.
Inside the block, get_run_logger() returns a logger that discards every record, even outside of a run.
This is useful when you call a task’s function directly in a test:
Keep one message out of the API
To print a single log record to the console without sending it to the API, passsend_to_api=False in the extra argument of the log call:
Silence the warning for logs outside of a run
Prefect can only send a log record to the API when the record belongs to a flow run. If a logger that is attached to the API handler is used outside of a run, Prefect drops the record and prints a warning. This happens, for example, when a library listed inPREFECT_LOGGING_EXTRA_LOGGERS logs outside of a flow run.
To turn the warning off:
warn. Set the value to error to raise an exception instead.