# Axiom Go adapter for zerolog





Use the adapter of the Axiom Go SDK to send logs generated by the [rs/zerolog](https://github.com/rs/zerolog) library to Axiom. The adapter is an `io.Writer`, so you can pass it to `zerolog.New` or combine it with other writers.

<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 and zerolog to your Go module. The adapter has dependencies that the SDK installation doesn’t add, so your app doesn’t build without this step.

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

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

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

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 `ErrMissingDataset`. Alternatively, configure the adapter using [options](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/zerolog#Option) passed to the [New](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/zerolog#New) function:

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

<ReplaceDataset />

## Send logs [#send-logs]

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

```go
package main

import (
    "io"
    "log"
    "os"

    "github.com/rs/zerolog"

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

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

    // Send logs to Axiom and print them to stderr. Timestamp() adds the time of each event.
    logger := zerolog.New(io.MultiWriter(writer, os.Stderr)).With().Timestamp().Logger()

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

<ReplaceDataset />

To send logs only to Axiom, pass the writer directly: `zerolog.New(writer)`. To use the adapter with the global logger of the `github.com/rs/zerolog/log` package, assign the logger to `log.Logger`.

Add `Timestamp()` to the logger context as in the example above. Axiom uses the `time` field of each log as the event time and stores it in `_time`. Without it, Axiom uses the time it receives the batch.

<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/zerolog#Writer.Close) before the app exits. Deferred calls don’t run when the app exits through `os.Exit` or a `Fatal` log, so call `Close` before those functions. For more information, see the [example in GitHub](https://github.com/axiomhq/axiom-go/blob/main/examples/zerolog/main.go).
</Note>

## Event fields [#event-fields]

The adapter sends the JSON object of each zerolog log as an Axiom event and adds the field `logger` with the value `zerolog`. With the default zerolog field names, each event has the following fields:

| Field     | Description                                                                                                                                                                                         |
| :-------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `_time`   | Time of the log from the zerolog `time` field. Axiom stores the `time` value in `_time` and doesn’t keep a separate `time` field. Without `Timestamp()`, this is the time Axiom receives the batch. |
| `level`   | Log level, such as `info` or `error`.                                                                                                                                                               |
| `message` | Log message.                                                                                                                                                                                        |
| `logger`  | Always `zerolog`.                                                                                                                                                                                   |

The fields you add to a log, such as with `Str`, become fields of the event.

## Configure adapter [#configure-adapter]

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

* [SetLevels](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/zerolog#SetLevels) sets the log levels the adapter sends to Axiom. By default, the adapter sends `info`, `warn`, `error`, `fatal`, and `panic` logs and drops `trace` and `debug` logs.
* [SetIngestOptions](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/zerolog#SetIngestOptions) passes [ingest options](https://pkg.go.dev/github.com/axiomhq/axiom-go/axiom/ingest#Option) to each ingest request.
* [SetMaxBufferCapacity](https://pkg.go.dev/github.com/axiomhq/axiom-go/adapters/zerolog#SetMaxBufferCapacity) sets the buffer capacity in bytes above which the adapter releases the buffer after a flush. The default is 1 MB.

```go
import (
    "github.com/rs/zerolog"

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

// ...

writer, err := adapter.New(
    adapter.SetDataset("DATASET_NAME"),
    adapter.SetLevels([]zerolog.Level{
        zerolog.DebugLevel,
        zerolog.InfoLevel,
        zerolog.WarnLevel,
        zerolog.ErrorLevel,
        zerolog.FatalLevel,
        zerolog.PanicLevel,
    }),
)
```

<ReplaceDataset />

<Warning>
  By default, the adapter sets ingest options that tell Axiom to read the event time from the zerolog timestamp field. `SetIngestOptions` replaces these defaults. If you use `SetIngestOptions`, include `ingest.SetTimestampField(zerolog.TimestampFieldName)` and `ingest.SetTimestampFormat(zerolog.TimeFieldFormat)` to keep the event time.

  ```go
  import (
      "github.com/rs/zerolog"

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

  // ...

  writer, err := adapter.New(
      adapter.SetDataset("DATASET_NAME"),
      adapter.SetIngestOptions([]ingest.Option{
          ingest.SetTimestampField(zerolog.TimestampFieldName),
          ingest.SetTimestampFormat(zerolog.TimeFieldFormat),
          ingest.SetEventLabel("service", "checkout"),
      }),
  )
  ```
</Warning>

## 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/zerolog#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/zerolog#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/zerolog"
)

// ...

writer, 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
writer, 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/zerolog).
