-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
145 lines (122 loc) · 3.78 KB
/
logging.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package logging
import (
"io"
"io/ioutil"
"os"
graylog "github.com/gemnasium/logrus-graylog-hook/v3"
log "github.com/sirupsen/logrus"
)
var (
logger *log.Logger
field map[string]interface{}
)
// WriterHook is a hook that writes logs of specified LogLevels to specified Writer
type WriterHook struct {
Writer io.Writer
LogLevels []log.Level
}
// init is the initiliaze logrus logger
func init() {
logger = log.New()
}
// Initialize is using default set text formatter if formatter is nil.
func Initialize(formatter log.Formatter) *log.Logger {
if formatter == nil {
formatter = &log.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
ForceColors: true,
FullTimestamp: true,
}
}
logger.SetFormatter(formatter)
return logger
}
//SetDefaultFields, you can have fields always attached to log statements in application
func SetDefaultFields(fields map[string]interface{}) {
field = fields
}
// Fire will be called when some logging function is called with current hook
// It will format log entry to string and write it to appropriate writer
func (hook *WriterHook) Fire(entry *log.Entry) error {
line, err := entry.String()
if err != nil {
return err
}
_, err = hook.Writer.Write([]byte(line))
return err
}
// Levels define on which log levels this hook would trigger
func (hook *WriterHook) Levels() []log.Level {
return hook.LogLevels
}
// SplitLogs adds hooks to send logs to different destinations depending on level
func SplitLogs() {
logger.SetOutput(ioutil.Discard) // Send all logs to nowhere by default
logger.AddHook(&WriterHook{ // Send logs with level higher than warning to stderr
Writer: os.Stderr,
LogLevels: []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
log.WarnLevel,
},
})
logger.AddHook(&WriterHook{ // Send info and debug logs to stdout
Writer: os.Stdout,
LogLevels: []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
log.WarnLevel,
log.InfoLevel,
log.DebugLevel,
},
})
}
// AddAsyncGraylogHook also send log data to graylog
func AddAsyncGraylogHook(host, port string, extra map[string]interface{}) {
addr := host + ":" + port
hook := graylog.NewAsyncGraylogHook(addr, extra)
hook.Level = log.WarnLevel
logger.AddHook(hook)
}
// Fatalln is equivalent to Logln() followed by a call to os.Exit(1)
func Fatalln(args ...interface{}) {
logger.WithFields(field).Fatalln(args...)
}
// Fatalf is equivalent to Logf() followed by a call to os.Exit(1)
func Fatalf(format string, args ...interface{}) {
logger.WithFields(field).Fatalf(format, args...)
}
// Println calls Output to print to the standard logger
func Println(args ...interface{}) {
logger.WithFields(field).Println(args...)
}
// Printf calls Output to print to the standard logger
func Printf(format string, args ...interface{}) {
logger.WithFields(field).Printf(format, args...)
}
// Errorf calls Output to print to the standard logger with error level
func Errorf(format string, args ...interface{}) {
logger.WithFields(field).Errorf(format, args...)
}
// Errorln calls Output to print to the standard logger with error level
func Errorln(format string, args ...interface{}) {
logger.WithFields(field).Errorln(args...)
}
// Warnf calls Output to print to the standard logger with warm level
func Warnf(format string, args ...interface{}) {
logger.WithFields(field).Warnf(format, args...)
}
// Warnln calls Output to print to the standard logger with warm level
func Warnln(format string, args ...interface{}) {
logger.WithFields(field).Warnln(args...)
}
// Println calls Output to print to the standard logger
func Debugln(args ...interface{}) {
logger.WithFields(field).Debugln(args...)
}
// Printf calls Output to print to the standard logger
func Debugf(format string, args ...interface{}) {
logger.WithFields(field).Debugf(format, args...)
}