2019-12-02 00:30:58 +01:00
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
2022-05-20 11:31:39 +02:00
|
|
|
"fmt"
|
2019-12-02 00:30:58 +01:00
|
|
|
|
|
|
|
"github.com/leandro-lugaresi/hub"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// TextFormatter for log messages.
|
|
|
|
var TextFormatter = &logrus.TextFormatter{
|
|
|
|
DisableColors: false,
|
|
|
|
FullTimestamp: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log is the global default logger.
|
|
|
|
var Log Logger
|
|
|
|
var LogBuffer Buffer
|
2019-12-02 00:30:58 +01:00
|
|
|
|
2022-05-20 11:31:39 +02:00
|
|
|
// Hook represents a log event hook.
|
2019-12-02 00:30:58 +01:00
|
|
|
type Hook struct {
|
|
|
|
hub *hub.Hub
|
|
|
|
}
|
|
|
|
|
2022-05-20 11:31:39 +02:00
|
|
|
// NewHook creates a new log event hook.
|
2019-12-02 00:30:58 +01:00
|
|
|
func NewHook(hub *hub.Hub) *Hook {
|
|
|
|
return &Hook{hub: hub}
|
|
|
|
}
|
|
|
|
|
2022-05-20 11:31:39 +02:00
|
|
|
// Fire publishes a new log event,
|
2019-12-02 00:30:58 +01:00
|
|
|
func (h *Hook) Fire(entry *logrus.Entry) error {
|
2022-05-20 11:31:39 +02:00
|
|
|
if entry == nil {
|
|
|
|
return fmt.Errorf("log entry is empty")
|
|
|
|
} else if entry.Message == "" {
|
|
|
|
return fmt.Errorf("log message is empty")
|
2022-09-28 09:01:17 +02:00
|
|
|
} else if LogBuffer.Get() == entry.Message {
|
2022-05-20 11:31:39 +02:00
|
|
|
return nil
|
2022-09-28 09:01:17 +02:00
|
|
|
} else if err := LogBuffer.Set(entry.Message); err != nil {
|
2022-05-20 11:31:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-02 00:30:58 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-20 11:31:39 +02:00
|
|
|
// Levels returns a slice containing all supported log levels.
|
2019-12-02 00:30:58 +01:00
|
|
|
func (h *Hook) Levels() []logrus.Level {
|
|
|
|
return logrus.AllLevels
|
|
|
|
}
|