forked from YoungPioneers/blog4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog4go.go
492 lines (407 loc) · 11 KB
/
blog4go.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
// Copyright (c) 2015, huangjunwei <[email protected]>. All rights reserved.
// Package blog4go provide an efficient and easy-to-use writers library for
// logging into files, console or sockets. Writers suports formatting
// string filtering and calling user defined hook in asynchronous mode.
package blog4go
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strings"
"sync"
)
const (
// EOL end of a line
EOL = '\n'
// ESCAPE escape character
ESCAPE = '\\'
// PLACEHOLDER placeholder
PLACEHOLDER = '%'
)
var (
// blog is the singleton instance use for blog.write/writef
blog Writer
// global mutex log used for singlton
singltonLock *sync.Mutex
// DefaultBufferSize bufio buffer size
DefaultBufferSize = 4096 // default memory page size
// ErrInvalidFormat invalid format error
ErrInvalidFormat = errors.New("Invalid format type")
// ErrAlreadyInit show that blog is already initialized once
ErrAlreadyInit = errors.New("blog4go has been already initialized")
)
// Writer interface is a common definition of any writers in this package.
// Any struct implements Writer interface must implement functions below.
// Close is used for close the writer and free any elements if needed.
// write is an internal function that write pure message with specific
// logging level.
// writef is an internal function that formatting message with specific
// logging level. Placeholders in the format string will be replaced with
// args given.
// Both write and writef may have an asynchronous call of user defined
// function before write and writef function end..
type Writer interface {
// Close do anything end before program end
Close()
// Level return logging level threshold
Level() Level
// SetLevel set logging level threshold
SetLevel(level Level)
// write/writef functions with different levels
write(level Level, format string)
writef(level Level, format string, args ...interface{})
Debug(format string)
Debugf(format string, args ...interface{})
Trace(format string)
Tracef(format string, args ...interface{})
Info(format string)
Infof(format string, args ...interface{})
Warn(format string)
Warnf(format string, args ...interface{})
Error(format string)
Errorf(format string, args ...interface{})
Critical(format string)
Criticalf(format string, args ...interface{})
// flush log to disk
flush()
// hook
SetHook(hook Hook)
SetHookLevel(level Level)
// logrotate
SetTimeRotated(timeRotated bool)
SetRotateSize(rotateSize ByteSize)
SetRotateLines(rotateLines int)
SetRetentions(retentions int64)
SetColored(colored bool)
}
func init() {
singltonLock = new(sync.Mutex)
DefaultBufferSize = os.Getpagesize()
}
// NewWriterFromConfigAsFile initialize a writer according to given config file
// configFile must be the path to the config file
func NewWriterFromConfigAsFile(configFile string) (err error) {
singltonLock.Lock()
defer singltonLock.Unlock()
if nil != blog {
return
}
// read config from file
config, err := readConfig(configFile)
if nil != err {
return
}
multiWriter := new(MultiWriter)
multiWriter.level = DEBUG
if level := LevelFromString(config.MinLevel); level.valid() {
multiWriter.level = level
}
multiWriter.closed = false
multiWriter.writers = make(map[Level]Writer)
for _, filter := range config.Filters {
var rotate = false
var isSocket = false
var file *os.File
var blog *BLog
var fileLock *sync.Mutex
// get file path
var filePath string
if nil != &filter.File && "" != filter.File.Path {
// file do not need logrotate
filePath = filter.File.Path
rotate = false
file, err = os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(0644))
if nil != err {
return err
}
blog = NewBLog(file)
fileLock = new(sync.Mutex)
} else if nil != &filter.RotateFile && "" != filter.RotateFile.Path {
// file need logrotate
filePath = filter.RotateFile.Path
rotate = true
file, err = os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(0644))
if nil != err {
return err
}
blog = NewBLog(file)
fileLock = new(sync.Mutex)
} else if nil != &filter.Socket && "" != filter.Socket.Address && "" != filter.Socket.Network {
isSocket = true
} else {
// config error
return ErrFilePathNotFound
}
levels := strings.Split(filter.Levels, ",")
for _, levelStr := range levels {
var level Level
if level = LevelFromString(levelStr); !level.valid() {
return ErrInvalidLevel
}
if isSocket {
// socket writer
writer, err := newSocketWriter(filter.Socket.Network, filter.Socket.Address)
if nil != err {
return err
}
multiWriter.writers[level] = writer
continue
}
// init a base file writer
writer, err := newBaseFileWriter(filePath, rotate)
if nil != err {
return err
}
if rotate {
// set logrotate strategy
if TypeTimeBaseRotate == filter.RotateFile.Type {
writer.SetTimeRotated(true)
writer.SetRetentions(filter.RotateFile.Retentions)
} else if TypeSizeBaseRotate == filter.RotateFile.Type {
writer.SetRotateSize(filter.RotateFile.RotateSize)
writer.SetRotateLines(filter.RotateFile.RotateLines)
writer.SetRetentions(filter.RotateFile.Retentions)
} else {
return ErrInvalidRotateType
}
}
writer.file = file
writer.blog = blog
writer.lock = fileLock
// set color
multiWriter.SetColored(filter.Colored)
multiWriter.writers[level] = writer
}
}
blog = multiWriter
return
}
// BLog struct is a threadsafe log writer inherit bufio.Writer
type BLog struct {
// logging level
// every message level exceed this level will be written
level Level
// input io
in io.Writer
// bufio.Writer object of the input io
writer *bufio.Writer
// exclusive lock while calling write function of bufio.Writer
lock *sync.Mutex
// closed tag
closed bool
}
// NewBLog create a BLog instance and return the pointer of it.
// fileName must be an absolute path to the destination log file
func NewBLog(in io.Writer) (blog *BLog) {
blog = new(BLog)
blog.in = in
blog.level = DEBUG
blog.lock = new(sync.Mutex)
blog.closed = false
blog.writer = bufio.NewWriterSize(in, DefaultBufferSize)
return
}
// write writes pure message with specific level
func (blog *BLog) write(level Level, format string) int {
blog.lock.Lock()
defer blog.lock.Unlock()
// 统计日志size
var size = 0
blog.writer.Write(timeCache.format)
blog.writer.WriteString(level.prefix())
blog.writer.WriteString(format)
blog.writer.WriteByte(EOL)
size = len(timeCache.format) + len(level.prefix()) + len(format) + 1
return size
}
// write formats message with specific level and write it
func (blog *BLog) writef(level Level, format string, args ...interface{}) int {
// 格式化构造message
// 边解析边输出
// 使用 % 作占位符
blog.lock.Lock()
defer blog.lock.Unlock()
// 统计日志size
var size = 0
// 识别占位符标记
var tag = false
var tagPos int
// 转义字符标记
var escape = false
// 在处理的args 下标
var n int
// 未输出的,第一个普通字符位置
var last int
var s int
blog.writer.Write(timeCache.format)
blog.writer.WriteString(level.prefix())
size += len(timeCache.format) + len(level.prefix())
for i, v := range format {
if tag {
switch v {
case 'd', 'f', 'v', 'b', 'o', 'x', 'X', 'c', 'p', 't', 's', 'T', 'q', 'U', 'e', 'E', 'g', 'G':
if escape {
escape = false
}
s, _ = blog.writer.WriteString(fmt.Sprintf(format[tagPos:i+1], args[n]))
size += s
n++
last = i + 1
tag = false
//转义符
case ESCAPE:
if escape {
blog.writer.WriteByte(ESCAPE)
size++
}
escape = !escape
//默认
default:
}
} else {
// 占位符,百分号
if PLACEHOLDER == format[i] && !escape {
tag = true
tagPos = i
s, _ = blog.writer.WriteString(format[last:i])
size += s
escape = false
}
}
}
blog.writer.WriteString(format[last:])
blog.writer.WriteByte(EOL)
size += len(format[last:]) + 1
return size
}
// Flush flush buffer to disk
func (blog *BLog) flush() {
blog.lock.Lock()
defer blog.lock.Unlock()
if blog.closed {
return
}
blog.writer.Flush()
}
// Close close file writer
func (blog *BLog) Close() {
blog.lock.Lock()
defer blog.lock.Unlock()
if blog.closed {
return
}
blog.closed = true
blog.writer.Flush()
blog.writer = nil
}
// In return the input io.Writer
func (blog *BLog) In() io.Writer {
return blog.in
}
// Level return logging level threshold
func (blog *BLog) Level() Level {
return blog.level
}
// SetLevel set logging level threshold
func (blog *BLog) SetLevel(level Level) *BLog {
blog.level = level
return blog
}
// resetFile resets file descriptor of the writer with specific file name
func (blog *BLog) resetFile(in io.Writer) (err error) {
blog.lock.Lock()
defer blog.lock.Unlock()
blog.writer.Flush()
blog.in = in
blog.writer.Reset(in)
return
}
// SetHook set hook for logging action
func SetHook(hook Hook) {
blog.SetHook(hook)
}
// SetHookLevel set when hook will be called
func SetHookLevel(level Level) {
blog.SetHookLevel(level)
}
// SetColored set logging color
func SetColored(colored bool) {
blog.SetColored(colored)
}
// SetTimeRotated toggle time base logrotate on the fly
func SetTimeRotated(timeRotated bool) {
blog.SetTimeRotated(timeRotated)
}
// SetRetentions set how many logs will keep after logrotate
func SetRetentions(retentions int64) {
blog.SetRetentions(retentions)
}
// SetRotateSize set size when logroatate
func SetRotateSize(rotateSize ByteSize) {
blog.SetRotateSize(rotateSize)
}
// SetRotateLines set line number when logrotate
func SetRotateLines(rotateLines int) {
blog.SetRotateLines(rotateLines)
}
// Flush flush logs to disk
func Flush() {
blog.flush()
}
// Debug static function for Debug
func Debug(format string) {
blog.Debug(format)
}
// Debugf static function for Debugf
func Debugf(format string, args ...interface{}) {
blog.Debugf(format, args...)
}
// Trace static function for Trace
func Trace(format string) {
blog.Trace(format)
}
// Tracef static function for Tracef
func Tracef(format string, args ...interface{}) {
blog.Tracef(format, args...)
}
// Info static function for Info
func Info(format string) {
blog.Info(format)
}
// Infof static function for Infof
func Infof(format string, args ...interface{}) {
blog.Infof(format, args...)
}
// Warn static function for Warn
func Warn(format string) {
blog.Warn(format)
}
// Warnf static function for Warnf
func Warnf(format string, args ...interface{}) {
blog.Warnf(format, args...)
}
// Error static function for Error
func Error(format string) {
blog.Error(format)
}
// Errorf static function for Errorf
func Errorf(format string, args ...interface{}) {
blog.Errorf(format, args...)
}
// Critical static function for Critical
func Critical(format string) {
blog.Critical(format)
}
// Criticalf static function for Criticalf
func Criticalf(format string, args ...interface{}) {
blog.Criticalf(format, args...)
}
// Close close the logger
func Close() {
singltonLock.Lock()
defer singltonLock.Unlock()
blog.Close()
blog = nil
}