forked from sirupsen/logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_basic_test.go
70 lines (60 loc) · 1.98 KB
/
example_basic_test.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package logrus_test
import (
"github.com/xitonix/logrus"
"os"
)
func Example_basic() {
var log = logrus.New(logrus.DebugLevel)
log.SetFormatter(new(logrus.JSONFormatter))
log.SetFormatter(&logrus.TextFormatter{
DisableSorting: false,
DisableTimestamp: true,
})
log.SetOutput(os.Stdout)
// file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
// if err == nil {
// log.SetOutput(file)
// } else {
// log.Info("Failed to log to file, using default stderr")
// }
defer func() {
err := recover()
if err != nil {
entry := *err.(**logrus.Entry)
log.AsError().WithFields(logrus.Fields{
"omg": true,
"err_animal": entry.Data["animal"],
"err_size": entry.Data["size"],
"err_level": entry.Level,
"err_message": entry.Message,
"number": 100,
}).Write("The ice breaks!") // or use Fatal() to force the process to exit with a nonzero code
}
}()
log.AsDebug().WithFields(logrus.Fields{
"animal": "walrus",
"number": 8,
}).Write("Started observing beach")
log.AsInfo().WithFields(logrus.Fields{
"animal": "walrus",
"size": 10,
}).Write("A group of walrus emerges from the ocean")
log.AsWarning().WithFields(logrus.Fields{
"omg": true,
"number": 122,
}).Write("The group's number increased tremendously!")
log.AsDebug().WithFields(logrus.Fields{
"temperature": -4,
}).Write("Temperature changes")
log.AsPanic().WithFields(logrus.Fields{
"animal": "orca",
"size": 9009,
}).Write("It's over 9000!")
// Output:
// level=debug msg="Started observing beach" animal=walrus number=8
// level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
// level=warning msg="The group's number increased tremendously!" number=122 omg=true
// level=debug msg="Temperature changes" temperature=-4
// level=panic msg="It's over 9000!" animal=orca size=9009
// level=error msg="The ice breaks!" err_animal=orca err_level=panic err_message="It's over 9000!" err_size=9009 number=100 omg=true
}