/
logrus.go
90 lines (73 loc) · 2.07 KB
/
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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"log/syslog"
"os"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
"gopkg.in/gemnasium/logrus-airbrake-hook.v4"
)
func do1() {
log.WithFields(log.Fields{
"animal": "walrus",
}).Info("A walrus appears")
}
func do2() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.WarnLevel)
log.WithFields(log.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
log.WithFields(log.Fields{
"omg": true,
"number": 122,
}).Warn("The group's number increased tremendously!")
/*log.WithFields(log.Fields{
"omg": true,
"number": 100,
}).Fatal("The ice breaks!")*/
log.SetLevel(log.DebugLevel)
// A common pattern is to re-use fields between logging statements by re-using
// the logrus.Entry returned from WithFields()
contextLogger := log.WithFields(log.Fields{
"common": "this is a common field",
"other": "I also should be logged always",
})
contextLogger.Info("I'll be logged with common and other field")
contextLogger.Info("Me too")
}
func do3() {
var log = logrus.New()
log.Out = os.Stdout
log.WithFields(logrus.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
}
func do4() {
log.AddHook(airbrake.NewHook(123, "xyz", "production"))
//hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
if err != nil {
log.Error("Unable to connect to local syslog daemon")
} else {
log.Info("connect to local syslog daemon")
log.AddHook(hook)
}
}
func do5() {
//log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&log.TextFormatter{})
log.WithFields(log.Fields{
"animal": "walrus",
}).Info("A walrus appears")
}
func main() {
do4()
}