2019-12-02 00:30:58 +01:00
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/leandro-lugaresi/hub"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Log *logrus.Logger
|
|
|
|
|
|
|
|
type Hook struct {
|
|
|
|
hub *hub.Hub
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHook(hub *hub.Hub) *Hook {
|
|
|
|
return &Hook{hub: hub}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hook) Fire(entry *logrus.Entry) error {
|
|
|
|
h.hub.Publish(Message{
|
2019-12-11 14:10:20 +01:00
|
|
|
Name: "log." + entry.Level.String(),
|
2019-12-02 00:30:58 +01:00
|
|
|
Fields: Data{
|
2020-07-07 10:51:55 +02:00
|
|
|
"time": entry.Time,
|
|
|
|
"level": entry.Level.String(),
|
|
|
|
"message": entry.Message,
|
2019-12-02 00:30:58 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hook) Levels() []logrus.Level {
|
|
|
|
return logrus.AllLevels
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
hooks := logrus.LevelHooks{}
|
|
|
|
hooks.Add(NewHook(SharedHub()))
|
|
|
|
|
|
|
|
Log = &logrus.Logger{
|
|
|
|
Out: os.Stderr,
|
|
|
|
Formatter: &logrus.TextFormatter{},
|
|
|
|
Hooks: hooks,
|
|
|
|
Level: logrus.DebugLevel,
|
|
|
|
ExitFunc: os.Exit,
|
|
|
|
ReportCaller: false,
|
|
|
|
}
|
|
|
|
}
|