docs/v3/api-ref/python/prefect-logging-loggers.mdx
prefect.logging.loggersget_logger <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L65" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_logger(name: str | None = None) -> logging.Logger
Get a prefect logger. These loggers are intended for internal use within the
prefect package.
See get_run_logger for retrieving loggers for use within task or flow runs.
By default, only run-related loggers are connected to the APILogHandler.
get_run_logger <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L92" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_run_logger(context: Optional['RunContext'] = None, **kwargs: Any) -> Union[logging.Logger, LoggingAdapter]
Get a Prefect logger for the current task run or flow run.
The logger will be named either prefect.task_runs or prefect.flow_runs.
Contextual data about the run will be attached to the log records.
These loggers are connected to the APILogHandler by default to send log records to
the API.
Args:
context: A specific context may be provided as an override. By default, the
context is inferred from global state and this should not be needed.**kwargs: Additional keyword arguments will be attached to the log records in
addition to the run metadataRaises:
MissingContextError: If no context can be foundflow_run_logger <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L178" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>flow_run_logger(flow_run: 'FlowRun | None' = None, flow: 'Flow[Any, Any] | None' = None, flow_run_id: UUID | None = None, **kwargs: str) -> PrefectLogAdapter
Create a flow run logger with the run's metadata attached.
Additional keyword arguments can be provided to attach custom data to the log records.
Accepts a FlowRun object or a bare flow_run_id UUID. At least one must
be provided. When only flow_run_id is given, flow_run_name and
flow_name default to "<unknown>". If both are provided, flow_run
takes precedence.
If the flow run context is available, see get_run_logger instead.
Raises:
ValueError: If neither flow_run nor flow_run_id is provided.task_run_logger <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L222" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>task_run_logger(task_run: 'TaskRun', task: Optional['Task[Any, Any]'] = None, flow_run: Optional['FlowRun'] = None, flow: Optional['Flow[Any, Any]'] = None, **kwargs: Any) -> LoggingAdapter
Create a task run logger with the run's metadata attached.
Additional keyword arguments can be provided to attach custom data to the log records.
If the task run context is available, see get_run_logger instead.
If only the flow run context is available, it will be used for default values
of flow_run and flow.
get_worker_logger <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L266" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_worker_logger(worker: 'BaseWorker[Any, Any, Any]', name: Optional[str] = None) -> logging.Logger | LoggingAdapter
Create a worker logger with the worker's metadata attached.
If the worker has a backend_id, it will be attached to the log records. If the worker does not have a backend_id a basic logger will be returned. If the worker does not have a backend_id attribute, a basic logger will be returned.
disable_logger <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L292" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>disable_logger(name: str)
Get a logger by name and disables it within the context manager. Upon exiting the context manager, the logger is returned to its original state.
disable_run_logger <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L312" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>disable_run_logger()
Gets both prefect.flow_run and prefect.task_run and disables them
within the context manager. Upon exiting the context manager, both loggers
are returned to their original state.
print_as_log <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L322" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>print_as_log(*args: Any, **kwargs: Any) -> None
A patch for print to send printed messages to the Prefect run logger.
If no run is active, print will behave as if it were not patched.
If print sends data to a file other than sys.stdout or sys.stderr, it will
not be forwarded to the Prefect logger either.
patch_print <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L380" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>patch_print()
Patches the Python builtin print method to use print_as_log
PrefectLogAdapter <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L34" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Adapter that ensures extra kwargs are passed through correctly; without this
the extra fields set on the adapter would overshadow any provided on a
log-by-log basis.
See https://bugs.python.org/issue32732 — the Python team has declared that this is not a bug in the LoggingAdapter and subclassing is the intended workaround.
Methods:
getChild <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L50" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>getChild(self, suffix: str, extra: dict[str, Any] | None = None) -> 'PrefectLogAdapter'
process <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L44" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>process(self, msg: str, kwargs: MutableMapping[str, Any]) -> tuple[str, MutableMapping[str, Any]]
LogEavesdropper <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L395" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>A context manager that collects logs for the duration of the context
Example:
```python
import logging
from prefect.logging import LogEavesdropper
with LogEavesdropper("my_logger") as eavesdropper:
logging.getLogger("my_logger").info("Hello, world!")
logging.getLogger("my_logger.child_module").info("Another one!")
print(eavesdropper.text())
# Outputs: "Hello, world!
Another one!"
Methods:
emit <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L448" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>emit(self, record: LogRecord) -> None
The logging.Handler implementation, not intended to be called directly.
text <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L452" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>text(self) -> str
Return the collected logs as a single newline-delimited string