-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlog.go
44 lines (38 loc) · 834 Bytes
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package squircy2
import (
"os"
log "github.com/sirupsen/logrus"
"github.com/veonik/squircy2/event"
)
const (
LogEvent event.EventType = "log.OUTPUT"
)
func newLogger(evm event.EventManager) *log.Logger {
hooks := make(log.LevelHooks)
hooks.Add(&triggerHook{evm})
return &log.Logger{
Out: os.Stderr,
Formatter: new(log.TextFormatter),
Hooks: hooks,
Level: log.InfoLevel,
}
}
type triggerHook struct {
evm event.EventManager
}
func (l *triggerHook) Levels() []log.Level {
return log.AllLevels
}
func (l *triggerHook) Fire(e *log.Entry) error {
data := make(map[string]interface{})
for k, v := range e.Data {
data[k] = v
}
l.evm.Trigger(LogEvent, map[string]interface{}{
"Time": e.Time.String(),
"Level": e.Level.String(),
"Message": e.Message,
"Data": data,
})
return nil
}