-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
131 lines (114 loc) · 2.78 KB
/
log.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
package mylog
import (
"fmt"
"go.uber.org/zap"
"runtime/debug"
)
const (
LevelDebug = iota
LevelInfo
LevelWarn
LevelError
LevelPanic
LevelFatal
)
const (
colorDebug = 95
colorInfo = 94
colorWarn = 93
colorError = 91
colorPanic = 91
colorFatal = 91
)
type Logger struct {
log *zap.SugaredLogger
name string
level int
}
func (l *Logger) ErrStack() {
l.Warn(string(debug.Stack()))
}
func (l *Logger) Debugf(format string, a ...interface{}) {
if l.level > LevelDebug {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorDebug, l.name, fmt.Sprintf(format, a...))
l.log.Debug(msg)
}
func (l *Logger) Infof(format string, a ...interface{}) {
if l.level > LevelInfo {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorInfo, l.name, fmt.Sprintf(format, a...))
l.log.Info(msg)
}
func (l *Logger) Warnf(format string, a ...interface{}) {
if l.level > LevelWarn {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorWarn, l.name, fmt.Sprintf(format, a...))
l.log.Warn(msg)
}
func (l *Logger) Errorf(format string, a ...interface{}) {
if l.level > LevelError {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorError, l.name, fmt.Sprintf(format, a...))
l.log.Error(msg)
}
func (l *Logger) Panicf(format string, a ...interface{}) {
if l.level > LevelPanic {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorPanic, l.name, fmt.Sprintf(format, a...))
l.log.Panic(msg)
}
func (l *Logger) Fatalf(format string, a ...interface{}) {
if l.level > LevelFatal {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorFatal, l.name, fmt.Sprintf(format, a...))
l.log.Fatal(msg)
}
func (l *Logger) Debug(a ...interface{}) {
if l.level > LevelDebug {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorDebug, l.name, fmt.Sprintln(a...))
l.log.Debug(msg)
}
func (l *Logger) Info(a ...interface{}) {
if l.level > LevelInfo {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorInfo, l.name, fmt.Sprintln(a...))
l.log.Info(msg)
}
func (l *Logger) Warn(a ...interface{}) {
if l.level > LevelWarn {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorWarn, l.name, fmt.Sprintln(a...))
l.log.Warn(msg)
}
func (l *Logger) Error(a ...interface{}) {
if l.level > LevelError {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorError, l.name, fmt.Sprintln(a...))
l.log.Error(msg)
}
func (l *Logger) Panic(a ...interface{}) {
if l.level > LevelPanic {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorPanic, l.name, fmt.Sprintln(a...))
l.log.Panic(msg)
}
func (l *Logger) Fatal(a ...interface{}) {
if l.level > LevelFatal {
return
}
msg := fmt.Sprintf("\x1b[%dm▶ [%s] %s\x1b[0m", colorFatal, l.name, fmt.Sprintln(a...))
l.log.Fatal(msg)
}