stdemuxerhook

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2019 License: MIT Imports: 4 Imported by: 0

README

A stdout/stderr demuxer hook for Logrus

Logrus loggers always output all log levels to a single common output, e.g. stderr. This logrus hook makes it so that logs with a severity below Error are written to stdout while all the important stuff goes to stderr.

You can also use the hook to demux logs to custom IO writers based on severity. Just override the default outputs using the hook's SetOutput(infoLevel, errorLevel io.Writer) method.

Example

Given you have an application that uses a the logrus standard logger similar to this:

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

func main() {

    log.SetLevel(log.InfoLevel)

    log.Info("A group of penguins emerges from the ocean")

    (...)

The only change required is adding in the hook. Make sure to configure the parent logger before initializing the hook as the latter will inherit it's configuration.

import (
    log "github.com/Sirupsen/logrus"
    "github.com/janeczku/stdemuxerhook"
)

func main() {

    log.SetLevel(log.InfoLevel)
    log.AddHook(stdemuxerhook.New(log.StandardLogger()))

    log.Info("A group of penguins emerges from the ocean") // -> logged to stdout
    log.Panic("A group of polar bears emerges from the ocean") // -> logged to stderr

    (...)

}

Benchmarks

BenchmarkLoggerWithoutHook        300000          4348 ns/op
BenchmarkLoggerWithHook           200000          5436 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NopFormatter

type NopFormatter struct{}

NopFormatter always yields zero bytes and consumes 0 allocs/op.

func (*NopFormatter) Format

func (*NopFormatter) Format(entry *logrus.Entry) ([]byte, error)

type StdDemuxerHook

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

StdDemuxerHook demuxes logs to io.Writers based on severity. By default it uses the following outputs: error and higher -> os.Stderr warning and lower -> os.Stdout

func New

func New(parent *logrus.Logger) *StdDemuxerHook

New returns a new StdDemuxerHook by silencing the parent logger and configuring separate loggers for stderr and stdout with the parents loggers properties.

func (*StdDemuxerHook) Fire

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

Fire is triggered on new log entries

func (*StdDemuxerHook) Levels

func (*StdDemuxerHook) Levels() []logrus.Level

Levels returns all levels this hook should be registered to

func (*StdDemuxerHook) SetOutput

func (hook *StdDemuxerHook) SetOutput(infoLevel io.Writer, errorLevel io.Writer)

SetOutput allows to set the info and error level outputs to arbitrary io.Writers

Jump to

Keyboard shortcuts

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