This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
317 lines (263 loc) · 6.08 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package log4go
import (
"fmt"
"log"
"path"
"runtime"
"strconv"
"sync"
"time"
)
var (
LevelFlags = [...]string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
recordPool *sync.Pool
)
const (
DEBUG = iota
INFO
WARNING
ERROR
FATAL
)
const (
tunnelSizeDefault = 2048
)
// Record record struct
type Record struct {
time string
code string
info string
level int
}
// String record string
func (r *Record) String() string {
return fmt.Sprintf("%s [%s] <%s> %s\n", r.time, LevelFlags[r.level], r.code, r.info)
}
// Writer writer interface
type Writer interface {
Init() error
Write(*Record) error
}
// Rotater rotate interface
type Rotater interface {
Rotate() error
SetPathPattern(string) error
}
// Flusher flush interface
type Flusher interface {
Flush() error
}
// Logger log struct
type Logger struct {
writers []Writer
tunnel chan *Record
level int // input level, valid minimum global level
lastTime int64
lastTimeStr string
c chan bool
layout string
fullPath bool // show full path, default only show file:line_number
lock sync.RWMutex
}
// NewLogger create the logger instance
func NewLogger() *Logger {
if loggerDefault != nil && !takeUP {
takeUP = true
return loggerDefault
}
l := new(Logger)
l.writers = make([]Writer, 0, 2)
l.tunnel = make(chan *Record, tunnelSizeDefault)
l.c = make(chan bool, 1)
l.layout = "2006/01/02 15:04:05"
go bootstrapLogWriter(l)
return l
}
// Register register logger writer
func (l *Logger) Register(w Writer) {
if err := w.Init(); err != nil {
panic(err)
}
l.writers = append(l.writers, w)
}
// SetLevel Logger set default level
func (l *Logger) SetLevel(lvl int) {
l.level = lvl
}
// SetLayout Logger set the time data format, layout
func (l *Logger) SetLayout(layout string) {
l.layout = layout
}
// Debug Logger deliver record to writer
func (l *Logger) Debug(fmt string, args ...interface{}) {
l.deliverRecordToWriter(DEBUG, fmt, args...)
}
// Warn Logger deliver record to writer
func (l *Logger) Warn(fmt string, args ...interface{}) {
l.deliverRecordToWriter(WARNING, fmt, args...)
}
// Info Logger deliver record to writer
func (l *Logger) Info(fmt string, args ...interface{}) {
l.deliverRecordToWriter(INFO, fmt, args...)
}
// Error Logger deliver record to writer
func (l *Logger) Error(fmt string, args ...interface{}) {
l.deliverRecordToWriter(ERROR, fmt, args...)
}
// Fatal Logger deliver record to writer
func (l *Logger) Fatal(fmt string, args ...interface{}) {
l.deliverRecordToWriter(FATAL, fmt, args...)
}
// Close Logger close buffer, flush and stop logger
func (l *Logger) Close() {
close(l.tunnel)
<-l.c
for _, w := range l.writers {
if f, ok := w.(Flusher); ok {
if err := f.Flush(); err != nil {
log.Println(err)
}
}
}
}
func (l *Logger) deliverRecordToWriter(level int, format string, args ...interface{}) {
var inf, code string
if level < l.level {
return
}
if format != "" {
inf = fmt.Sprintf(format, args...)
} else {
inf = fmt.Sprint(args...)
}
// source code, file and line num
_, file, line, ok := runtime.Caller(2)
if ok {
if l.fullPath {
code = file + ":" + strconv.Itoa(line)
} else {
code = path.Base(file) + ":" + strconv.Itoa(line)
}
}
// format time
now := time.Now()
l.lock.Lock() // avoid data race
if now.Unix() != l.lastTime {
l.lastTime = now.Unix()
l.lastTimeStr = now.Format(l.layout)
}
lastTimeStr := l.lastTimeStr
l.lock.Unlock()
r := recordPool.Get().(*Record)
r.info = inf
r.code = code
// r.time = l.lastTimeStr
r.time = lastTimeStr
r.level = level
l.tunnel <- r
}
func bootstrapLogWriter(logger *Logger) {
if logger == nil {
panic("logger is nil")
}
var (
r *Record
ok bool
)
if r, ok = <-logger.tunnel; !ok {
logger.c <- true
return
}
for _, w := range logger.writers {
if err := w.Write(r); err != nil {
log.Println(err)
}
}
flushTimer := time.NewTimer(time.Millisecond * 500)
rotateTimer := time.NewTimer(time.Second * 10)
for {
select {
case r, ok = <-logger.tunnel:
if !ok {
logger.c <- true
return
}
for _, w := range logger.writers {
if err := w.Write(r); err != nil {
log.Println(err)
}
}
recordPool.Put(r)
case <-flushTimer.C:
for _, w := range logger.writers {
if f, ok := w.(Flusher); ok {
if err := f.Flush(); err != nil {
log.Println(err)
}
}
}
flushTimer.Reset(time.Millisecond * 1000)
case <-rotateTimer.C:
for _, w := range logger.writers {
if r, ok := w.(Rotater); ok {
if err := r.Rotate(); err != nil {
log.Println(err)
}
}
}
rotateTimer.Reset(time.Second * 10)
}
}
}
// default logger
var (
loggerDefault *Logger
takeUP = false
)
// SetLevel global set level is ignore
// logger level should be set in specific logger
func SetLevel(lvl int) {
loggerDefault.level = lvl
}
// SetLayout loggerDefault set the time format layout
func SetLayout(layout string) {
loggerDefault.layout = layout
}
// Debug loggerDefault deliver record to writer
func Debug(fmt string, args ...interface{}) {
loggerDefault.deliverRecordToWriter(DEBUG, fmt, args...)
}
// Warn loggerDefault deliver record to writer
func Warn(fmt string, args ...interface{}) {
loggerDefault.deliverRecordToWriter(WARNING, fmt, args...)
}
// Info loggerDefault deliver record to writer
func Info(fmt string, args ...interface{}) {
loggerDefault.deliverRecordToWriter(INFO, fmt, args...)
}
// Error loggerDefault deliver record to writer
func Error(fmt string, args ...interface{}) {
loggerDefault.deliverRecordToWriter(ERROR, fmt, args...)
}
// Fatal loggerDefault deliver record to writer
func Fatal(fmt string, args ...interface{}) {
loggerDefault.deliverRecordToWriter(FATAL, fmt, args...)
}
// Register loggerDefault register writer
func Register(w Writer) {
loggerDefault.Register(w)
}
// Close loggerDefault close logger
func Close() {
loggerDefault.Close()
}
// ShowFullPath loggerDefault show full path
func ShowFullPath(show bool) {
loggerDefault.fullPath = show
}
func init() {
loggerDefault = NewLogger()
recordPool = &sync.Pool{New: func() interface{} {
return &Record{}
}}
}