| 1 | # Writer Hooks for Logrus |
| 2 | |
| 3 | Send logs of given levels to any object with `io.Writer` interface. |
| 4 | |
| 5 | ## Usage |
| 6 | |
| 7 | If you want for example send high level logs to `Stderr` and |
| 8 | logs of normal execution to `Stdout`, you could do it like this: |
| 9 | |
| 10 | ```go |
| 11 | package main |
| 12 | |
| 13 | import ( |
| 14 | "io/ioutil" |
| 15 | "os" |
| 16 | |
| 17 | log "github.com/sirupsen/logrus" |
| 18 | "github.com/sirupsen/logrus/hooks/writer" |
| 19 | ) |
| 20 | |
| 21 | func main() { |
| 22 | log.SetOutput(ioutil.Discard) // Send all logs to nowhere by default |
| 23 | |
| 24 | log.AddHook(&writer.Hook{ // Send logs with level higher than warning to stderr |
| 25 | Writer: os.Stderr, |
| 26 | LogLevels: []log.Level{ |
| 27 | log.PanicLevel, |
| 28 | log.FatalLevel, |
| 29 | log.ErrorLevel, |
| 30 | log.WarnLevel, |
| 31 | }, |
| 32 | }) |
| 33 | log.AddHook(&writer.Hook{ // Send info and debug logs to stdout |
| 34 | Writer: os.Stdout, |
| 35 | LogLevels: []log.Level{ |
| 36 | log.InfoLevel, |
| 37 | log.DebugLevel, |
| 38 | }, |
| 39 | }) |
| 40 | log.Info("This will go to stdout") |
| 41 | log.Warn("This will go to stderr") |
| 42 | } |
| 43 | ``` |