zerolog

package
v0.17.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 12 Imported by: 0

README

Axiom Go Adapter for rs/zerolog

Adapter to ship logs generated by rs/zerolog to Axiom.

Quickstart

Follow the Axiom Go Quickstart to install the Axiom Go package and configure your environment.

Import the package:

// Imported as "adapter" to not conflict with the "rs/zerolog" package.
import adapter "github.com/axiomhq/axiom-go/adapters/zerolog"

You can also configure the adapter using options passed to the New function:

writer, err := adapter.New(
    WithDatasetName("logs"),
)
l.Logger = zerolog.New(io.MultiWriter(writer, os.Stderr)).With().Str("env", os.Getenv("ENV")).Timestamp().Logger()

To configure the underlying client manually either pass in a client that was created according to the Axiom Go Quickstart using SetClient or pass client options to the adapter using SetClientOptions.

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

// ...
writer, err := adapter.New()
if err != nil {
    log.Fatal(err)
}
l.Logger = zerolog.New(io.MultiWriter(writer, os.Stderr)).With().Str("env", os.Getenv("ENV")).Timestamp().Logger()

[!IMPORTANT] The adapter uses a buffer to batch events before sending them to Axiom. This buffer can be flushed explicitly by calling Close, and is necessary when terminating the program so as not to lose logs. If With().Timestamp() isn't passed, then the timestamp will be the batched timestamp on the server Checkout out the example.

Documentation

Overview

Package zerolog provides an adapter for the popular github.com/rs/zerolog logging library.

Example
package main

import (
	"io"
	"log"
	"os"

	"github.com/rs/zerolog"
	l "github.com/rs/zerolog/log"

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

func main() {
	// Export "AXIOM_DATASET" in addition to the required environment variables.

	writer, err := adapter.New()
	if err != nil {
		log.Fatal(err)
	}

	l.Logger = zerolog.New(io.MultiWriter(writer, os.Stderr)).With().Timestamp().Logger()

	l.Logger.Info().Str("mood", "hyped").Msg("This is awesome!")
	l.Logger.Warn().Str("mood", "worried").Msg("This is no that awesome...")
	l.Logger.Error().Str("mood", "depressed").Msg("This is rather bad.")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// ErrMissingDataset is raised when a dataset name is not provided. Set it
	// manually using the [SetDataset] option or export "AXIOM_DATASET".
	ErrMissingDataset = errors.New("missing dataset name")
)

Functions

This section is empty.

Types

type Option

type Option func(*Writer)

Option configures axiom events writer.

func SetClient

func SetClient(client *axiom.Client) Option

SetClient configures a custom axiom client.

func SetClientOptions

func SetClientOptions(clientOptions []axiom.Option) Option

SetClientOptions configures the axiom client options.

func SetDataset

func SetDataset(dataset string) Option

SetDataset configures the axiom dataset name.

func SetIngestOptions

func SetIngestOptions(ingestOptions []ingest.Option) Option

SetIngestOptions configures the axiom ingest options.

func SetLevels

func SetLevels(levels []zerolog.Level) Option

SetLevels configures zerolog levels that have to be sent to Axiom.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is a axiom events writer with std io.Writer interface.

func New

func New(opts ...Option) (*Writer, error)

New creates a new Writer that ingests logs into Axiom. It automatically takes its configuration from the environment. To connect, export the following environment variables:

  • AXIOM_TOKEN
  • AXIOM_ORG_ID (only when using a personal token)
  • AXIOM_DATASET

The configuration can be set manually using options which are prefixed with "Set".

An API token with "ingest" permission is sufficient enough.

A Writer needs to be closed properly to make sure all logs are sent by calling Writer.Close.

func (*Writer) Close

func (w *Writer) Close()

func (*Writer) Write

func (w *Writer) Write(data []byte) (int, error)

Write must not modify the slice data, even temporarily.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL