/
logger.go
210 lines (174 loc) · 4.15 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
package logger
import (
"fmt"
"io"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
DebugLevel int8 = iota - 1
InfoLevel
WarnLevel
ErrorLevel
PanicLevel
FatalLevel
)
var (
conf = zap.NewProductionEncoderConfig()
)
type logger struct {
core *zap.SugaredLogger
enable bool
name string
level int8
encoder zapcore.Encoder
outputConsole bool
outputFile bool
outputFilePath string
writers []io.Writer
}
var log = &logger{
name: "app",
encoder: zapcore.NewJSONEncoder(conf),
}
func init() {
conf.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
}
func (l *logger) init() {
l.initOutputConsole()
l.initOutputFile()
cores := make([]zapcore.Core, len(l.writers))
for i, w := range l.writers {
cores[i] = zapcore.NewCore(l.encoder, zapcore.Lock(zapcore.AddSync(w)), zapcore.Level(l.level))
}
l.core = zap.New(zapcore.NewTee(cores...)).Sugar()
}
func (l *logger) initOutputFile() {
if l.outputFile {
if _, err := os.Stat(l.outputFilePath); os.IsNotExist(err) {
os.Mkdir(l.outputFilePath, os.ModePerm)
}
f, _ := os.OpenFile(fmt.Sprintf("%s/%s.log", l.outputFilePath, l.name), os.O_APPEND|os.O_RDWR|os.O_CREATE, os.ModePerm)
l.writers = append(l.writers, f)
}
}
func (l *logger) initOutputConsole() {
if l.outputConsole {
l.writers = append(l.writers, os.Stdout)
}
}
// New returns a logger by specify a name
func New(name string) *logger {
return &logger{
name: name,
outputFilePath: "./logs",
}
}
// Enable makes logger enable to use.
func Enable() *logger {
log.enable = true
log.init()
return log
}
// Enable makes logger enable to use.
func (l *logger) Enable() *logger {
l.enable = true
l.init()
return l
}
// Level sets the level of logger.
// The default level is `Info`.
func Level(level int8) *logger {
log.level = level
return log
}
// Level sets the level of logger.
// The default level is `Info`.
func (l *logger) Level(level int8) *logger {
l.level = level
return l
}
// ConsoleEncoding EncodeConsole sets the encoding `console` of logger.
// The default encoding is `json`.
func ConsoleEncoding() *logger {
return log.EncodeConsole()
}
// EncodeConsole sets the encoding `console` of logger.
// The default encoding is `json`.
func (l *logger) EncodeConsole() *logger {
l.encoder = zapcore.NewConsoleEncoder(conf)
return l
}
// WithFile sets the output file of logger.
func WithFile(path string) *logger {
return log.WithFile(path)
}
// WithFile sets the output file of logger.
func (l *logger) WithFile(path string) *logger {
l.outputFile = true
l.outputFilePath = path
return l
}
func WithConsole() *logger {
return log.WithConsole()
}
func (l *logger) WithConsole() *logger {
l.outputConsole = true
return l
}
func WithWriter(w io.Writer) *logger {
return log.WithWriter(w)
}
func (l *logger) WithWriter(w io.Writer) *logger {
l.writers = append(l.writers, w)
return l
}
func Debug(msg string, keyAndValues ...interface{}) {
log.Debug(msg, keyAndValues...)
}
func (l *logger) Debug(msg string, keyAndValues ...interface{}) {
if l.enable {
l.core.Debugw(msg, keyAndValues...)
}
}
func Info(msg string, keyAndValues ...interface{}) {
log.Info(msg, keyAndValues...)
}
func (l *logger) Info(msg string, keyAndValues ...interface{}) {
if l.enable {
l.core.Infow(msg, keyAndValues...)
}
}
func Warn(msg string, keyAndValues ...interface{}) {
log.Warn(msg, keyAndValues...)
}
func (l *logger) Warn(msg string, keyAndValues ...interface{}) {
if l.enable {
l.core.Warnw(msg, keyAndValues...)
}
}
func Error(msg string, keyAndValues ...interface{}) {
log.Error(msg, keyAndValues)
}
func (l *logger) Error(msg string, keyAndValues ...interface{}) {
if l.enable {
l.core.Errorw(msg, keyAndValues...)
}
}
func Panic(msg string, keyAndValues ...interface{}) {
log.Panic(msg, keyAndValues...)
}
func (l *logger) Panic(msg string, keyAndValues ...interface{}) {
if l.enable {
l.core.Panicw(msg, keyAndValues...)
}
}
func Fatal(msg string, keyAndValues ...interface{}) {
log.Fatal(msg, keyAndValues...)
}
func (l *logger) Fatal(msg string, keyAndValues ...interface{}) {
if l.enable {
l.core.Fatalw(msg, keyAndValues...)
}
}