-
Notifications
You must be signed in to change notification settings - Fork 13
/
logger.go
384 lines (322 loc) · 9.06 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2019 Grendel Authors. All rights reserved.
//
// This file is part of Grendel.
//
// Grendel is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Grendel is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Grendel. If not, see <https://www.gnu.org/licenses/>.
package provision
// This code was adopted from https://github.com/neko-neko/echo-logrus
//
// The MIT License (MIT)
//
// Copyright (c) 2017 neko-neko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import (
"encoding/json"
"io"
echolog "github.com/labstack/gommon/log"
"github.com/sirupsen/logrus"
)
// MyLogger extend logrus.MyLogger
type MyLogger struct {
*logrus.Entry
}
// Singleton logger
var singletonLogger = &MyLogger{
log,
}
// Logger return singleton logger
func EchoLogger() *MyLogger {
return singletonLogger
}
// Print output message of print level
func Print(i ...interface{}) {
singletonLogger.Print(i...)
}
// Printf output format message of print level
func Printf(format string, i ...interface{}) {
singletonLogger.Printf(format, i...)
}
// Printj output json of print level
func Printj(j echolog.JSON) {
singletonLogger.Printj(j)
}
// Debug output message of debug level
func Debug(i ...interface{}) {
singletonLogger.Debug(i...)
}
// Debugf output format message of debug level
func Debugf(format string, args ...interface{}) {
singletonLogger.Debugf(format, args...)
}
// Debugj output json of debug level
func Debugj(j echolog.JSON) {
singletonLogger.Debugj(j)
}
// Info output message of info level
func Info(i ...interface{}) {
singletonLogger.Info(i...)
}
// Infof output format message of info level
func Infof(format string, args ...interface{}) {
singletonLogger.Infof(format, args...)
}
// Infoj output json of info level
func Infoj(j echolog.JSON) {
singletonLogger.Infoj(j)
}
// Warn output message of warn level
func Warn(i ...interface{}) {
singletonLogger.Warn(i...)
}
// Warnf output format message of warn level
func Warnf(format string, args ...interface{}) {
singletonLogger.Warnf(format, args...)
}
// Warnj output json of warn level
func Warnj(j echolog.JSON) {
singletonLogger.Warnj(j)
}
// Error output message of error level
func Error(i ...interface{}) {
singletonLogger.Error(i...)
}
// Errorf output format message of error level
func Errorf(format string, args ...interface{}) {
singletonLogger.Errorf(format, args...)
}
// Errorj output json of error level
func Errorj(j echolog.JSON) {
singletonLogger.Errorj(j)
}
// Fatal output message of fatal level
func Fatal(i ...interface{}) {
singletonLogger.Fatal(i...)
}
// Fatalf output format message of fatal level
func Fatalf(format string, args ...interface{}) {
singletonLogger.Fatalf(format, args...)
}
// Fatalj output json of fatal level
func Fatalj(j echolog.JSON) {
singletonLogger.Fatalj(j)
}
// Panic output message of panic level
func Panic(i ...interface{}) {
singletonLogger.Panic(i...)
}
// Panicf output format message of panic level
func Panicf(format string, args ...interface{}) {
singletonLogger.Panicf(format, args...)
}
// Panicj output json of panic level
func Panicj(j echolog.JSON) {
singletonLogger.Panicj(j)
}
// To logrus.Level
func toLogrusLevel(level echolog.Lvl) logrus.Level {
switch level {
case echolog.DEBUG:
return logrus.DebugLevel
case echolog.INFO:
return logrus.InfoLevel
case echolog.WARN:
return logrus.WarnLevel
case echolog.ERROR:
return logrus.ErrorLevel
}
return logrus.InfoLevel
}
// To Echo.log.lvl
func toEchoLevel(level logrus.Level) echolog.Lvl {
switch level {
case logrus.DebugLevel:
return echolog.DEBUG
case logrus.InfoLevel:
return echolog.INFO
case logrus.WarnLevel:
return echolog.WARN
case logrus.ErrorLevel:
return echolog.ERROR
}
return echolog.OFF
}
// Output return logger io.Writer
func (l *MyLogger) Output() io.Writer {
return l.Writer()
}
// SetOutput logger io.Writer
func (l *MyLogger) SetOutput(w io.Writer) {
}
// Level return logger level
func (l *MyLogger) Level() echolog.Lvl {
return toEchoLevel(l.Logger.Level)
}
// SetLevel logger level
func (l *MyLogger) SetLevel(v echolog.Lvl) {
l.Logger.Level = toLogrusLevel(v)
}
// SetHeader logger header
// Managed by Logrus itself
// This function do nothing
func (l *MyLogger) SetHeader(h string) {
// do nothing
}
// Formatter return logger formatter
func (l *MyLogger) Formatter() logrus.Formatter {
return l.Logger.Formatter
}
// SetFormatter logger formatter
// Only support logrus formatter
func (l *MyLogger) SetFormatter(formatter logrus.Formatter) {
l.Logger.Formatter = formatter
}
// Prefix return logger prefix
// This function do nothing
func (l *MyLogger) Prefix() string {
return ""
}
// SetPrefix logger prefix
// This function do nothing
func (l *MyLogger) SetPrefix(p string) {
// do nothing
}
// Print output message of print level
func (l *MyLogger) Print(i ...interface{}) {
l.Logger.Print(i...)
}
// Printf output format message of print level
func (l *MyLogger) Printf(format string, args ...interface{}) {
l.Logger.Printf(format, args...)
}
// Printj output json of print level
func (l *MyLogger) Printj(j echolog.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.Println(string(b))
}
// Debug output message of debug level
func (l *MyLogger) Debug(i ...interface{}) {
l.Logger.Debug(i...)
}
// Debugf output format message of debug level
func (l *MyLogger) Debugf(format string, args ...interface{}) {
l.Logger.Debugf(format, args...)
}
// Debugj output message of debug level
func (l *MyLogger) Debugj(j echolog.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.Debugln(string(b))
}
// Info output message of info level
func (l *MyLogger) Info(i ...interface{}) {
l.Logger.Info(i...)
}
// Infof output format message of info level
func (l *MyLogger) Infof(format string, args ...interface{}) {
l.Logger.Infof(format, args...)
}
// Infoj output json of info level
func (l *MyLogger) Infoj(j echolog.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.Infoln(string(b))
}
// Warn output message of warn level
func (l *MyLogger) Warn(i ...interface{}) {
l.Logger.Warn(i...)
}
// Warnf output format message of warn level
func (l *MyLogger) Warnf(format string, args ...interface{}) {
l.Logger.Warnf(format, args...)
}
// Warnj output json of warn level
func (l *MyLogger) Warnj(j echolog.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.Warnln(string(b))
}
// Error output message of error level
func (l *MyLogger) Error(i ...interface{}) {
l.Logger.Error(i...)
}
// Errorf output format message of error level
func (l *MyLogger) Errorf(format string, args ...interface{}) {
l.Logger.Errorf(format, args...)
}
// Errorj output json of error level
func (l *MyLogger) Errorj(j echolog.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.Errorln(string(b))
}
// Fatal output message of fatal level
func (l *MyLogger) Fatal(i ...interface{}) {
l.Logger.Fatal(i...)
}
// Fatalf output format message of fatal level
func (l *MyLogger) Fatalf(format string, args ...interface{}) {
l.Logger.Fatalf(format, args...)
}
// Fatalj output json of fatal level
func (l *MyLogger) Fatalj(j echolog.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.Fatalln(string(b))
}
// Panic output message of panic level
func (l *MyLogger) Panic(i ...interface{}) {
l.Logger.Panic(i...)
}
// Panicf output format message of panic level
func (l *MyLogger) Panicf(format string, args ...interface{}) {
l.Logger.Panicf(format, args...)
}
// Panicj output json of panic level
func (l *MyLogger) Panicj(j echolog.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.Panicln(string(b))
}