-
Notifications
You must be signed in to change notification settings - Fork 55
/
zlogger.go
529 lines (468 loc) · 11.1 KB
/
zlogger.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
package zlog
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"runtime"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/ztime"
)
// Log header information tag bit, using bitmap mode
const (
BitDate = 1 << iota // Date marker 2019/01/23
BitTime // Time Label Bit 01:23:12
BitMicroSeconds // Microsecond label bit 01:23:12.111222
BitLongFile // Full file name /home/go/src/github.com/sohaha/zlsgo/doc.go
BitShortFile // Final File name doc.go
BitLevel // Current log level
BitStdFlag = BitDate | BitTime // Standard header log format
BitDefault = BitLevel | BitShortFile | BitTime // Default log header format
// LogMaxBuf LogMaxBuf
LogMaxBuf = 1024 * 1024
)
// log level
const (
LogFatal = iota
LogPanic
LogError
LogWarn
LogTips
LogSuccess
LogInfo
LogDebug
LogDump
LogNot = -1
)
var Levels = []string{
"[FATAL]",
"[PANIC]",
"[ERROR]",
"[WARN] ",
"[TIPS] ",
"[SUCCE]",
"[INFO] ",
"[DEBUG]",
"[DUMP] ",
}
var LevelColous = []Color{
ColorRed,
ColorLightRed,
ColorRed,
ColorYellow,
ColorWhite,
ColorGreen,
ColorBlue,
ColorLightCyan,
ColorCyan,
}
type (
// Logger logger struct
Logger struct {
mu sync.RWMutex
prefix string
flag int
out io.Writer
buf bytes.Buffer
file *os.File
calldDepth int
level int
color bool
fileDir string
fileName string
fileAndStdout bool
}
formatter struct {
v reflect.Value
force bool
quote bool
}
visit struct {
v uintptr
typ reflect.Type
}
zprinter struct {
io.Writer
tw *tabwriter.Writer
visited map[visit]int
depth int
}
)
// New Initialize a log object
func New(moduleName ...string) *Logger {
name := ""
if len(moduleName) > 0 {
name = moduleName[0]
}
return NewZLog(os.Stderr, name, BitDefault, LogDump, true, 2)
}
// NewZLog Create log
func NewZLog(out io.Writer, prefix string, flag int, level int, color bool, calldDepth int) *Logger {
zlog := &Logger{out: out, prefix: prefix, flag: flag, file: nil, calldDepth: calldDepth, level: level, color: color}
runtime.SetFinalizer(zlog, CleanLog)
return zlog
}
// CleanLog CleanLog
func CleanLog(log *Logger) {
log.CloseFile()
}
// DisableConsoleColor DisableConsoleColor
func (log *Logger) DisableConsoleColor() {
log.color = false
}
// ForceConsoleColor ForceConsoleColor
func (log *Logger) ForceConsoleColor() {
log.color = true
}
// ColorTextWrap ColorTextWrap
func (log *Logger) ColorTextWrap(color Color, text string) string {
if log.color {
return ColorTextWrap(color, text)
}
return text
}
// ColorBackgroundWrap ColorBackgroundWrap
func (log *Logger) ColorBackgroundWrap(color Color, backgroundColor Color, text string) string {
if log.color {
return ColorBackgroundWrap(color, backgroundColor, text)
}
return text
}
// OpTextWrap OpTextWrap
func (log *Logger) OpTextWrap(color Op, text string) string {
if log.color {
return OpTextWrap(color, text)
}
return text
}
func (log *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line int, level int) {
if level == LogNot {
return
}
if log.prefix != "" {
buf.WriteString(log.prefix)
}
if log.flag&(BitDate|BitTime|BitMicroSeconds|BitLevel) != 0 {
if log.flag&BitLevel != 0 {
buf.WriteString(log.ColorTextWrap(LevelColous[level], Levels[level]+" "))
}
if log.flag&BitDate != 0 {
buf.WriteString(ztime.FormatTime(t, "Y/m/d "))
}
if log.flag&(BitTime|BitMicroSeconds) != 0 {
buf.WriteString(ztime.FormatTime(t, "H:i:s"))
if log.flag&BitMicroSeconds != 0 {
buf.WriteByte('.')
itoa(buf, t.Nanosecond()/1e3, 6) // "12:12:59.123456
}
buf.WriteByte(' ')
}
if log.flag&(BitShortFile|BitLongFile) != 0 {
if log.flag&BitShortFile != 0 {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
}
buf.WriteString(file)
buf.WriteByte(':')
itoa(buf, line, -1)
buf.WriteString(": ")
}
}
}
// outPut Output log
func (log *Logger) outPut(level int, s string, isWrap bool, fn func() func(),
prefixText ...string) error {
log.mu.Lock()
var after func()
if fn != nil {
after = fn()
}
defer func() {
if after != nil {
after()
}
log.mu.Unlock()
}()
if log.out == ioutil.Discard {
return nil
}
isNotLevel := level == LogNot
if log.level < level {
return nil
}
if len(prefixText) > 0 {
s = prefixText[0] + s
}
now := time.Now()
var file string
var line int
if !isNotLevel && (log.flag&(BitShortFile|BitLongFile) != 0) {
var ok bool
_, file, line, ok = runtime.Caller(log.calldDepth)
if !ok {
file = "unknown-file"
line = 0
}
}
log.buf.Reset()
log.formatHeader(&log.buf, now, file, line, level)
log.buf.WriteString(s)
if isWrap && len(s) > 0 && s[len(s)-1] != '\n' {
log.buf.WriteByte('\n')
}
_, err := log.out.Write(log.buf.Bytes())
return err
}
// Printf Printf
func (log *Logger) Printf(format string, v ...interface{}) {
_ = log.outPut(LogNot, fmt.Sprintf(format+"\r\n", v...), false, nil)
}
// Println Println
func (log *Logger) Println(v ...interface{}) {
_ = log.outPut(LogNot, fmt.Sprintln(v...), true, nil)
}
// Debugf Debugf
func (log *Logger) Debugf(format string, v ...interface{}) {
_ = log.outPut(LogDebug, fmt.Sprintf(format, v...), false, nil)
}
// Debug Debug
func (log *Logger) Debug(v ...interface{}) {
_ = log.outPut(LogDebug, fmt.Sprintln(v...), true, nil)
}
// Dump Dump
func (log *Logger) Dump(v ...interface{}) {
if log.level < LogDump {
return
}
args := formatArgs(v...)
_, file, line, ok := callerName(1)
if ok {
names, err := argNames(file, line)
if err == nil {
args = prependArgName(names, args)
}
}
_ = log.outPut(LogDump, fmt.Sprintln(args...), true, nil)
}
// Successf Successf
func (log *Logger) Successf(format string, v ...interface{}) {
_ = log.outPut(LogSuccess, fmt.Sprintf(format, v...), false, nil)
}
// Success Success
func (log *Logger) Success(v ...interface{}) {
_ = log.outPut(LogSuccess, fmt.Sprintln(v...), true, nil)
}
// Infof Infof
func (log *Logger) Infof(format string, v ...interface{}) {
_ = log.outPut(LogInfo, fmt.Sprintf(format, v...), false, nil)
}
// Info Info
func (log *Logger) Info(v ...interface{}) {
_ = log.outPut(LogInfo, fmt.Sprintln(v...), true, nil)
}
// Tipsf Tipsf
func (log *Logger) Tipsf(format string, v ...interface{}) {
_ = log.outPut(LogTips, fmt.Sprintf(format, v...), false, nil)
}
// Tips Tips
func (log *Logger) Tips(v ...interface{}) {
_ = log.outPut(LogTips, fmt.Sprintln(v...), true, nil)
}
// Warnf Warnf
func (log *Logger) Warnf(format string, v ...interface{}) {
_ = log.outPut(LogWarn, fmt.Sprintf(format, v...), false, nil)
}
// Warn Warn
func (log *Logger) Warn(v ...interface{}) {
_ = log.outPut(LogWarn, fmt.Sprintln(v...), true, nil)
}
// Errorf Errorf
func (log *Logger) Errorf(format string, v ...interface{}) {
_ = log.outPut(LogError, fmt.Sprintf(format, v...), false, nil)
}
// Error Error
func (log *Logger) Error(v ...interface{}) {
_ = log.outPut(LogError, fmt.Sprintln(v...), true, nil)
}
// Fatalf Fatalf
func (log *Logger) Fatalf(format string, v ...interface{}) {
_ = log.outPut(LogFatal, fmt.Sprintf(format, v...), false, nil)
osExit(1)
}
// Fatal Fatal
func (log *Logger) Fatal(v ...interface{}) {
_ = log.outPut(LogFatal, fmt.Sprintln(v...), true, nil)
osExit(1)
}
// Panicf Panicf
func (log *Logger) Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
_ = log.outPut(LogPanic, fmt.Sprintf(format, s), false, nil)
panic(s)
}
// panic panic
func (log *Logger) Panic(v ...interface{}) {
s := fmt.Sprintln(v...)
_ = log.outPut(LogPanic, s, true, nil)
panic(s)
}
// Stack Stack
func (log *Logger) Stack(v ...interface{}) {
s := fmt.Sprint(v...)
s += "\n"
buf := make([]byte, LogMaxBuf)
n := runtime.Stack(buf, true)
s += string(buf[:n])
s += "\n"
_ = log.outPut(LogError, s, true, nil)
}
// Track Track
func (log *Logger) Track(logTip string, v ...int) {
depth := log.calldDepth
max := 1
l := len(v)
if l == 1 {
max = v[0]
} else if l > 1 {
depth = depth + v[1]
max = v[0]
}
if max == 0 {
max = 9999
}
b := zstring.Buffer()
track := TrackCurrent(max, depth)
b.WriteString(logTip)
b.WriteString("\n")
b.WriteString(strings.Join(track, "\n"))
_ = log.outPut(LogDebug, b.String(), true, nil)
}
func TrackCurrent(max, depth int) (track []string) {
stop := func() bool {
if max == -1 {
return false
}
max--
return max <= -1
}
for skip := depth; ; skip++ {
name, file, line, ok := callerName(skip)
if !ok || stop() {
break
}
track = append(track, fmt.Sprintf("%v:%d %v", file, line, name))
}
return
}
func callerName(skip int) (name, file string, line int, ok bool) {
var pc uintptr
if pc, file, line, ok = runtime.Caller(skip + 1); !ok {
return
}
name = runtime.FuncForPC(pc).Name()
return
}
// GetFlags Get the current log bitmap tag
func (log *Logger) GetFlags() int {
log.mu.Lock()
defer log.mu.Unlock()
return log.flag
}
// ResetFlags Reset the GetFlags bitMap tag bit in the log
func (log *Logger) ResetFlags(flag int) {
log.mu.Lock()
defer log.mu.Unlock()
log.flag = flag
}
// AddFlag Set flag Tags
func (log *Logger) AddFlag(flag int) {
log.mu.Lock()
defer log.mu.Unlock()
log.flag |= flag
}
// SetPrefix Setting log prefix
func (log *Logger) SetPrefix(prefix string) {
log.mu.Lock()
defer log.mu.Unlock()
log.prefix = prefix
}
func (log *Logger) GetPrefix() string {
return log.prefix
}
// SetLogLevel Setting log display level
func (log *Logger) SetLogLevel(level int) {
log.level = level
}
// GetLogLevel Get log display level
func (log *Logger) GetLogLevel() int {
return log.level
}
func itoa(buf *bytes.Buffer, i int, wid int) {
u := uint(i)
if u == 0 && wid <= 1 {
buf.WriteByte('0')
return
}
var b [32]byte
bp := len(b)
for ; u > 0 || wid > 0; u /= 10 {
bp--
wid--
b[bp] = byte(u%10) + '0'
}
for bp < len(b) {
buf.WriteByte(b[bp])
bp++
}
}
func formatArgs(args ...interface{}) []interface{} {
formatted := make([]interface{}, 0, len(args))
for _, a := range args {
s := ColorTextWrap(ColorCyan, sprint(a))
formatted = append(formatted, s)
}
return formatted
}
func sprint(a ...interface{}) string {
return fmt.Sprint(wrap(a, true)...)
}
func wrap(a []interface{}, force bool) []interface{} {
w := make([]interface{}, len(a))
for i, x := range a {
w[i] = formatter{v: reflect.ValueOf(x), force: force}
}
return w
}
func writeByte(w io.Writer, b byte) {
_, _ = w.Write([]byte{b})
}
func prependArgName(names []string, values []interface{}) []interface{} {
vLen := len(values)
nLen := len(names)
prepended := make([]interface{}, vLen)
for i, value := range values {
name := ""
if i < nLen {
name = names[i]
}
if name == "" {
prepended[i] = OpTextWrap(OpBold, value.(string))
continue
}
name = ColorTextWrap(ColorBlue, OpTextWrap(OpBold, name))
prepended[i] = fmt.Sprintf("%s=%s", name, value)
}
return prepended
}