prefect.logging.loggers

Functions

get_logger

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

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 metadata

Raises:

  • MissingContextError: If no context can be found

flow_run_logger

flow_run_logger(flow_run: 'FlowRun', flow: Optional['Flow[Any, Any]'] = 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.

If the flow run context is available, see get_run_logger instead.

task_run_logger

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

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

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

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(*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

patch_print()

Patches the Python builtin print method to use print_as_log

Classes

PrefectLogAdapter

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:

process

process(self, msg: str, kwargs: MutableMapping[str, Any]) -> tuple[str, MutableMapping[str, Any]]

getChild

getChild(self, suffix: str, extra: dict[str, Any] | None = None) -> 'PrefectLogAdapter'

LogEavesdropper

A context manager that collects logs for the duration of the context

Example:

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`

```python
emit(self, record: LogRecord) -> None

The logging.Handler implementation, not intended to be called directly.

text

text(self) -> str

Return the collected logs as a single newline-delimited string