logging

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2019 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package logging provides a configuration model for logrus.

It introduces hierarchical configuration and hot-reloading of configuration for logrus through viper This means that you can use YAML, TOML, HCL and JSON to configure your loggers, or use env vars. It allows for storing configuration in etcd or consul.

You can configure multiple root level loggers which serve as default logger for that tree. In addition to being the default logger, each child logger created will inherit the configuration from its parents. So you only have to define overrides for things you want to customize.

logging:
  root:
    level: debug
    hooks:
      - name: journald
    writer: stderr
    child1:
      level: info
      hooks:
        - name: file
          path: ./app.log
        - name: syslog
          network: udp
          host: localhost:514
          priority: info
  alerts:
    level: error
    writer: stderr

In this example config there are 2 root loggers defined: root and alerts. Both root loggers use a stderr writer, other writers are stdout and discard.

The logger named root has also one child node that overrides the level property. The hooks property is not an override but instead combines the defined hooks. So the hooks work additive whereas everything else are overrides.

Custom writers:

You can register your own named writers with the RegisterWriter function.

Custom Formatters:

You can register your own formatters with the RegisterFormatter function.

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultWriter is used as fallback when no other writer can be found.
	// This defaults to writing to stderr, but you can replace it to do something more useful
	DefaultWriter io.Writer
)
View Source
var (
	// RootName of the root logger, defaults to root
	RootName string
)

Functions

func KnownFormatters

func KnownFormatters() []string

KnownFormatters returns a list of keys for the currently registered formatters

func KnownHooks

func KnownHooks() []string

KnownHooks returns the list of keys for the registered hooks

func KnownWriters

func KnownWriters() []string

KnownWriters returns the list of keys for the registered writers

func RegisterFormatter

func RegisterFormatter(name string, factory CreateFormatter)

RegisterFormatter registers a formatter for use in config files

func RegisterHook

func RegisterHook(name string, factory CreateHook)

RegisterHook for use through configuration system

func RegisterWriter

func RegisterWriter(name string, factory CreateWriter)

RegisterWriter for use through the configuration system When you register a writer with a name that was already present then that writer will get overwritten

Types

type CreateFormatter

type CreateFormatter func(*viper.Viper) logrus.Formatter

CreateFormatter is a factory for creating formatters configured through viper

var (

	// DefaultFormatter the fallback formatter when no registered one matches
	DefaultFormatter CreateFormatter
)

type CreateHook

type CreateHook func(*viper.Viper) logrus.Hook

CreateHook creates a hook based on a viper config

type CreateWriter

type CreateWriter func(*viper.Viper) io.Writer

CreateWriter based on a viper config

type Logger

type Logger interface {
	logrus.FieldLogger
	New(string, logrus.Fields) Logger
	Configure(v *viper.Viper)
	Fields() logrus.Fields
}

Logger is the interface that application use to log against. Ideally you use the logrus.FieldLogger or logrus.StdLogger interfaces in your own code.

type Registry

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

LoggerRegistry represents a registry for known loggers

func NewRegistry

func NewRegistry(cfg *viper.Viper, context logrus.Fields) *Registry

NewRegistry creates a new logger registry

func (*Registry) Get

func (r *Registry) Get(name string) Logger

Get a logger by name, returns nil when logger doesn't exist. GetOK is the safe method to use.

func (*Registry) GetOK

func (r *Registry) GetOK(name string) (Logger, bool)

GetOK a logger by name, boolean is true when a logger was found

func (*Registry) Register

func (r *Registry) Register(path string, logger Logger)

Register a logger in this registry, overrides existing keys

func (*Registry) Reload

func (r *Registry) Reload()

Reload all the loggers with the new config

func (*Registry) Root

func (r *Registry) Root() Logger

Root returns the root logger, the name is configurable through the RootName variable

func (*Registry) Writer

func (r *Registry) Writer() *io.PipeWriter

Writer returns the pipe writer for the root logger

Directories

Path Synopsis
hooks

Jump to

Keyboard shortcuts

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