-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
212 lines (178 loc) · 4.4 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Package common Self encapsulated log printing
package common
import (
"fmt"
"os"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/sirupsen/logrus"
)
const timestampFormat = "2006-01-02T15:04:05.000"
// JSONFormat print log in json format
var JSONFormat bool
// SetLogger set log level and format etc
func SetLogger(logLevel uint32, jsonFormat, colorFormat bool) {
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.Level(logLevel))
JSONFormat = jsonFormat
if JSONFormat {
logrus.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: timestampFormat,
})
} else {
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: colorFormat,
DisableColors: !colorFormat,
ForceQuote: true,
FullTimestamp: true,
TimestampFormat: timestampFormat,
DisableSorting: true,
})
}
}
// SetLogFile set log file path and rotation
func SetLogFile(logFile string, logRotation, logMaxAge uint64) {
if logFile == "" {
return
}
var (
logRotateSuffix = "%Y%m%d%H"
logRotationTime = time.Duration(logRotation) * time.Hour
logMaxAgeTime = time.Duration(logMaxAge) * time.Hour
)
writer, err := rotatelogs.New(
fmt.Sprintf("%s.%s", logFile, logRotateSuffix),
rotatelogs.WithLinkName(logFile),
rotatelogs.WithMaxAge(logMaxAgeTime),
rotatelogs.WithRotationTime(logRotationTime),
)
if err != nil {
logrus.Fatalf("Failed to Initialize Log File %s", err)
}
logrus.SetOutput(writer)
}
// WithFields encapsulate logrus.WithFields
func WithFields(ctx ...interface{}) *logrus.Entry {
length := len(ctx)
if length%2 != 0 {
Debugf("log fileds number %v is not even", length)
}
fields := make(logrus.Fields)
for k := 0; k+2 <= length; k += 2 {
key, ok := ctx[k].(string)
if ok {
fields[key] = ctx[k+1]
} else {
Debugf("log field key '%v' is not string", ctx[k])
}
}
return logrus.WithFields(fields)
}
// Trace trace
func Trace(msg string, ctx ...interface{}) {
WithFields(ctx...).Trace(msg)
}
// Tracef tracef
func Tracef(format string, args ...interface{}) {
logrus.Tracef(format, args...)
}
// Traceln traceln
func Traceln(msg string, ctx ...interface{}) {
WithFields(ctx...).Traceln(msg)
}
// Debug debug
func Debug(msg string, ctx ...interface{}) {
WithFields(ctx...).Debug(msg)
}
// Debugf debugf
func Debugf(format string, args ...interface{}) {
logrus.Debugf(format, args...)
}
// Debugln debugln
func Debugln(msg string, ctx ...interface{}) {
WithFields(ctx...).Debugln(msg)
}
// Info info
func Info(msg string, ctx ...interface{}) {
WithFields(ctx...).Info(msg)
}
// Infof infof
func Infof(format string, args ...interface{}) {
logrus.Infof(format, args...)
}
// Infoln infoln
func Infoln(msg string, ctx ...interface{}) {
WithFields(ctx...).Infoln(msg)
}
// Print print
func Print(msg ...interface{}) {
logrus.Print(msg...)
}
// Printf printf
func Printf(format string, args ...interface{}) {
logrus.Printf(format, args...)
}
// Println println
func Println(msg ...interface{}) {
logrus.Println(msg...)
}
// Warn warn
func Warn(msg string, ctx ...interface{}) {
WithFields(ctx...).Warn(msg)
}
// Warnf warnf
func Warnf(format string, args ...interface{}) {
logrus.Warnf(format, args...)
}
// Warnln warnln
func Warnln(msg string, ctx ...interface{}) {
WithFields(ctx...).Warnln(msg)
}
// Error error
func Error(msg string, ctx ...interface{}) {
WithFields(ctx...).Error(msg)
}
// Errorf errorf
func Errorf(format string, args ...interface{}) {
logrus.Errorf(format, args...)
}
// Errorln errorln
func Errorln(msg string, ctx ...interface{}) {
WithFields(ctx...).Errorln(msg)
}
// Fatal fatal
func Fatal(msg string, ctx ...interface{}) {
WithFields(ctx...).Fatal(msg)
}
// Fatalf fatalf
func Fatalf(format string, args ...interface{}) {
logrus.Fatalf(format, args...)
}
// Fatalln fatalln
func Fatalln(msg string, ctx ...interface{}) {
WithFields(ctx...).Fatalln(msg)
}
// Crit alias of `Fatal`
func Crit(msg string, ctx ...interface{}) {
Fatal(msg, ctx...)
}
// Critf alias of `Fatalf`
func Critf(format string, args ...interface{}) {
Fatalf(format, args...)
}
// Critln alias of `Fatalln`
func Critln(msg string, ctx ...interface{}) {
Fatalln(msg, ctx...)
}
// Panic panic
func Panic(msg string, ctx ...interface{}) {
WithFields(ctx...).Panic(msg)
}
// Panicf panicf
func Panicf(format string, args ...interface{}) {
logrus.Panicf(format, args...)
}
// Panicln panicln
func Panicln(msg string, ctx ...interface{}) {
WithFields(ctx...).Panicln(msg)
}