slog

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2024 License: MIT Imports: 10 Imported by: 2

README

Axiom Go Adapter for log/slog

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

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

handler, 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/slog"
)

// ...

handler, 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 Close. Checkout the example.

Documentation

Overview

Package slog provides an adapter for the standard libraries structured logging package.

Example
package main

import (
	"log"
	"log/slog"

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

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

	handler, err := adapter.New()
	if err != nil {
		log.Fatal(err.Error())
	}
	defer handler.Close()

	logger := slog.New(handler)

	logger.Info("This is awesome!", "mood", "hyped")
	logger.With("mood", "worried").Warn("This is no that awesome...")
	logger.Error("This is rather bad.", slog.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

This section is empty.

Types

type Handler

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

Handler implements a slog.Handler used for shipping logs to Axiom.

func New

func New(options ...Option) (*Handler, error)

New creates a new handler 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 handler needs to be closed properly to make sure all logs are sent by calling Handler.Close.

func (*Handler) Close

func (h *Handler) Close()

Close the handler and make sure all events are flushed. Closing the handler renders it unusable for further use.

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled implements slog.Handler.

func (*Handler) Handle

func (h *Handler) Handle(_ context.Context, r slog.Record) error

Handle implements slog.Handler.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler.

type Option

type Option func(*Handler) error

An Option modifies the behaviour of the Axiom handler.

func SetClient

func SetClient(client *axiom.Client) Option

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

func SetClientOptions

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

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

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

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

func SetLevel

func SetLevel(level slog.Leveler) Option

SetLevel specifies the log level the handler is enabled for.

Jump to

Keyboard shortcuts

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