zap

package
v0.17.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: MIT Imports: 11 Imported by: 5

README

Axiom Go Adapter for uber-go/zap

Adapter to ship logs generated by uber-go/zap 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 "uber-go/zap" package.
import adapter "github.com/axiomhq/axiom-go/adapters/zap"

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

core, err := adapter.New(
    adapter.SetDataset("AXIOM_DATASET"),
)

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/zap"
)

// ...

core, err := adapter.New(
    adapter.SetClientOptions(
        axiom.SetPersonalTokenConfig("AXIOM_TOKEN", "AXIOM_ORG_ID"),
    ),
)

[!IMPORTANT] The adapter uses a buffer to batch events before sending them to Axiom. This buffer must be flushed explicitly by calling Sync. Refer to the zap documentation for details and checkout out the example.

Documentation

Overview

Package zap provides an adapter for the popular github.com/uber-go/zap logging library.

Example
package main

import (
	"log"

	"go.uber.org/zap"

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

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

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

	logger := zap.New(core)
	defer func() {
		if syncErr := logger.Sync(); syncErr != nil {
			log.Fatal(syncErr)
		}
	}()

	logger.Info("This is awesome!", zap.String("mood", "hyped"))
	logger.Warn("This is no that awesome...", zap.String("mood", "worried"))
	logger.Error("This is rather bad.", zap.String("mood", "depressed"))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrMissingDatasetName = errors.New("missing dataset name")

ErrMissingDatasetName is raised when a dataset name is not provided. Set it manually using the SetDataset option or export "AXIOM_DATASET".

Functions

func New

func New(options ...Option) (zapcore.Core, error)

New creates a new zapcore.Core 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.

Types

type Option

type Option func(*WriteSyncer) error

An Option modifies the behaviour of the Axiom WriteSyncer.

func SetClient added in v0.4.0

func SetClient(client *axiom.Client) Option

SetClient specifies the Axiom client to use for ingesting the logs.

func SetClientOptions added in v0.4.0

func SetClientOptions(options ...axiom.Option) Option

SetClientOptions specifies the Axiom client options to pass to axiom.NewClient which is only called if no axiom.Client was specified by the SetClient option.

func SetDataset added in v0.4.0

func SetDataset(datasetName string) Option

SetDataset specifies the dataset to ingest the logs into. Can also be specified using the "AXIOM_DATASET" environment variable.

func SetIngestOptions added in v0.4.0

func SetIngestOptions(opts ...ingest.Option) Option

SetIngestOptions specifies the ingestion options to use for ingesting the logs.

func SetLevelEnabler added in v0.4.0

func SetLevelEnabler(levelEnabler zapcore.LevelEnabler) Option

SetLevelEnabler sets the level enabler that the Axiom WriteSyncer will us to determine if logs will be shipped to Axiom.

type WriteSyncer

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

WriteSyncer implements a zapcore.WriteSyncer used for shipping logs to Axiom.

func (*WriteSyncer) Sync

func (ws *WriteSyncer) Sync() error

Sync implements zapcore.WriteSyncer.

func (*WriteSyncer) Write

func (ws *WriteSyncer) Write(p []byte) (n int, err error)

Write implements zapcore.WriteSyncer.

Jump to

Keyboard shortcuts

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