-
Notifications
You must be signed in to change notification settings - Fork 9
/
logger.go
114 lines (94 loc) · 2.43 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
package logrus
import (
"io"
"os"
"github.com/sirupsen/logrus"
"github.com/tcncloud/wollemi/ports/logging"
)
func NewLogger(out io.Writer) *Logger {
log := &logrus.Logger{
Formatter: &logrus.TextFormatter{},
Level: logrus.InfoLevel,
ExitFunc: os.Exit,
Out: out,
}
return &Logger{
entry: logrus.NewEntry(log),
logrus: log,
}
}
type Logger struct {
logrus *logrus.Logger
entry *logrus.Entry
}
func (this *Logger) WithError(err error) logging.Logger {
return &Logger{
entry: this.entry.WithError(err),
logrus: this.logrus,
}
}
func (this *Logger) WithFields(fields logging.Fields) logging.Logger {
return &Logger{
entry: this.entry.WithFields(logrus.Fields(fields)),
logrus: this.logrus,
}
}
func (this *Logger) WithField(keys string, value interface{}) logging.Logger {
return &Logger{
entry: this.entry.WithField(keys, value),
logrus: this.logrus,
}
}
func (this *Logger) Infof(format string, args ...interface{}) {
this.entry.Infof(format, args...)
}
func (this *Logger) Info(args ...interface{}) {
this.entry.Info(args...)
}
func (this *Logger) Warnf(format string, args ...interface{}) {
this.entry.Warnf(format, args...)
}
func (this *Logger) Warn(args ...interface{}) {
this.entry.Warn(args...)
}
func (this *Logger) Error(args ...interface{}) {
this.entry.Error(args...)
}
func (this *Logger) Debugf(format string, args ...interface{}) {
this.entry.Debugf(format, args...)
}
func (this *Logger) Debug(args ...interface{}) {
this.entry.Debug(args...)
}
func (this *Logger) GetLevel() logging.Level {
return logging.Level(this.logrus.GetLevel())
}
func (this *Logger) SetLevel(lvl logging.Level) {
switch lvl {
case logging.PanicLevel:
this.logrus.SetLevel(logrus.PanicLevel)
case logging.FatalLevel:
this.logrus.SetLevel(logrus.FatalLevel)
case logging.ErrorLevel:
this.logrus.SetLevel(logrus.ErrorLevel)
case logging.WarnLevel:
this.logrus.SetLevel(logrus.WarnLevel)
case logging.InfoLevel:
this.logrus.SetLevel(logrus.InfoLevel)
case logging.DebugLevel:
this.logrus.SetLevel(logrus.DebugLevel)
case logging.TraceLevel:
this.logrus.SetLevel(logrus.TraceLevel)
}
}
func (*Logger) Exit(code int) {
logrus.Exit(code)
}
func (this *Logger) SetFormatter(formatter logging.Formatter) {
switch formatter.(type) {
case *logging.TextFormatter:
this.logrus.SetFormatter(&logrus.TextFormatter{})
case *logging.JsonFormatter:
this.logrus.SetFormatter(&logrus.JSONFormatter{})
}
}