...
1 package logrus_test
2
3 import (
4 "os"
5
6 "github.com/sirupsen/logrus"
7 )
8
9 func Example_basic() {
10 var log = logrus.New()
11 log.Formatter = new(logrus.JSONFormatter)
12 log.Formatter = new(logrus.TextFormatter)
13 log.Formatter.(*logrus.TextFormatter).DisableColors = true
14 log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true
15 log.Level = logrus.TraceLevel
16 log.Out = os.Stdout
17
18
19
20
21
22
23
24
25 defer func() {
26 err := recover()
27 if err != nil {
28 entry := err.(*logrus.Entry)
29 log.WithFields(logrus.Fields{
30 "omg": true,
31 "err_animal": entry.Data["animal"],
32 "err_size": entry.Data["size"],
33 "err_level": entry.Level,
34 "err_message": entry.Message,
35 "number": 100,
36 }).Error("The ice breaks!")
37 }
38 }()
39
40 log.WithFields(logrus.Fields{
41 "animal": "walrus",
42 "number": 0,
43 }).Trace("Went to the beach")
44
45 log.WithFields(logrus.Fields{
46 "animal": "walrus",
47 "number": 8,
48 }).Debug("Started observing beach")
49
50 log.WithFields(logrus.Fields{
51 "animal": "walrus",
52 "size": 10,
53 }).Info("A group of walrus emerges from the ocean")
54
55 log.WithFields(logrus.Fields{
56 "omg": true,
57 "number": 122,
58 }).Warn("The group's number increased tremendously!")
59
60 log.WithFields(logrus.Fields{
61 "temperature": -4,
62 }).Debug("Temperature changes")
63
64 log.WithFields(logrus.Fields{
65 "animal": "orca",
66 "size": 9009,
67 }).Panic("It's over 9000!")
68
69
70
71
72
73
74
75
76
77 }
78
View as plain text