> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# handlers

# `prefect.logging.handlers`

## Functions

### `set_api_log_sink` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L54](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L54)</sup>

```python theme={null}
set_api_log_sink(sink: Callable[[Dict[str, Any]], None] | None) -> None
```

### `emit_api_log` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L59](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L59)</sup>

```python theme={null}
emit_api_log(log: Dict[str, Any]) -> None
```

## Classes

### `APILogWorker` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L67](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L67)</sup>

**Methods:**

#### `instance` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L128](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L128)</sup>

```python theme={null}
instance(cls: Type[Self], *args: Any) -> Self
```

#### `max_batch_size` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L69](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L69)</sup>

```python theme={null}
max_batch_size(self) -> int
```

#### `min_interval` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L82](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L82)</sup>

```python theme={null}
min_interval(self) -> float | None
```

#### `send` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L85](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L85)</sup>

```python theme={null}
send(self, item: Dict[str, Any] | bytes) -> None
```

Queue a log to send to the API.

Accepts a `LogCreate`-compatible dict, such as one returned by
`APILogHandler.prepare`, or its serialized JSON. Logs are queued as JSON
so each one is held once and is not serialized again when its batch is
sent.

### `APILogHandler` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L144](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L144)</sup>

A logging handler that sends logs to the Prefect API.

Sends log records to the `APILogWorker` which manages sending batches of logs in
the background.

**Methods:**

#### `aflush` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L179](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L179)</sup>

```python theme={null}
aflush(cls) -> None
```

Tell the `APILogWorker` to send any currently enqueued logs and block until
completion.

#### `emit` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L189](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L189)</sup>

```python theme={null}
emit(self, record: logging.LogRecord) -> None
```

Send a log to the `APILogWorker`

#### `flush` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L152](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L152)</sup>

```python theme={null}
flush(self) -> None
```

Tell the `APILogWorker` to send any currently enqueued logs and block until
completion.

Use `aflush` from async contexts instead.

#### `handleError` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L209](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L209)</sup>

```python theme={null}
handleError(self, record: logging.LogRecord) -> None
```

#### `prepare` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L232](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L232)</sup>

```python theme={null}
prepare(self, record: logging.LogRecord) -> Dict[str, Any]
```

Convert a `logging.LogRecord` to the API `LogCreate` schema and serialize.

This infers the linked flow or task run from the log record or the current
run context.

If a flow run id cannot be found, the log will be dropped.

Logs exceeding the maximum size will be dropped.

### `WorkerAPILogHandler` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L323](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L323)</sup>

**Methods:**

#### `aflush` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L179](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L179)</sup>

```python theme={null}
aflush(cls) -> None
```

Tell the `APILogWorker` to send any currently enqueued logs and block until
completion.

#### `emit` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L324](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L324)</sup>

```python theme={null}
emit(self, record: logging.LogRecord) -> None
```

#### `emit` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L189](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L189)</sup>

```python theme={null}
emit(self, record: logging.LogRecord) -> None
```

Send a log to the `APILogWorker`

#### `flush` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L152](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L152)</sup>

```python theme={null}
flush(self) -> None
```

Tell the `APILogWorker` to send any currently enqueued logs and block until
completion.

Use `aflush` from async contexts instead.

#### `handleError` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L209](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L209)</sup>

```python theme={null}
handleError(self, record: logging.LogRecord) -> None
```

#### `prepare` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L332](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L332)</sup>

```python theme={null}
prepare(self, record: logging.LogRecord) -> Dict[str, Any]
```

Convert a `logging.LogRecord` to the API `LogCreate` schema and serialize.

This will add in the worker id to the log.

Logs exceeding the maximum size will be dropped.

#### `prepare` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L232](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L232)</sup>

```python theme={null}
prepare(self, record: logging.LogRecord) -> Dict[str, Any]
```

Convert a `logging.LogRecord` to the API `LogCreate` schema and serialize.

This infers the linked flow or task run from the log record or the current
run context.

If a flow run id cannot be found, the log will be dropped.

Logs exceeding the maximum size will be dropped.

### `PrefectConsoleHandler` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L398](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L398)</sup>

**Methods:**

#### `emit` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L438](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/logging/handlers.py#L438)</sup>

```python theme={null}
emit(self, record: logging.LogRecord) -> None
```
