---
title: loggers
sidebarTitle: loggers
---

# `prefect.logging.loggers`

## Functions

### `get_logger` <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L64" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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#L91" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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` <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L157" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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` <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L183" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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#L225" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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#L251" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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#L271" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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#L281" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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#L334" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
patch_print()
```


Patches the Python builtin `print` method to use `print_as_log`


## Classes

### `PrefectLogAdapter` <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/loggers.py#L33" 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#L49" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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#L43" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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#L349" 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#L402" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
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#L406" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python
text(self) -> str
```

Return the collected logs as a single newline-delimited string

