forked from gookit/slog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
131 lines (110 loc) · 2.73 KB
/
write.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 slog
//
// ---------------------------------------------------------------------------
// Do write log message
// ---------------------------------------------------------------------------
//
func (r *Record) logBytes(level Level) {
// Will reduce memory allocation once
// r.Message = strutil.Byte2str(message)
// var buf *bytes.Buffer
// buf = bufferPool.Get().(*bytes.Buffer)
// defer bufferPool.Put(buf)
// r.Buffer = buf
// TODO release on here ??
// defer r.logger.releaseRecord(r)
handlers, ok := r.logger.matchHandlers(level)
if !ok {
return
}
// init record
r.Level = level
r.Init(r.logger.LowerLevelName)
r.logger.mu.Lock()
defer r.logger.mu.Unlock()
// log caller. will alloc 3 times
if r.logger.ReportCaller {
caller, ok := getCaller(r.CallerSkip)
if ok {
r.Caller = &caller
}
}
// do write log message
r.logger.write(level, r, handlers)
// r.Buffer = nil
}
// Init something for record.
func (r *Record) Init(lowerLevelName bool) {
// use lower level name
if lowerLevelName {
r.levelName = r.Level.LowerName()
} else {
r.levelName = r.Level.Name()
}
// init log time
if r.Time.IsZero() {
r.Time = r.logger.TimeClock.Now()
}
// r.microSecond = r.Time.Nanosecond() / 1000
}
//
// ---------------------------------------------------------------------------
// Do write log message
// ---------------------------------------------------------------------------
//
func (l *Logger) matchHandlers(level Level) ([]Handler, bool) {
// alloc: 1 times for match handlers
var matched []Handler
for _, handler := range l.handlers {
if handler.IsHandling(level) {
matched = append(matched, handler)
}
}
return matched, len(matched) > 0
}
func (l *Logger) write(level Level, r *Record, matched []Handler) {
// // alloc: 1 times for match handlers
// var matched []Handler
// for _, handler := range l.handlers {
// if handler.IsHandling(level) {
// matched = append(matched, handler)
// }
// }
//
// // log level is don't match
// if len(matched) == 0 {
// return
// }
//
// // init record
// r.Init(l.LowerLevelName)
// l.mu.Lock()
// defer l.mu.Unlock()
//
// // log caller. will alloc 3 times
// if l.ReportCaller {
// caller, ok := getCaller(l.CallerSkip)
// if ok {
// r.Caller = &caller
// }
// }
// processing log record
for i := range l.processors {
l.processors[i].Process(r)
}
// handling log record
for _, handler := range matched {
if err := handler.Handle(r); err != nil {
printlnStderr("slog: failed to handle log, error: ", err)
}
}
// flush logs on level <= error level.
if level <= ErrorLevel {
l.flushAll() // has locked on Record.logBytes()
}
if level <= PanicLevel {
l.PanicFunc(r)
} else if level <= FatalLevel {
l.Exit(1)
}
}