2019-03-15 14:09:11 +03:00
|
|
|
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 {
|
2019-03-18 17:07:31 +03:00
|
|
|
line, err := entry.Bytes()
|
2019-03-15 14:09:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-18 17:07:31 +03:00
|
|
|
_, err = hook.Writer.Write(line)
|
2019-03-15 14:09:11 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Levels define on which log levels this hook would trigger
|
|
|
|
func (hook *Hook) Levels() []log.Level {
|
|
|
|
return hook.LogLevels
|
|
|
|
}
|