forked from agtorre/go-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logrus.go
39 lines (32 loc) · 843 Bytes
/
logrus.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
package structured
import "github.com/sirupsen/logrus"
// Hook will implement the logrus
// hook interface
type Hook struct {
id string
}
// Fire will trigger whenever you log
func (hook *Hook) Fire(entry *logrus.Entry) error {
entry.Data["id"] = hook.id
return nil
}
// Levels is what levels this hook will fire on
func (hook *Hook) Levels() []logrus.Level {
return logrus.AllLevels
}
// Logrus demonstrates some basic logrus functionality
func Logrus() {
// we're emitting in json format
logrus.SetFormatter(&logrus.TextFormatter{})
logrus.SetLevel(logrus.InfoLevel)
logrus.AddHook(&Hook{"123"})
fields := logrus.Fields{}
fields["success"] = true
fields["complex_struct"] = struct {
Event string
When string
}{"Something happened", "Just now"}
x := logrus.WithFields(fields)
x.Warn("warning!")
x.Error("error!")
}