logri

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

README

Logri

Logri is a wrapper for Logrus that provides hierarchical, configurable, structured logging.

Like Logrus, it's a drop-in replacement for Go's standard logging library, but it adds the ability to:

  • Define loggers that inherit their log levels and output streams from parent loggers
  • Configure loggers from a YAML file
  • Update configuration on the fly
  • Optionally watch a logging configuration file for changes

And, of course, it retains Logrus's excellent structured logging capabilities.

Usage

You can drop Logri in to replace logging or Logrus very simply:

package main


import (
        "github.com/Sirupsen/logrus"
    log "github.com/zenoss/logri"
)

func main() {

    log.Infof("Logri can replace the %s package", "logging")

    log.WithFields(logrus.Fields{
        "package": "logrus",
    }).Infof("Or another popular logging package")
}
Named loggers

The power of Logri comes in with named hierarchical loggers. Use logri.GetLogger(name) with a dotted name to return an individual logger that inherits its log level and outputs from its parent, but can add or override its own.

package main

import (
    "github.com/Sirupsen/logrus"
    "github.com/zenoss/logri"
)

var (
    pkglog = logri.GetLogger("package")
    cmplog = logri.GetLogger("package.component")
    subcmplog = logri.GetLogger("package.component.subcomponent")
)

func main() {

    pkglog.SetLevel(logrus.DebugLevel, true) // Second argument makes it inherited
    // package.component and package.component.subcomponent are also Debug level now

    // Quiet package.component down but leave subcomponent at debug
    cmplog.SetLevel(logrus.ErrorLevel, false) // Second argument false means
                                              // local to this logger only
}

Further calls to logri.GetLogger(name) will retrieve the same logger instance, so there's no need to jump through hoops exporting loggers to share them among packages.

Configuration via file

You can also configure Logri using a YAML file. Given a file /etc/logging.conf with these contents:

- logger: '*'
  level: debug
  out:
  - type: stderr
  - type: file
    options:
      file: /var/log/app.log
- logger: package
  level: warn
  local: true
  out:
  - type: file
    local: true
    options:
      file: /var/log/package.warn.log
- logger: package.component
  level: error
  out:
  - type: file
    options:
      file: /var/log/package.component.error.log

You can configure the loggers defined above very simply:

logri.ApplyConfigFromFile("/etc/logging.conf")

That can be called at any time, and it will reconfigure loggers to match the config at that time, with no need to restart or recreate loggers.

You can also watch that file for changes, rather than listening for a signal to reload logging config:

package main

import (
    "github.com/zenoss/logri"
)

func main() {

    // Start watching the logging config for changes
    go logri.WatchConfigFile("/etc/logging.conf")

    doStuff()
}

Documentation

Index

Constants

View Source
const (
	FileOutput   OutputType = "file"
	StdoutOutput            = "stdout"
	StderrOutput            = "stderr"
	TestOutput              = "test" // Used for tests only
)

Variables

View Source
var (
	ConfigurationError = errors.New("Unable to parse configuration")
)
View Source
var (
	ErrInvalidOutputOptions = errors.New("Insufficient or invalid options were given for an output")
)
View Source
var (
	// ErrInvalidRootLevel is returned when a nil level is set on the root logger
	ErrInvalidRootLevel = errors.New("The root logger must have a level")
)
View Source
var (
	// RootLogger is the default logger tree.
	RootLogger = NewLoggerFromLogrus(logrus.New())
)

Functions

func AddHook

func AddHook(hook logrus.Hook)

Adds the given hook to every logger in the tree

func ApplyConfig

func ApplyConfig(config LogriConfig) error

ApplyConfig applies configuration to the default tree.

func ApplyConfigFromFile

func ApplyConfigFromFile(file string) error

ApplyConfigFromFile reads logging configuration from a file and applies it to the default tree.

func Debug

func Debug(args ...interface{})

Debug logs a message at level Debug on the standard logger.

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a message at level Debug on the standard logger.

func Debugln

func Debugln(args ...interface{})

Debugln logs a message at level Debug on the standard logger.

func Error

func Error(args ...interface{})

Error logs a message at level Error on the standard logger.

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a message at level Error on the standard logger.

func Errorln

func Errorln(args ...interface{})

Errorln logs a message at level Error on the standard logger.

func Fatal

func Fatal(args ...interface{})

Fatal logs a message at level Fatal on the standard logger.

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs a message at level Fatal on the standard logger.

func Fatalln

func Fatalln(args ...interface{})

Fatalln logs a message at level Fatal on the standard logger.

func GetOutputWriter

func GetOutputWriter(outtype OutputType, options map[string]string) (io.Writer, error)

func Info

func Info(args ...interface{})

Info logs a message at level Info on the standard logger.

func Infof

func Infof(format string, args ...interface{})

Infof logs a message at level Info on the standard logger.

func Infoln

func Infoln(args ...interface{})

Infoln logs a message at level Info on the standard logger.

func Panic

func Panic(args ...interface{})

Panic logs a message at level Panic on the standard logger.

func Panicf

func Panicf(format string, args ...interface{})

Panicf logs a message at level Panic on the standard logger.

func Panicln

func Panicln(args ...interface{})

Panicln logs a message at level Panic on the standard logger.

func Print

func Print(args ...interface{})

Print logs a message at level Info on the standard logger.

func Printf

func Printf(format string, args ...interface{})

Printf logs a message at level Info on the standard logger.

func Println

func Println(args ...interface{})

Println logs a message at level Info on the standard logger.

func SetLevel

func SetLevel(level logrus.Level)

SetLevel sets the level of the root logger and its descendants

func Warn

func Warn(args ...interface{})

Warn logs a message at level Warn on the standard logger.

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs a message at level Warn on the standard logger.

func Warning

func Warning(args ...interface{})

Warning logs a message at level Warn on the standard logger.

func Warningf

func Warningf(format string, args ...interface{})

Warningf logs a message at level Warn on the standard logger.

func Warningln

func Warningln(args ...interface{})

Warningln logs a message at level Warn on the standard logger.

func Warnln

func Warnln(args ...interface{})

Warnln logs a message at level Warn on the standard logger.

func WatchConfigFile

func WatchConfigFile(file string) error

WatchConfigFile watches a given config file, applying the config on change

func WithError

func WithError(err error) *logrus.Entry

WithError creates an entry from the root logger and adds an error to it, using the value defined in ErrorKey as key.

func WithField

func WithField(key string, value interface{}) *logrus.Entry

WithField creates an entry from the root logger and adds a field to it. If you want multiple fields, use `WithFields`.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithFields

func WithFields(fields logrus.Fields) *logrus.Entry

WithFields creates an entry from the root logger and adds multiple fields to it. This is simply a helper for `WithField`, invoking it once for each field.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

Types

type Logger

type Logger struct {
	Name string
	// contains filtered or unexported fields
}

Logger is the wrapper of a Logrus logger. It holds references to its child loggers, and manages transactional application of new levels and output streams.

func GetLogger

func GetLogger(name string) *Logger

GetLogger returns a logger from the default tree with the given name.

func NewLoggerFromLogrus

func NewLoggerFromLogrus(base *logrus.Logger) *Logger

NewLoggerFromLogrus creates a new Logri logger tree rooted at a given Logrus logger.

func (*Logger) AddHook

func (l *Logger) AddHook(hook logrus.Hook)

AddHook adds a hook to this logger and all its children

func (*Logger) ApplyConfig

func (l *Logger) ApplyConfig(config LogriConfig) error

ApplyConfig applies a Logrus config to a logger tree. Regardless of the logger within the tree to which the config is applied, it is treated as the root of the tree for purposes of configuring loggers.

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...interface{})

func (*Logger) Debugln

func (l *Logger) Debugln(args ...interface{})

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...interface{})

func (*Logger) Errorln

func (l *Logger) Errorln(args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, args ...interface{})

func (*Logger) Fatalln

func (l *Logger) Fatalln(args ...interface{})

func (*Logger) GetChild

func (l *Logger) GetChild(name string) *Logger

GetChild returns a logger that is a child of this logger, creating intervening loggers if they do not exist. If the name given starts with the full name of the current logger (i.e., is "absolute"), then the logger returned will take that into account, rather than creating a duplicate tree below this one.

Example:

 logger := logri.Logger("a.b.c") // logger.name == "a.b.c"
	l := logger.GetChild("a.b.c.d") // l.name == "a.b.c.d"
	l = logger.GetChild("d") // l.name == "a.b.c.d"
	l = logger.GetChild("b.c.d") // l.name == "a.b.c.b.c.d"

func (*Logger) GetEffectiveLevel

func (l *Logger) GetEffectiveLevel() logrus.Level

GetEffectiveLevel returns the effective level of this logger. If this logger has no level set locally, it returns the level of its closest ancestor with an inheritable level.

func (*Logger) GetRoot

func (l *Logger) GetRoot() *Logger

GetRoot returns the logger at the root of this logger's tree.

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...interface{})

func (*Logger) Infoln

func (l *Logger) Infoln(args ...interface{})

func (*Logger) Panic

func (l *Logger) Panic(args ...interface{})

func (*Logger) Panicf

func (l *Logger) Panicf(format string, args ...interface{})

func (*Logger) Panicln

func (l *Logger) Panicln(args ...interface{})

func (*Logger) Print

func (l *Logger) Print(args ...interface{})

func (*Logger) Printf

func (l *Logger) Printf(format string, args ...interface{})

func (*Logger) Println

func (l *Logger) Println(args ...interface{})

func (*Logger) SetLevel

func (l *Logger) SetLevel(level logrus.Level, inherit bool) error

SetLevel sets the logging level for this logger and children inheriting their level from this logger. If inherit is false, the level will be set locally only.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput sets the output to which this logger should write.

func (*Logger) SetOutputs

func (l *Logger) SetOutputs(writers ...io.Writer)

SetOutputs combines several output writers into one and configures this logger to write to that.

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...interface{})

func (*Logger) Warning

func (l *Logger) Warning(args ...interface{})

func (*Logger) Warningf

func (l *Logger) Warningf(format string, args ...interface{})

func (*Logger) Warningln

func (l *Logger) Warningln(args ...interface{})

func (*Logger) Warnln

func (l *Logger) Warnln(args ...interface{})

func (*Logger) WithError

func (l *Logger) WithError(err error) *logrus.Entry

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) *logrus.Entry

func (*Logger) WithFields

func (l *Logger) WithFields(fields logrus.Fields) *logrus.Entry

type LoggerConfig

type LoggerConfig struct {
	Logger string
	Level  string
	Local  bool
	Out    []OutConfig
}

LoggerConfig is the configuration for a single logger

type LoggerHook

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

LoggerHook is a Logrus hook that adds the logger name as a field to the entry

func (LoggerHook) Fire

func (hook LoggerHook) Fire(entry *logrus.Entry) error

Fire satisfies the logrus.Hook interface

func (LoggerHook) Levels

func (hook LoggerHook) Levels() []logrus.Level

Levels satisfies the logrus.Hook interface

type LogriConfig

type LogriConfig []LoggerConfig

LogriConfig is the configuration for a logri manager

func ConfigFromBytes

func ConfigFromBytes(b []byte) (LogriConfig, error)

func ConfigFromYAML

func ConfigFromYAML(r io.Reader) (LogriConfig, error)

func (LogriConfig) Len

func (c LogriConfig) Len() int

func (LogriConfig) Less

func (c LogriConfig) Less(i, j int) bool

Sort loggers by depth in the hierarchy

func (LogriConfig) Swap

func (c LogriConfig) Swap(i, j int)

type OutConfig

type OutConfig struct {
	Type    OutputType
	Options map[string]string
	Local   bool
}

type OutputType

type OutputType string

Jump to

Keyboard shortcuts

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