logrus/hooks/writer
re 22f067d83b fix repos 2022-12-21 15:59:06 +03:00
..
README.md fix repos 2022-12-21 15:59:06 +03:00
writer.go fix repos 2022-12-21 15:59:06 +03:00
writer_test.go fix repos 2022-12-21 15:59:06 +03:00

README.md

Writer Hooks for Logrus

Send logs of given levels to any object with io.Writer interface.

Usage

If you want for example send high level logs to Stderr and logs of normal execution to Stdout, you could do it like this:

package main

import (
	"io/ioutil"
	"os"

	log "git.internal/re/logrus"
	"git.internal/re/logrus/hooks/writer"
)

func main() {
	log.SetOutput(ioutil.Discard) // Send all logs to nowhere by default

	log.AddHook(&writer.Hook{ // Send logs with level higher than warning to stderr
		Writer: os.Stderr,
		LogLevels: []log.Level{
			log.PanicLevel,
			log.FatalLevel,
			log.ErrorLevel,
			log.WarnLevel,
		},
	})
	log.AddHook(&writer.Hook{ // Send info and debug logs to stdout
		Writer: os.Stdout,
		LogLevels: []log.Level{
			log.InfoLevel,
			log.DebugLevel,
		},
	})
	log.Info("This will go to stdout")
	log.Warn("This will go to stderr")
}