Add hook to send logs to custom writer #678

This commit is contained in:
tbunyk
2019-03-15 13:09:11 +02:00
parent dae0fa8d5b
commit d8e3add56f
3 changed files with 109 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
package writer
import (
"io"
log "github.com/sirupsen/logrus"
)
// Hook is a hook that writes logs of specified LogLevels to specified Writer
type Hook struct {
Writer io.Writer
LogLevels []log.Level
}
// Fire will be called when some logging function is called with current hook
// It will format log entry to string and write it to appropriate writer
func (hook *Hook) Fire(entry *log.Entry) error {
line, err := entry.String()
if err != nil {
return err
}
_, err = hook.Writer.Write([]byte(line))
return err
}
// Levels define on which log levels this hook would trigger
func (hook *Hook) Levels() []log.Level {
return hook.LogLevels
}