---
title: FastlyLogHandler
summary: A logging handler that sends logs to a Fastly endpoint.
url: https://www.fastly.com/documentation/reference/compute/sdks/python/fastly_compute/log/class.FastlyLogHandler/
---

**Kind:** Class  
**Module:** [log](https://www.fastly.com/documentation/reference/compute/sdks/python/fastly_compute/log/)  
**Package:** fastly-compute 0.1.3  
**Source:** https://github.com/fastly/compute-sdk-python/blob/2bffcc6d596a68bcc0b4342404641deff78e68f6/fastly_compute/log.py#L92

Extends `logging.Handler`.

A logging handler that sends logs to a Fastly endpoint.

This handler integrates with Python's standard logging framework,
allowing you to use familiar logging patterns with Fastly's
Real-Time Log Streaming.

The handler supports routing logs from different loggers to different
endpoints using an endpoint_mapper function.

```python
import logging
from fastly_compute import FastlyLogHandler

# Simple: single endpoint for all loggers
handler = FastlyLogHandler("my_logs")
logger = logging.getLogger("my_app")
logger.addHandler(handler)

# Advanced: route different loggers to different endpoints
def route_logs(logger_name: str) -> str:
    if logger_name.startswith("api"):
        return "api-logs"
    elif logger_name.startswith("worker"):
        return "worker-logs"
    return "app-logs"  # default

handler = FastlyLogHandler(endpoint_mapper=route_logs)

# Or use a dict for simple mappings
endpoint_map = {"api": "api-logs", "worker": "worker-logs"}
handler = FastlyLogHandler(
    "default",
    endpoint_mapper=lambda name: endpoint_map.get(name, "app-logs")
)

# Use logger
logger.info("Request received", extra={"user_id": 123})
logger.error("Error processing request", exc_info=True)
```

## Constructor

**Source:** https://github.com/fastly/compute-sdk-python/blob/2bffcc6d596a68bcc0b4342404641deff78e68f6/fastly_compute/log.py#L134

```python
FastlyLogHandler(default_endpoint: str | None, endpoint_mapper: Callable[[str], str | None] | None = None, level=logging.NOTSET)
```

Initialize the handler.

**Parameters**

- `default_endpoint`: `str | None` — The default endpoint name (used when no mapper provided)
- `endpoint_mapper`: `Callable[[str], str | None] | None` (optional) = `None`

  Optional callable that maps logger name to endpoint name.
  Signature: (logger_name: str) -> endpoint_name: str
- `level`: `Any` (optional) = `logging.NOTSET` — Minimum logging level to handle (default: NOTSET)

**Throws**

- [ValueError](https://docs.python.org/3/library/exceptions.html#ValueError) — If neither default_endpoint nor endpoint_mapper is provided

```python
# Simple: single endpoint
handler = FastlyLogHandler("my_logs")

# Advanced: route by logger name
handler = FastlyLogHandler(
    endpoint_mapper=lambda name: f"{name}-logs"
)

# With fallback
handler = FastlyLogHandler(
    default_endpoint="app-logs",
    endpoint_mapper=lambda name: "api-logs" if "api" in name else None
)
```

## Methods

### close

**Source:** https://github.com/fastly/compute-sdk-python/blob/2bffcc6d596a68bcc0b4342404641deff78e68f6/fastly_compute/log.py#L227

```python
close()
```

Close the handler and release all endpoint resources.

This is called automatically when the handler is garbage collected
or when logging.shutdown() is called.

### emit

**Source:** https://github.com/fastly/compute-sdk-python/blob/2bffcc6d596a68bcc0b4342404641deff78e68f6/fastly_compute/log.py#L206

```python
emit(record: logging.LogRecord)
```

Emit a record to the Fastly logging endpoint.

This method is called automatically by the logging framework.
You should not need to call it directly.

The endpoint is determined by calling endpoint_mapper with the
logger name, or using the default_endpoint.

**Parameters**

- `record`: `logging.LogRecord` — The log record to emit
