-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
275 lines (232 loc) · 4.76 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
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
// Copyright 2019 The vogo Authors. All rights reserved.
// author: wongoo
package logger
import (
"fmt"
"io"
"os"
"runtime"
"sync"
"time"
)
const (
TagTrace = "TRAC"
TagDebug = "DEBG"
TagInfo = "INFO"
TagWarn = "WARN"
TagError = "ERRO"
TagFatal = "FATL"
TagPanic = "PNIC"
TagPrint = "PRNT"
LevelTrace = 5
LevelDebug = 4
LevelInfo = 3
LevelWarn = 2
LevelError = 1
LevelFatal = 0
)
const (
Lnone = 0 // none file
Lfile = 1 // d.go:23
Lfunc = 1 << 1 // foo
LfileFunc = Lfunc | Lfile // d.go:foo:23
)
var (
Level = LevelInfo
output io.Writer = os.Stdout
flag int
)
// SetLevel set logger Level
// the Level variable is exported and can be set directly.
func SetLevel(l int) {
Level = l
}
// SetOutput set logger output writer
func SetOutput(w io.Writer) {
output = w
}
// SetFlags set logger flags
func SetFlags(f int) {
flag = f
}
// Writer return the logger writer
func Writer() io.Writer {
return output
}
func Trace(a ...any) {
if Level < LevelTrace {
return
}
WriteLog(TagTrace, fmt.Sprint(a...))
}
func Debug(a ...any) {
if Level < LevelDebug {
return
}
WriteLog(TagDebug, fmt.Sprint(a...))
}
func Info(a ...any) {
if Level < LevelInfo {
return
}
WriteLog(TagInfo, fmt.Sprint(a...))
}
func Warn(a ...any) {
if Level < LevelWarn {
return
}
WriteLog(TagWarn, fmt.Sprint(a...))
}
func Error(a ...any) {
if Level < LevelError {
return
}
WriteLog(TagError, fmt.Sprint(a...))
}
func Tracef(format string, a ...any) {
if Level < LevelTrace {
return
}
WriteLog(TagTrace, fmt.Sprintf(format, a...))
}
func Debugf(format string, a ...any) {
if Level < LevelDebug {
return
}
WriteLog(TagDebug, fmt.Sprintf(format, a...))
}
func Infof(format string, a ...any) {
if Level < LevelInfo {
return
}
WriteLog(TagInfo, fmt.Sprintf(format, a...))
}
func Warnf(format string, a ...any) {
if Level < LevelWarn {
return
}
WriteLog(TagWarn, fmt.Sprintf(format, a...))
}
func Errorf(format string, a ...any) {
if Level < LevelError {
return
}
WriteLog(TagError, fmt.Sprintf(format, a...))
}
func Fatal(a ...any) {
WriteLog(TagFatal, fmt.Sprint(a...))
os.Exit(1)
}
func Fatalf(format string, a ...any) {
WriteLog(TagFatal, fmt.Sprintf(format, a...))
os.Exit(1)
}
func Fatalln(a ...any) {
WriteLog(TagFatal, fmt.Sprint(a...))
os.Exit(1)
}
func Print(a ...any) {
WriteLog(TagPrint, fmt.Sprint(a...))
}
func Printf(format string, a ...any) {
WriteLog(TagPrint, fmt.Sprintf(format, a...))
}
func Println(format string, a ...any) {
WriteLog(TagPrint, fmt.Sprintf(format, a...))
}
func Panic(a ...any) {
s := fmt.Sprint(a...)
WriteLog(TagPanic, s)
panic(s)
}
func Panicf(format string, a ...any) {
s := fmt.Sprintf(format, a...)
WriteLog(TagPanic, s)
panic(s)
}
func Panicln(a ...any) {
s := fmt.Sprint(a...)
WriteLog(TagPanic, s)
panic(s)
}
var (
bytesPool = sync.Pool{New: func() any {
b := make([]byte, 0, 1024)
return &b
}}
)
// WriteLog write log data
func WriteLog(tag, s string) {
t := time.Now()
buf := bytesPool.Get().(*[]byte)
year, month, day := t.Date()
appendNumber(buf, year, 4)
*buf = append(*buf, '/')
appendNumber(buf, int(month), 2)
*buf = append(*buf, '/')
appendNumber(buf, day, 2)
*buf = append(*buf, ' ')
hour, min, sec := t.Clock()
appendNumber(buf, hour, 2)
*buf = append(*buf, ':')
appendNumber(buf, min, 2)
*buf = append(*buf, ':')
appendNumber(buf, sec, 2)
*buf = append(*buf, '.')
appendNumber(buf, t.Nanosecond()/1e6, 3)
*buf = append(*buf, ' ')
*buf = append(*buf, tag...)
if flag&Lfile != 0 {
*buf = append(*buf, ' ', '[')
pc, fileName, line, callerOk := runtime.Caller(2)
if callerOk {
for i := len(fileName) - 1; i > 0; i-- {
if fileName[i] == '/' {
fileName = fileName[i+1:]
break
}
}
*buf = append(*buf, fileName...)
if flag&Lfunc != 0 {
funcName := runtime.FuncForPC(pc).Name() // main.(*MyStruct).foo
for i := len(funcName) - 1; i > 0; i-- {
if funcName[i] == '.' {
funcName = funcName[i+1:]
break
}
}
*buf = append(*buf, ':')
*buf = append(*buf, funcName...)
}
*buf = append(*buf, ':')
appendNumber(buf, line, -1)
} else {
*buf = append(*buf, '?')
}
*buf = append(*buf, ']')
}
*buf = append(*buf, ' ')
*buf = append(*buf, s...)
if s == "" || s[len(s)-1] != '\n' {
*buf = append(*buf, '\n')
}
_, _ = output.Write(*buf)
*buf = (*buf)[:0]
bytesPool.Put(buf)
}
// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding.
func appendNumber(buf *[]byte, i, wid int) {
// Assemble decimal in reverse order.
var b [20]byte
bp := len(b) - 1
for i >= 10 || wid > 1 {
wid--
q := i / 10
b[bp] = byte('0' + i - q*10)
bp--
i = q
}
// i < 10
b[bp] = byte('0' + i)
*buf = append(*buf, b[bp:]...)
}