-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
206 lines (176 loc) · 3.59 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
package logger
import (
"fmt"
"os"
"runtime"
"sync"
"sync/atomic"
"time"
)
type LogMsg struct {
Level LogLevel
Timestamp time.Time
Msg string
FileName string
FileLine int
}
type Logger struct {
level LogLevel
callDepth int
signal chan int
msgBuffer chan *LogMsg
msgPool *sync.Pool
wg sync.WaitGroup
executors []Executor
formatter Formatter
isRunning int32
}
func NewLogger() *Logger {
return &Logger{
level: LevelInfo,
signal: make(chan int),
msgBuffer: make(chan *LogMsg, LogMsgBufferSize),
msgPool: &sync.Pool{New: func() interface{} {
return &LogMsg{}
}},
isRunning: 0,
}
}
func NewDefaultLogger() *Logger {
logger := NewLogger()
logger.AppendExecutor(NewConsoleExecutor())
return logger
}
func (l *Logger) AppendExecutor(executor Executor) {
l.executors = append(l.executors, executor)
}
func (l *Logger) ResetExecutor() {
l.executors = make([]Executor, 0)
}
func (l *Logger) SetFormatter(formatter Formatter) {
l.formatter = formatter
}
func (l *Logger) GetLevel() LogLevel {
return l.level
}
func (l *Logger) SetLevel(level LogLevel) {
l.level = level
}
func (l *Logger) GetCallDepth() int {
return l.callDepth
}
func (l *Logger) SetCallDepth(callDepth int) {
l.callDepth = callDepth
}
func (l *Logger) write(msg *LogMsg) {
if len(l.executors) == 0 {
return
}
if l.formatter == nil {
l.formatter = &DefaultFormatter{}
}
for _, executorItem := range l.executors {
if err := executorItem.WriteMsg(l.formatter.FormatMsg(msg)); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to save log to destination, err:%v\n", err)
}
}
}
func (l *Logger) WriteMsg(msg string, level LogLevel) {
if atomic.LoadInt32(&l.isRunning) == 0 {
return
}
var fileName string
var fileLine int
if l.callDepth > 0 {
_, file, line, ok := runtime.Caller(l.callDepth)
if ok {
fileName = file
fileLine = line
}
}
logMsg := l.msgPool.Get().(*LogMsg)
logMsg.Level = level
logMsg.Msg = msg
logMsg.Timestamp = time.Now()
logMsg.FileName = fileName
logMsg.FileLine = fileLine
l.msgBuffer <- logMsg
}
func (l *Logger) Close() {
if !atomic.CompareAndSwapInt32(&l.isRunning, 1, 0) {
return
}
l.signal <- 1
l.wg.Wait()
close(l.msgBuffer)
close(l.signal)
// close all
for _, executorItem := range l.executors {
executorItem.Close()
}
}
func (l *Logger) Flush() {
for {
if len(l.msgBuffer) == 0 {
break
}
msg, ok := <-l.msgBuffer
if !ok {
break
}
l.write(msg)
}
for _, executorItem := range l.executors {
executorItem.Flush()
}
}
func (l *Logger) Start() {
if !atomic.CompareAndSwapInt32(&l.isRunning, 0, 1) {
return
}
workerFunc := func() {
defer l.wg.Done()
for {
select {
case msg := <-l.msgBuffer:
l.write(msg)
l.msgPool.Put(msg)
case <-l.signal:
l.Flush()
return
}
}
}
l.wg.Add(1)
go workerFunc()
}
func (l *Logger) Debug(format string, v ...interface{}) {
if l.GetLevel() > LevelDebug {
return
}
l.WriteMsg(fmt.Sprintf(format, v...), LevelDebug)
}
func (l *Logger) Info(format string, v ...interface{}) {
if l.GetLevel() > LevelInfo {
return
}
l.WriteMsg(fmt.Sprintf(format, v...), LevelInfo)
}
func (l *Logger) Warn(format string, v ...interface{}) {
if l.GetLevel() > LevelWarn {
return
}
l.WriteMsg(fmt.Sprintf(format, v...), LevelWarn)
}
func (l *Logger) Error(format string, v ...interface{}) {
if l.GetLevel() > LevelError {
return
}
l.WriteMsg(fmt.Sprintf(format, v...), LevelError)
}
func (l *Logger) Fatal(format string, v ...interface{}) {
if l.GetLevel() > LevelFatal {
return
}
l.WriteMsg(fmt.Sprintf(format, v...), LevelFatal)
}