-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
194 lines (159 loc) · 3.64 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
package glog
import (
"fmt"
"io"
std_log "log"
"os"
"path"
"runtime"
)
var _ = std_log.Print
var loggers map[string]*Logger
func init() {
loggers = make(map[string]*Logger)
loggers["default"] = NewLogger(os.Stdout)
}
type Logger struct {
Level Level
Colorful bool
ShowLine bool
Prefix string
StackSkip int
l *std_log.Logger
}
func NewLogger(w io.Writer) *Logger {
return &Logger{
Level: TRACE,
Colorful: true,
ShowLine: true,
StackSkip: 4,
l: std_log.New(w, "", std_log.LstdFlags),
}
}
func NewLoggerBySkip(w io.Writer, skip int) *Logger {
return &Logger{
Level: TRACE,
Colorful: true,
ShowLine: true,
StackSkip: skip,
l: std_log.New(w, "", std_log.LstdFlags),
}
}
func NewNamedLogger(name string, w io.Writer) *Logger {
r := NewLogger(w)
loggers[name] = r
return r
}
func GetLogger(name string) *Logger {
l, ok := loggers[name]
if !ok {
Fatalf("can't find logger '%s'", name)
return loggers["default"]
}
return l
}
func color(col, s string) string {
if col == "" {
return s
}
return "\x1b[0;" + col + "m" + s + "\x1b[0m"
}
func (l *Logger) SetPrefix(prefix string) *Logger {
l.Prefix = prefix
return l
}
func (l *Logger) Skip(skip int) *Logger {
nl := *l
nl.StackSkip = skip
return &nl
}
func (l *Logger) getPrefix(level Level) string {
s := level.String()
if l.Colorful {
s = color(level.Color(), s)
}
s = "[" + s + "]"
if l.Prefix != "" {
s = s + " " + l.Prefix
}
return s
}
func (l *Logger) getPosix() string {
if !l.ShowLine {
return ""
}
_, file, line, ok := runtime.Caller(l.StackSkip)
if !ok {
return ""
}
file = path.Base(file)
return fmt.Sprintf("[%s:%d]", file, line)
}
func (l *Logger) Output(level Level, format string, a ...interface{}) {
if level < l.Level {
return
}
s := ""
if len(a) != 0 {
s = fmt.Sprintf(format, a...)
} else {
s = fmt.Sprint(format)
}
content := l.getPrefix(level) + " " + s + " " + l.getPosix()
std_log.Println(content)
}
func (l *Logger) Printf(format string, a ...interface{}) {
l.Output(TRACE, format, a...)
}
func (l *Logger) Tracef(format string, a ...interface{}) {
l.Output(TRACE, format, a...)
}
func (l *Logger) Debugf(format string, a ...interface{}) {
l.Output(DEBUG, format, a...)
}
func (l *Logger) Infof(format string, a ...interface{}) {
l.Output(INFO, format, a...)
}
func (l *Logger) Warnf(format string, a ...interface{}) {
l.Output(WARN, format, a...)
}
func (l *Logger) Errorf(format string, a ...interface{}) {
l.Output(ERROR, format, a...)
}
func (l *Logger) Fatalf(format string, a ...interface{}) {
l.Output(FATAL, format, a...)
os.Exit(1)
}
func Printf(format string, a ...interface{}) {
loggers["default"].Printf(format, a...)
}
func Tracef(format string, a ...interface{}) {
loggers["default"].Tracef(format, a...)
}
func Debugf(format string, a ...interface{}) {
loggers["default"].Debugf(format, a...)
}
func Infof(format string, a ...interface{}) {
loggers["default"].Infof(format, a...)
}
func Warnf(format string, a ...interface{}) {
loggers["default"].Warnf(format, a...)
}
func Errorf(format string, a ...interface{}) {
loggers["default"].Errorf(format, a...)
}
func Fatalf(format string, a ...interface{}) {
loggers["default"].Fatalf(format, a...)
}
func DefaultLogger() *Logger {
return loggers["default"]
}
func ErrorfWithoutReport(err string) {
std_log.Println(fmt.Sprintf("[%s] %s", color(levelColor[ERROR], "ERROR"), err))
}
func TracefWithoutReport(err string) {
std_log.Println(fmt.Sprintf("[%s] %s", color(levelColor[TRACE], "TRACE"), err))
}
func InfofWithoutReport(err string) {
std_log.Println(fmt.Sprintf("[%s] %s", color(levelColor[INFO], "INFO"), err))
}