Repository navigation
Expand file tree
/
Copy pathstd.go
More file actions
320 lines (296 loc) · 6.58 KB
/
Copy pathstd.go
File metadata and controls
320 lines (296 loc) · 6.58 KB
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
package llog
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"sync"
"time"
)
var _ Logger = (*stdLogger)(nil)
type StdOption interface {
apply(*StdConfig) error
}
var colorsLen = 6
const (
b = 1 << (10 * iota) // byte
Kb
Mb
Gb
Tb
perm = 0700
// Pb
)
type StdConfig struct {
// 日志输出基础文件名
LogFile string
// 是否在达到指定大小时切换文件
IsBackup bool
// 日志文件大小
MaxLogSize int64
// 是否启用标准输出
IsStdOut bool
// 是否启用标准错误
IsStdErr bool
// Info Debug Warn Error Fatal
Colors map[Level][]Color
Colored bool
TimeFormat string
TimeZone *time.Location
}
type stdopfunc func(*StdConfig) error
func (s stdopfunc) apply(sc *StdConfig) error {
return s(sc)
}
type stdLogger struct {
pool *sync.Pool
callerSkip int
stdcfg *StdConfig
logfile *os.File
// io *IO
}
func WithStdOut(c bool) StdOption {
return stdopfunc(func(sc *StdConfig) error {
sc.IsStdOut = c
return nil
})
}
func WithStdErr(c bool) StdOption {
return stdopfunc(func(sc *StdConfig) error {
sc.IsStdErr = c
return nil
})
}
func WithMaxLogSize(sz int64) StdOption {
return stdopfunc(func(sc *StdConfig) error {
sc.MaxLogSize = sz
return nil
})
}
func WithBackUp(c bool) StdOption {
return stdopfunc(func(sc *StdConfig) error {
sc.IsBackup = c
return nil
})
}
func WithLogFile(f string) StdOption {
return stdopfunc(func(sc *StdConfig) error {
sc.LogFile = f
return nil
})
}
func WithStdTimeFormat(format string) StdOption {
return stdopfunc(func(sc *StdConfig) error {
if format == "" {
return errors.New("time format empty")
}
sc.TimeFormat = format
return nil
})
}
func WithStdTimeZone(tz *time.Location) StdOption {
return stdopfunc(func(sc *StdConfig) error {
if tz == nil {
return errors.New("time zone nil")
}
sc.TimeZone = tz
return nil
})
}
func WithStdColors(cs map[Level][]Color) StdOption {
return stdopfunc(func(sc *StdConfig) error {
// cs's length should be le then 6
if len(cs) > colorsLen {
return errors.New("colors should be no more than 6")
}
for level, colour := range cs {
sc.Colors[level] = colour
}
return nil
})
}
func WithStdColored(c bool) StdOption {
return stdopfunc(func(sc *StdConfig) error {
sc.Colored = c
return nil
})
}
// TODO: lock
func NewStdLogger(opts ...StdOption) Logger {
cfg := &StdConfig{
Colored: true,
Colors: map[Level][]Color{
Info: {
// FgWhite,
FgGreen,
},
Debug: {
FgCyan,
// BgCyan,
},
Warn: {
// FgWhite,
FgYellow,
},
Error: {
// FgWhite,
FgRed,
},
Fatal: {
// BgRed,
FgRed,
},
// Fatal
},
TimeFormat: "2006-01-02 15:04:05.000",
TimeZone: time.Now().UTC().Location(),
IsBackup: false, // 默认在文件到达MaxLogSize大小时要切换文件
MaxLogSize: 100 * Mb, // 默认文件大小为100日志mb
IsStdOut: true, // 默认开启标准输出
IsStdErr: false, // 默认关闭标准错误
LogFile: "llog.log",
}
l := &stdLogger{
pool: &sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
},
}
for _, o := range opts {
o.apply(cfg)
}
if !cfg.IsBackup {
l.stdcfg = cfg
return l
}
f := OpenFile(cfg.LogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, perm)
if f == nil {
l.stdcfg = cfg
return l
}
l.logfile = f
l.stdcfg = cfg
return l
}
// 底层logger的caller skip
func (l *stdLogger) AddCallerSkip(skip int) Logger {
l.callerSkip += skip
return l
}
func (l *stdLogger) GetCallerSkip() int {
return l.callerSkip
}
func (l *stdLogger) Clone() Logger {
cloned := &stdLogger{}
*cloned = *l
return cloned
}
func buildstring(l *stdLogger, level Level, keyvals ...any) (builded string, raw string) {
if len(keyvals) == 0 {
return "", ""
}
if (len(keyvals) & 1) == 1 {
keyvals = append(keyvals, "KEYVALS UNPAIRED")
}
buf := l.pool.Get().(*bytes.Buffer)
stack := captureStacktrace(l.callerSkip + 1)
defer stack.Free()
frame, _ := stack.Next()
file, line := frame.File, frame.Line
var ws string
if !l.stdcfg.Colored {
ws = level.String()
ws = fmt.Sprintf("%v %9s", time.Now().Format(l.stdcfg.TimeFormat), ws)
// 构建没有颜色的字符串前部份
raw += ws
goto blank
}
// 构建有颜色的字符串前部份
builded = fmt.Sprintf("%v %20s", time.Now().Format(l.stdcfg.TimeFormat), level.String())
raw = fmt.Sprintf("%v %20s", time.Now().Format(l.stdcfg.TimeFormat), level.String())
switch level {
case Debug:
ws = WithColor(builded, l.stdcfg.Colors[Debug]...)
case Info:
ws = WithColor(builded, l.stdcfg.Colors[Info]...)
case Warn:
ws = WithColor(builded, l.stdcfg.Colors[Warn]...)
case Error:
ws = WithColor(builded, l.stdcfg.Colors[Error]...)
case Fatal:
ws = WithColor(builded, l.stdcfg.Colors[Fatal]...)
default:
ws = WithColor(builded, l.stdcfg.Colors[Info]...)
}
blank:
buf.WriteString(ws)
var path string
idx := strings.LastIndexByte(file, '/')
if idx == -1 {
path = file + ":" + fmt.Sprintf("%d", line)
} else {
idx = strings.LastIndexByte(file[:idx], '/')
path = file[idx+1:] + ":" + fmt.Sprintf("%d", line)
}
fmt.Fprintf(buf, " [%s]", path)
// TODO: maybe this should be colored?
for i := 0; i < len(keyvals); i += 2 {
raw += fmt.Sprintf(" %s: %v", keyvals[i], keyvals[i+1])
_, _ = fmt.Fprintf(buf, " %s: %v", keyvals[i], keyvals[i+1])
}
res := buf.String()
buf.Reset()
l.pool.Put(buf)
return res, raw
}
func (l *stdLogger) backup() bool {
if !l.stdcfg.IsBackup {
return true
}
fi, err := l.logfile.Stat()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return false
}
// 如果当前文件大小已经达到设置的备份大小,则进行备份
if fi.Size() >= l.stdcfg.MaxLogSize {
// 重新创建并打开一个新文件
f := OpenFile(fmt.Sprintf("%s-%d.log", l.stdcfg.LogFile, time.Now().UnixNano()), os.O_CREATE|os.O_APPEND|os.O_RDWR, perm)
if f == nil {
fmt.Fprintln(os.Stderr, "new log file created failed")
return false
}
// 关闭当前日志文件
l.logfile.Close()
l.logfile = f
return true
}
return true
}
func (l *stdLogger) Log(level Level, keyvals ...any) error {
s, raw := buildstring(l, level, keyvals...)
if l.stdcfg.IsStdOut {
// TODO: 暂时stdout直接使用fmt
fmt.Fprintln(os.Stdout, s)
}
if l.stdcfg.IsStdErr {
fmt.Fprintln(os.Stderr, s)
}
if l.stdcfg.IsBackup {
if !l.backup() {
return errors.New("back to file error")
}
_, err := l.logfile.Write([]byte(raw + "\n"))
if err != nil {
fmt.Fprintln(os.Stderr, "log to file error")
return errors.New("log to file error")
}
}
// _ = l.log.Output(4, buf.String())
if level == Fatal {
os.Exit(1)
}
return nil
}