Axiom Go adapter for slog
This page explains how to send logs generated by the Go standard library log/slog package to Axiom.
Use the adapter of the Axiom Go SDK to send logs generated by the log/slog package of the Go standard library to Axiom. The adapter is a slog.Handler, so you can use it with slog.New like any other handler.
Prerequisites
- Create an Axiom account.
- Create a dataset in Axiom where you send your data.
- Create an API token in Axiom with permissions to ingest data to the dataset you have created.
- Go 1.26 or later. This is the minimum Go version of the Axiom Go SDK.
Set up SDK
-
Install the Axiom Go SDK and configure your environment as explained in Send data from Go app to Axiom.
-
Add the adapter package to your Go module:
shell -
In your Go app, import the
slogadapter package. It’s imported asadapterso that it doesn’t conflict with the standard librarylog/slogpackage.go
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 passed to the New function:
Replace DATASET_NAME with the name of the Axiom dataset where you send your data.
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:
Replace DATASET_NAME with the name of the Axiom dataset where you send your data.
To send logs only to Axiom, pass the handler directly: slog.New(handler).
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
Use the following options to change the behavior of the adapter:
- SetLevel sets the minimum log level the adapter sends to Axiom. The default is
slog.LevelInfo, so the adapter drops debug logs. - SetAddSource adds the
sourcefield to each event. - SetIngestOptions passes ingest options to each ingest request. For example, use
ingest.SetEventLabelto add a field to every event.
Replace DATASET_NAME with the name of the Axiom dataset where you send your data.
Configure client
To configure the underlying client manually, choose one of the following:
- Use SetClient to pass in the client you have previously created with Send data from Go app to Axiom.
- Use SetClientOptions to pass client options to the adapter.
Replace API_TOKEN with the Axiom API token you have generated. For added security, store the API token in an environment variable.
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:
Replace API_TOKEN with the Axiom API token you have generated. For added security, store the API token in an environment variable.
Replace AXIOM_DOMAIN with the base domain of your edge deployment. For more information, see Edge deployments.
The following edge domains are available:
| Edge deployment | Base domain for ingest and query |
|---|---|
US East 1 (AWS) | us-east-1.aws.edge.axiom.co |
EU Central 1 (AWS) | eu-central-1.aws.edge.axiom.co |
Reference
For a full reference of the adapter’s functions, see the Go Packages page.