-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
295 lines (259 loc) · 5.98 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
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
package bilog
import (
"io"
"strconv"
"sync"
"time"
)
// 测试版本
/*
bilog 高性能思路:复用string参数的内存,copy但不重新分配
prefix的内存也复用,拷贝但不分配,reset使用unsafe修改len
*/
const (
DEFAULT_TOP_BUFFER_SIZE = 256
DEFAULT_LOW_BUFFER_SIZE = DEFAULT_TOP_BUFFER_SIZE * 6
TIME_BUF_SIZE = 64
CALLER_BUF_SIZE = 64
)
// 缓存特殊的字符
var (
cacheEntry byte = '\n'
cacheSplit byte = ':'
cacheSpace byte = ' '
)
// SimpleLogger atomic flag 会有公平性的问题
type SimpleLogger struct {
mu sync.Mutex
// 缓存和生产date数据的工厂
factory *TimeFactory
// 设置的输出等级
level level
// config
confObj *loggerConfig
// 缓存level对应的string,避免频繁拷贝和分配
// 必须按照doc.go中定义的日志等级来初始化
levelCache []string
write io.Writer
// 缓存格式化后的时间
timeBuf []byte
// 缓存的纳秒级时间戳
timeStamp int64
// 虽然runtime.Caller提供的file string已经逃逸到堆中,不用多次一举去拷贝
// 该buffer是主要为了line而提供的
callerBuf []byte
//TODO:计划删除
//prefix string
// 顶层缓冲区
topBuf []byte
// 底层缓冲区
lowBuf []byte
}
func NewLogger(write io.Writer, l level, options ...options) *SimpleLogger {
var cf loggerConfig
if options == nil || len(options) == 0 {
WithDefault().apply(&cf)
} else {
for _, option := range options {
option.apply(&cf)
}
}
var factory *TimeFactory
if cf.tt.start {
factory = timeFactory
factory.Start()
}
return &SimpleLogger{
level: l,
write: write,
levelCache: []string{
"[INFO] ", "[DEBUG] ", "[TRACE] ", "[ERROR] ",
"[PANIC] ",
},
confObj: &cf,
callerBuf: make([]byte, 0, CALLER_BUF_SIZE),
topBuf: make([]byte, cf.topBufferSize),
lowBuf: make([]byte, 0, cf.lowBufferSize),
timeBuf: make([]byte, 0, TIME_BUF_SIZE),
factory: factory,
}
}
// SetPrefix 复用string的内存来避免memove
//func (l *SimpleLogger) SetPrefix(s string) {
// //for !atomic.CompareAndSwapInt32(l.flas, NoWriter,Writer) {}
// l.mu.Lock()
// defer l.mu.Unlock()
// l.prefix = s
//}
// TODO优化转换速度
func (l *SimpleLogger) fastConvert() {
// 比较新老时间戳,如果还在有效时间之内则不更新timeBuf里的内容,减少memmove次数
timeStamp := l.factory.TimeStamp()
if !(timeStamp-l.timeStamp > int64(time.Millisecond*10)) && len(l.timeBuf) > 0 {
l.timeStamp = timeStamp
return
} else {
l.timeStamp = timeStamp
l.resetTimeBuf()
date := l.factory.Get()
l.timeBuf = append(l.timeBuf, date...)
}
}
// 重置用于保存时间的缓冲区
func (l *SimpleLogger) resetTimeBuf() {
l.timeBuf = l.timeBuf[:0]
}
// 写入到低层缓冲器,该函数会有一些检查
func (l *SimpleLogger) writeLowBuf() {
// 使用No-Buffer时直接输出
if l.confObj.lowBufferSize == 0 {
writeHandle(l.write, l.topBuf)
return
}
if len(l.lowBuf)+len(l.topBuf) > cap(l.lowBuf) {
l.flushLowBuf()
}
l.lowBuf = append(l.lowBuf, l.topBuf...)
}
// 将lowBuf中的数据全部写入到writer中并reset
func (l *SimpleLogger) flushLowBuf() {
writeHandle(l.write, l.lowBuf)
l.resetLowBuf()
}
// 重置用于输出的缓冲区
func (l *SimpleLogger) resetTopBuf() {
l.topBuf = l.topBuf[:0]
}
// 重置lowBuf
func (l *SimpleLogger) resetLowBuf() {
l.lowBuf = l.lowBuf[:0]
}
func (l *SimpleLogger) printTime(level level) {
l.resetTopBuf()
// 获取日志的时间
if l.confObj.tt.start {
l.fastConvert()
}
l.topBuf = append(l.topBuf, l.levelCache[level]...)
l.topBuf = append(l.topBuf, l.timeBuf...)
}
// 在最顶层调用以提升性能
func (l *SimpleLogger) printCaller() {
// reset
l.callerBuf = l.callerBuf[:0]
file, line := CallerOfConcurrentCache(l.confObj.st.skip)
l.callerBuf = append(l.callerBuf, file...)
l.callerBuf = append(l.callerBuf, cacheSplit)
l.callerBuf = append(l.callerBuf, strconv.Itoa(line)...)
}
func (l *SimpleLogger) print(s string, level level) {
l.mu.Lock()
defer l.mu.Unlock()
l.printTime(level)
l.topBuf = append(l.topBuf, s...)
l.writeLowBuf()
}
func (l *SimpleLogger) println(s string, level level) {
l.resetTopBuf()
l.printTime(level)
l.topBuf = append(l.topBuf, l.callerBuf...)
l.topBuf = append(l.topBuf, cacheSpace)
l.topBuf = append(l.topBuf, s...)
l.topBuf = append(l.topBuf, cacheEntry)
l.writeLowBuf()
}
// 验证输出的日志级别是否小于等于预设的级别
func (l *SimpleLogger) checkLevel(level level) bool {
return level <= l.level
}
func (l *SimpleLogger) Level() int {
return int(l.level)
}
func (l *SimpleLogger) Info(s string) {
if !l.checkLevel(INFO) {
return
}
l.mu.Lock()
defer l.mu.Unlock()
if l.confObj.st.start {
l.printCaller()
}
l.println(s, INFO)
}
func (l *SimpleLogger) Debug(s string) {
if !l.checkLevel(DEBUG) {
return
}
l.mu.Lock()
defer l.mu.Unlock()
if l.confObj.st.start {
l.printCaller()
}
l.println(s, DEBUG)
}
func (l *SimpleLogger) Trace(s string) {
if !l.checkLevel(TRACE) {
return
}
l.mu.Lock()
defer l.mu.Unlock()
if l.confObj.st.start {
l.printCaller()
}
l.println(s, TRACE)
}
func (l *SimpleLogger) ErrorFromErr(e error) {
if !l.checkLevel(ERROR) {
return
}
l.mu.Lock()
defer l.mu.Unlock()
if l.confObj.st.start {
l.printCaller()
}
if e == nil {
l.println("nil", ERROR)
} else {
l.println(e.Error(), ERROR)
}
}
func (l *SimpleLogger) ErrorFromString(s string) {
if !l.checkLevel(ERROR) {
return
}
l.mu.Lock()
defer l.mu.Unlock()
if l.confObj.st.start {
l.printCaller()
}
l.println(s, ERROR)
}
func (l *SimpleLogger) PanicFromErr(e error) {
if !l.checkLevel(PANIC) {
return
}
l.mu.Lock()
defer l.mu.Unlock()
if l.confObj.st.start {
l.printCaller()
}
if e == nil {
l.println("nil", PANIC)
} else {
l.println(e.Error(), PANIC)
}
}
func (l *SimpleLogger) PanicFromString(s string) {
if !l.checkLevel(PANIC) {
return
}
l.mu.Lock()
defer l.mu.Unlock()
if l.confObj.st.start {
l.printCaller()
}
l.println(s, PANIC)
}
func (l *SimpleLogger) Flush() {
l.flushLowBuf()
}