logrus

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: MIT Imports: 9 Imported by: 1

README

Axiom Go Adapter for sirupsen/logrus

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

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

hook, err := adapter.New(
    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 adapter "github.com/axiomhq/axiom-go/axiom"

// ...

hook, err := adapter.New(
    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 out the example.

Documentation

Overview

Package logrus provides an adapter for the popular github.com/sirupsen/logrus logging library.

Example
package main

import (
	"log"

	"github.com/sirupsen/logrus"

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

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

	hook, err := adapter.New()
	if err != nil {
		log.Fatal(err)
	}
	logrus.RegisterExitHandler(hook.Close)

	logger := logrus.New()
	logger.AddHook(hook)

	logger.WithField("mood", "hyped").Info("This is awesome!")
	logger.WithField("mood", "worried").Warn("This is no that awesome...")
	logger.WithField("mood", "depressed").Error("This is rather bad.")

	logrus.Exit(0)
}
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 Hook

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

Hook implements a logrus.Hook used for shipping logs to Axiom.

func New

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

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

func (*Hook) Close

func (h *Hook) Close()

Close the hook and make sure all events are flushed. This should be registered with logrus.RegisterExitHandler. Closing the hook renders it unusable for further use.

func (*Hook) Fire

func (h *Hook) Fire(entry *logrus.Entry) error

Fire implements logrus.Hook.

func (*Hook) Levels

func (h *Hook) Levels() []logrus.Level

Levels implements logrus.Hook.

type Option

type Option func(*Hook) error

An Option modifies the behaviour of the Axiom hook.

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 SetLevels added in v0.4.0

func SetLevels(levels ...logrus.Level) Option

SetLevels sets the logrus levels that the Axiom Hook will create log entries for.

Jump to

Keyboard shortcuts

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