forked from golang-standards/project-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
82 lines (73 loc) · 2.25 KB
/
logger.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
package tools
import (
"github.com/sirupsen/logrus"
"os"
)
// For more information: https://github.com/sirupsen/logrus/blob/master/example_basic_test.go
var log *logrus.Logger
func InitializeLogger() {
log = logrus.New()
//log.Formatter = new(logrus.JSONFormatter)
log.Formatter = new(logrus.TextFormatter) //default
log.Level = logrus.TraceLevel
log.Out = os.Stdout
// file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
// if err == nil {
// log.Out = file
// } else {
// log.Info("Failed to log to file, using default stderr")
// }
defer func() {
err := recover()
if err != nil {
entry := err.(*logrus.Entry)
log.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,
}).Error("The ice breaks!") // or use Fatal() to force the process to exit with a nonzero code
}
}()
//log.WithFields(logrus.Fields{
// "animal": "walrus",
// "number": 0,
//}).Trace("Went to the beach")
//
//log.WithFields(logrus.Fields{
// "animal": "walrus",
// "number": 8,
//}).Debug("Started observing beach")
//
//log.WithFields(logrus.Fields{
// "animal": "walrus",
// "size": 10,
//}).Info("A group of walrus emerges from the ocean")
//
//log.WithFields(logrus.Fields{
// "omg": true,
// "number": 122,
//}).Warn("The group's number increased tremendously!")
//
//log.WithFields(logrus.Fields{
// "temperature": -4,
//}).Debug("Temperature changes")
//
//log.WithFields(logrus.Fields{
// "animal": "orca",
// "size": 9009,
//}).Panic("It's over 9000!")
// Output:
// level=trace msg="Went to the beach" animal=walrus number=0
// 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
}
func Log() *logrus.Logger {
return log
}