Axiom Go adapter for zerolog
This page explains how to send logs generated by the rs/zerolog library to Axiom.
Use the adapter of the Axiom Go SDK to send logs generated by the 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.
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 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 -
In your Go app, import the
zerologadapter package. It’s imported asadapterso that it doesn’t conflict with thers/zerologpackage.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 ErrMissingDataset. 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 writer, 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 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.
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
Use the following options to change the behavior of the adapter:
- SetLevels sets the log levels the adapter sends to Axiom. By default, the adapter sends
info,warn,error,fatal, andpaniclogs and dropstraceanddebuglogs. - SetIngestOptions passes ingest options to each ingest request.
- SetMaxBufferCapacity sets the buffer capacity in bytes above which the adapter releases the buffer after a flush. The default is 1 MB.
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.