# Axiom Go adapter for slog





Use the adapter of the Axiom Go SDK to send logs generated by the [log/slog](https://pkg.go.dev/log/slog) package of the Go standard library to Axiom. The adapter is a [slog.Handler](https://pkg.go.dev/log/slog#Handler), so you can use it with `slog.New` like any other handler.

<Note>
  The Axiom Go SDK is an open-source project and welcomes your contributions. For more information, see the [GitHub repository](https://github.com/axiomhq/axiom-go).
</Note>

<Prerequisites />

* Go 1.26 or later. This is the minimum Go version of the Axiom Go SDK.

## Set up SDK [#set-up-sdk]

1. Install the Axiom Go SDK and configure your environment as explained in [Send data from Go app to Axiom](/guides/go).

2. Add the adapter package to your Go module:

   ```shell
   go get github.com/axiomhq/axiom-go/adapters/slog
   ```

3. In your Go app, import the `slog` adapter package. It’s imported as `adapter` so that it doesn’t conflict with the standard library `log/slog` package.

   ```go
   import adapter "github.com/axiomhq/axiom-go/adapters/slog"
   ```

By default, the adapter reads the dataset name from the `AXIOM_DATASET` environment variable. If neither the variable nor `SetDataset` sets a dataset, `New` returns `ErrMissingDatasetName`. Alternatively, configure the adapter using [options](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/slog#Option) passed to the [New](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/slog#New) function:

```go
handler, err := adapter.New(
    adapter.SetDataset("DATASET_NAME"),
)
```

<ReplaceDataset />

## Send logs [#send-logs]

The following example creates an Axiom handler, sends logs to both Axiom and `stderr`, and flushes the buffered logs when the app exits:

```go
package main

import (
    "log"
    "log/slog"
    "os"

    adapter "github.com/axiomhq/axiom-go/adapters/slog"
)

func main() {
    // Create the Axiom handler.
    handler, err := adapter.New(
        adapter.SetDataset("DATASET_NAME"),
    )
    if err != nil {
        log.Fatal(err)
    }
    // Flush buffered logs to Axiom before the app exits.
    defer handler.Close()

    // Send logs to Axiom and print them to stderr.
    logger := slog.New(slog.NewMultiHandler(
        handler,
        slog.NewTextHandler(os.Stderr, nil),
    ))

    // Optional: Use the logger for the top-level slog functions and the log package.
    slog.SetDefault(logger)

    logger.Info("This is awesome!", "mood", "hyped")
    logger.With("mood", "worried").Warn("This is not that awesome...")
    logger.Error("This is rather bad.", slog.String("mood", "depressed"))
}
```

<ReplaceDataset />

To send logs only to Axiom, pass the handler directly: `slog.New(handler)`.

<Note>
  The adapter uses a buffer to batch events before sending them to Axiom. Flush this buffer explicitly by calling [Close](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/slog#Handler.Close) before the app exits. Deferred calls don’t run when the app exits through `os.Exit` or `log.Fatal`, so call `Close` before those functions. For more information, see the [example in GitHub](https://github.com/axiomhq/axiom-go/blob/main/examples/slog/main.go).
</Note>

## Event fields [#event-fields]

The adapter turns each log record into an Axiom event with the following fields:

| Field    | Description                                                                                |
| :------- | :----------------------------------------------------------------------------------------- |
| `_time`  | Time of the log record.                                                                    |
| `level`  | Log level, such as `INFO` or `ERROR`.                                                      |
| `msg`    | Log message.                                                                               |
| `source` | Source file, function, and line of the log call. Only present when you use `SetAddSource`. |

Each attribute becomes a field of the event. Attributes in a group, created with `slog.Group` or `Logger.WithGroup`, become nested objects. Axiom shows their fields with dotted names, such as `req.user.name`. Error values are sent as their error message.

## Configure adapter [#configure-adapter]

Use the following options to change the behavior of the adapter:

* [SetLevel](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/slog#SetLevel) sets the minimum log level the adapter sends to Axiom. The default is `slog.LevelInfo`, so the adapter drops debug logs.
* [SetAddSource](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/slog#SetAddSource) adds the `source` field to each event.
* [SetIngestOptions](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/slog#SetIngestOptions) passes [ingest options](https://pkg.go.dev/github.com/axiomhq/axiom-go/axiom/ingest#Option) to each ingest request. For example, use `ingest.SetEventLabel` to add a field to every event.

```go
import (
    "log/slog"

    "github.com/axiomhq/axiom-go/axiom/ingest"
    adapter "github.com/axiomhq/axiom-go/adapters/slog"
)

// ...

handler, err := adapter.New(
    adapter.SetDataset("DATASET_NAME"),
    adapter.SetLevel(slog.LevelDebug),
    adapter.SetAddSource(),
    adapter.SetIngestOptions(ingest.SetEventLabel("service", "checkout")),
)
```

<ReplaceDataset />

## Configure client [#configure-client]

To configure the underlying client manually, choose one of the following:

* Use [SetClient](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/slog#SetClient) to pass in the client you have previously created with [Send data from Go app to Axiom](/guides/go).
* Use [SetClientOptions](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/slog#SetClientOptions) to pass [client options](https://pkg.go.dev/github.com/axiomhq/axiom-go/axiom#Option) to the adapter.

```go
import (
    "github.com/axiomhq/axiom-go/axiom"
    adapter "github.com/axiomhq/axiom-go/adapters/slog"
)

// ...

handler, err := adapter.New(
    adapter.SetClientOptions(
        axiom.SetAPITokenConfig("API_TOKEN"),
    ),
)
```

<ReplaceToken />

### Configure region [#configure-region]

By default, the adapter sends data to `api.axiom.co`. To target a specific edge region, pass `axiom.SetEdge` through `SetClientOptions` with the edge domain that matches the region your dataset lives in:

```go
handler, err := adapter.New(
    adapter.SetClientOptions(
        axiom.SetAPITokenConfig("API_TOKEN"),
        axiom.SetEdge("AXIOM_DOMAIN"),
    ),
)
```

<ReplaceToken />

<ReplaceDomain />

The following edge domains are available:

<BaseDomains />

<Note>
  Edge ingestion requires an API token (`xaat-`), not a personal token (`xapt-`). Use `axiom.SetAPITokenConfig` when targeting an edge region. You can also set `AXIOM_EDGE` or `AXIOM_EDGE_URL` in the environment instead of `axiom.SetEdge`. For a full edge URL such as a proxy, use `axiom.SetEdgeURL`, which takes precedence over `axiom.SetEdge`.
</Note>

## Reference [#reference]

For a full reference of the adapter’s functions, see the [Go Packages page](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/slog).
