photoprism/internal/event/log.go
Michael Mayer 2de1e338bd Logs: Prevent feedback loops by omitting repeated messages #2335
This also prevents debug and trace messages to be sent to the frontend.
2022-05-20 11:31:39 +02:00

50 lines
1,000 B
Go

package event
import (
"fmt"
"github.com/leandro-lugaresi/hub"
"github.com/sirupsen/logrus"
)
var Log *logrus.Logger
var LastLog Buffer
// Hook represents a log event hook.
type Hook struct {
hub *hub.Hub
}
// NewHook creates a new log event hook.
func NewHook(hub *hub.Hub) *Hook {
return &Hook{hub: hub}
}
// Fire publishes a new log event,
func (h *Hook) Fire(entry *logrus.Entry) error {
if entry == nil {
return fmt.Errorf("log entry is empty")
} else if entry.Message == "" {
return fmt.Errorf("log message is empty")
} else if LastLog.Get() == entry.Message {
return nil
} else if err := LastLog.Set(entry.Message); err != nil {
return err
}
h.hub.Publish(Message{
Name: "log." + entry.Level.String(),
Fields: Data{
"time": entry.Time,
"level": entry.Level.String(),
"message": entry.Message,
},
})
return nil
}
// Levels returns a slice containing all supported log levels.
func (h *Hook) Levels() []logrus.Level {
return logrus.AllLevels
}