photoprism/internal/event/log.go
Michael Mayer d1db3d04f7 Logs: Improve event log and messages in i18n package
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-07-07 10:51:55 +02:00

49 lines
814 B
Go

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{
Name: "log." + entry.Level.String(),
Fields: Data{
"time": entry.Time,
"level": entry.Level.String(),
"message": entry.Message,
},
})
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,
}
}