-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
548 lines (471 loc) · 12.5 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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package glog
import (
"bytes"
"errors"
"flag"
"fmt"
//"io"
"github.com/slclub/goqueue"
"github.com/slclub/utils"
"os"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"time"
)
const (
// LEVEL
LEVEL_INFO = 1
LEVEL_DEBUG = 2
LEVEL_WARNNING = 4
LEVEL_ERROR = 8
LEVEL_FATAL = 16
TRACE_INFO = 32
TRACE_DEBUG = 64
TRACE_WARNNING = 128
TRACE_ERROR = 256
TRACE_FATAL = 512
CONST_QUEUE_CAPACITY = 2 * 1024 * 1024
)
// default level. also is most commonly used.
var ALL_LEVEL = LEVEL_INFO + LEVEL_DEBUG + LEVEL_WARNNING + LEVEL_ERROR + LEVEL_FATAL + TRACE_ERROR + TRACE_FATAL
// debuging. = 1024 -1
var ALL_TRACE = ALL_LEVEL + TRACE_INFO + TRACE_DEBUG + TRACE_WARNNING
var level_name = map[int]string{
LEVEL_INFO: "INFO ",
LEVEL_DEBUG: "DEBUG",
LEVEL_WARNNING: "WARN ",
LEVEL_ERROR: "ERROR",
LEVEL_FATAL: "FATAL",
}
var log_mgr = &logManager{}
func init() {
log_mgr.show_time = true
log_mgr.level_security = ALL_LEVEL
log_mgr.pool_log.New = func() interface{} {
return log_mgr.allocateLog()
}
ring := goqueue.NewQueue()
ring.Set("capacity", CONST_QUEUE_CAPACITY)
ring.Reload()
log_mgr.ring = ring
// write now
log_mgr.min_write = 10 * 1024
log_mgr.trace_len = 5024
log_mgr.write_now = make(chan byte, 2)
log_mgr.tick_time = 5
//log_mgr.time_format = "2020-05-19 00:00:00"
log_mgr.time_format = "2006-01-02 15:04:05"
log_mgr.buf = new(bytes.Buffer)
log_mgr.log_file = createFileLog()
// start writing file routine.
go log_mgr.deamon()
}
//------------------------------------------------log manager ----------------------------------------------------
type logManager struct {
to_stderr bool // log to os.stderr flag.
to_both bool // to the file and os.stderr
level_security int // print level by level you defined. but dont include error and fatal level.
trace_len int // set the trace bytes length.
time_format string // log info time format
pool_log sync.Pool // cache loggin object . GET:pool.Get()(*logging) , pool.Put(c)
show_time bool // check whether display time.
log_file *filelog
log_file_err *filelog
ring goqueue.Ring
buf *bytes.Buffer
min_write int
write_now chan byte // write now.
tick_time time.Duration // write now.
}
func (m *logManager) allocateLog() *logging {
return &logging{buf: new(bytes.Buffer)}
}
// level check
func (m *logManager) levelCheck(level int) bool {
if m.level_security&level > 0 {
return true
}
return false
}
// follow your level check the trace opend.
func (m *logManager) traceCheck(level int) bool {
lt := level << 5
if lt&m.level_security > 0 {
return true
}
return false
}
func (m *logManager) levelSet(level int) {
m.level_security |= level
}
func (m *logManager) getLevelName(level int) string {
return level_name[level]
}
func (m *logManager) timeCheck() bool {
return m.show_time
}
// set log name
func (m *logManager) SetLogName(name_pre string) {
if name_pre == "" {
return
}
m.log_file.prefix = name_pre
if m.log_file_err != nil {
m.log_file_err.prefix = name_pre + ".error"
}
}
// set log relative path.
func (m *logManager) SetLogDir(log_dir string) {
m.log_file.dir_log = log_dir
}
// set log file abs path.
func (m *logManager) SetLogAbs(log_dir string) {
if log_dir == "" {
return
}
m.log_file.dir_abs = log_dir
if m.log_file_err != nil {
m.log_file_err.dir_abs = log_dir
}
}
func (m *logManager) SetPath(dir_abs string, dir_log string) {
m.SetLogAbs(dir_abs)
m.SetLogDir(dir_log)
}
func (m *logManager) getLog(level int) *filelog {
if !m.levelCheck(level) {
return nil
}
sl := level & (LEVEL_ERROR | LEVEL_ERROR)
file := m.log_file
if sl > 0 && m.log_file_err != nil {
file = m.log_file_err
}
return file
}
// here support concurrency invoke.
func (m *logManager) output(level int, log []byte) {
if !flag.Parsed() {
//os.Stderr.Write([]byte("Warn:log before flag.parsed."))
//os.Stderr.Write(log)
}
if m.to_stderr {
os.Stderr.Write(log)
}
// write log in ring queue.
m.ring.Write(log)
// notice deamon routine now.
m.write_now <- 1
}
// read log data from ring queue.
// this is rune on single routine.
func (m *logManager) Flush() (err error) {
//select {
//case finish_num := <-m.write_now:
// if m.ring.Size() < m.min_write && finish_num < 2 {
// return 0, nil
// }
// m.flushToFile()
//default:
// m.flushToFile()
//}
//return
m.flushToFile()
return nil
}
func (m *logManager) flushToFile() (size int64) {
m.buf.Write(m.ring.Read(0))
size = int64(m.buf.Len())
// TODO: separate the error log file. now no did it.
m.getLog(LEVEL_INFO).Write(m.buf.Bytes())
m.buf.Reset()
//testcode
//fmt.Println("[WRITE][FILE][OK]")
return
}
// This deamon should only runing in one routine.
//
func (m *logManager) deamon() {
//// 定义一个cron运行器
//c := cron.New()
//// 定时5秒,每5秒执行print5
//c.AddFunc("*/5 * * * * *", print5)
//// 定时15秒,每5秒执行print5
//c.AddFunc("*/15 * * * * *", print15)
//// 开始
//c.Start()
//defer c.Stop()
// we choice another cron task method.
tick1 := time.NewTicker(time.Second * m.tick_time)
for {
//fmt.Println("[DEAMON][FOR]", m.ring.Size())
select {
// listen write channel finis:=1
case finish := <-m.write_now:
//testcode
//fmt.Println("[DEAMON][FROM WRITER]", m.ring.Size(), "[FINISH]", finish)
if m.ring.Size() < m.min_write && finish < 2 {
break
}
m.Flush()
//testcode
//fmt.Println("[DEAMON][FROM WRITER]", m.ring.Size())
case <-tick1.C:
// cron write data to file and not care data size.
m.Flush()
//tick1.Reset(time.Second * 1)
//testcode
//fmt.Println("[DEAMON][TICK]", m.ring.Size())
}
}
}
// create logging.
func (m *logManager) createLog(level int, method string, depth int, args ...interface{}) {
// 写队列前 检查日志级别
if !m.levelCheck(level) {
return
}
log := m.pool_log.Get().(*logging)
log.level = level
switch method {
case "print":
log.print(args...)
case "println":
log.println(args...)
case "printDepth":
log.printDepth(depth, args...)
}
log.Reset()
m.pool_log.Put(log)
}
// create logging.
func (m *logManager) createLogF(level int, format string, args ...interface{}) {
if !m.levelCheck(level) {
return
}
log := m.pool_log.Get().(*logging)
log.level = level
log.printf(format, args...)
m.pool_log.Put(log)
}
//--------------------------------------------logging method ---------------------------------------
type logging struct {
buf *bytes.Buffer
level int // current log level.
}
func (l *logging) Reset() {
l.level = 0
l.buf.Reset()
}
func (l *logging) header(depth int) (*bytes.Buffer, string, int) {
_, file, line, ok := runtime.Caller(5 + depth)
if !ok {
file = "???"
line = -1
return l.formatHeader(file, line), file, line
}
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
return l.formatHeader(file, line), file, line
}
// format every log info header
func (l *logging) formatHeader(file string, line int) *bytes.Buffer {
if log_mgr.timeCheck() {
now := time.Now()
hts := fmt.Sprintf(now.Format(log_mgr.time_format))
l.buf.WriteString(hts)
l.buf.WriteString(" ")
}
l.buf.WriteString(log_mgr.getLevelName(l.level))
l.buf.WriteString(" ")
//
if log_mgr.traceCheck(l.level) {
l.buf.WriteString(file)
l.buf.WriteString(":")
l.buf.WriteString(strconv.Itoa(line))
l.buf.WriteString(" ")
}
return l.buf
}
func (l *logging) println(args ...interface{}) {
fmt.Fprintln(l.buf, args...)
l.printTrace()
log_mgr.output(l.level, l.buf.Bytes())
}
func (l *logging) lineBreak() {
if l.buf.Bytes()[l.buf.Len()-1] != utils.EOL[0] {
if utils.IsUnix() {
l.buf.WriteByte(utils.EOL[0])
} else {
l.buf.WriteString("\r\n")
}
}
}
func (l *logging) printDepth(depth int, args ...interface{}) {
l.header(depth)
fmt.Fprint(l.buf, args...)
l.lineBreak()
l.printTrace()
log_mgr.output(l.level, l.buf.Bytes())
}
func (l *logging) print(args ...interface{}) {
l.printDepth(0, args...)
}
func (l *logging) printf(format string, args ...interface{}) {
l.header(0)
fmt.Fprintf(l.buf, format, args...)
l.lineBreak()
l.printTrace()
log_mgr.output(l.level, l.buf.Bytes())
}
func (l *logging) printTrace() {
if !log_mgr.traceCheck(l.level) {
return
}
tmp_buf := stack(log_mgr.trace_len)
//fmt.Fprint(l.buf, string(tmp_buf))
//l.lineBreak()
bytes.Trim(tmp_buf, "\x00")
l.buf.Write(tmp_buf)
//l.buf.WriteString(string(tmp_buf))
//fmt.Println("STACK LEVEL CHECK", l.level, log_mgr.level_security, l.buf.Len(), len(tmp_buf))
}
// ---------------------------------------------------------------------------------------------------------------
// info
func Info(args ...interface{}) {
log_mgr.createLog(LEVEL_INFO, "print", 0, args...)
}
func Infoln(args ...interface{}) {
log_mgr.createLog(LEVEL_INFO, "println", 0, args...)
}
func Infof(format string, args ...interface{}) {
log_mgr.createLogF(LEVEL_INFO, format, args...)
}
func InfoDepth(depth int, args ...interface{}) {
log_mgr.createLog(LEVEL_INFO, "printDepth", depth, args...)
}
// debug
func Debug(args ...interface{}) {
log_mgr.createLog(LEVEL_DEBUG, "print", 0, args...)
}
func Debugln(args ...interface{}) {
log_mgr.createLog(LEVEL_DEBUG, "println", 0, args...)
}
func Debugf(format string, args ...interface{}) {
log_mgr.createLogF(LEVEL_DEBUG, format, args...)
}
func DebugDepth(depth int, args ...interface{}) {
log_mgr.createLog(LEVEL_DEBUG, "printDepth", depth, args...)
}
// warn
func Warnning(args ...interface{}) {
log_mgr.createLog(LEVEL_WARNNING, "print", 0, args...)
}
func Warnningln(args ...interface{}) {
log_mgr.createLog(LEVEL_WARNNING, "println", 0, args...)
}
func Warnningf(format string, args ...interface{}) {
log_mgr.createLogF(LEVEL_WARNNING, format, args...)
}
func WarnningDepth(depth int, args ...interface{}) {
log_mgr.createLog(LEVEL_WARNNING, "printDepth", depth, args...)
}
//error
func Error(args ...interface{}) {
log_mgr.createLog(LEVEL_ERROR, "print", 0, args...)
}
func Errorln(args ...interface{}) {
log_mgr.createLog(LEVEL_ERROR, "println", 0, args...)
}
func Errorf(format string, args ...interface{}) {
log_mgr.createLogF(LEVEL_ERROR, format, args...)
}
func ErrorDepth(depth int, args ...interface{}) {
log_mgr.createLog(LEVEL_ERROR, "printDepth", depth, args...)
}
// fatal
func Fatal(args ...interface{}) {
log_mgr.createLog(LEVEL_FATAL, "print", 0, args...)
}
func Fatalln(args ...interface{}) {
log_mgr.createLog(LEVEL_FATAL, "println", 0, args...)
}
func Fatalf(format string, args ...interface{}) {
log_mgr.createLogF(LEVEL_FATAL, format, args...)
}
func FatalDepth(depth int, args ...interface{}) {
log_mgr.createLog(LEVEL_FATAL, "printDepth", depth, args...)
}
// set log manager paramter.
func Set(field string, args ...interface{}) {
if len(args) < 1 || args[0] == nil {
fmt.Println("ERROR:Set log path need to at least one paramter ")
return
}
field = strings.ToLower(field)
switch field {
// log file path
case "path":
if len(args) < 2 {
fmt.Println("Set log path need to 2 paramter 1:abs path, 2:relative path")
return
}
log_mgr.SetLogAbs(args[0].(string))
log_mgr.SetLogDir(args[1].(string))
fmt.Println("path print Set", log_mgr.log_file.fullpath())
case "name":
log_mgr.SetLogName(args[0].(string))
case "tick":
tick_time, ok := args[0].(time.Duration)
if ok && tick_time > 0 {
log_mgr.tick_time = tick_time
}
case "head":
log_mgr.log_file.head_create = args[0].(string)
case "show_time":
log_mgr.show_time = args[0].(bool)
case "format":
log_mgr.time_format = args[0].(string)
case "rotate" :
log_mgr.log_file.setRotateSize(args[0].(string)) // 100K, 100M, 100G
case "keep" :
log_mgr.log_file.setKeepDays(args[0].(uint))
case "debug":
if check, ok := args[0].(bool); ok {
if check {
log_mgr.level_security = ALL_LEVEL
} else {
log_mgr.level_security = ALL_LEVEL - LEVEL_DEBUG
}
return
}
if level, ok := args[0].(int); ok {
log_mgr.level_security = level
}
case "stderr":
if check, ok := args[0].(bool); ok {
log_mgr.to_stderr = check
}
case "capacity":
if capacity, ok := args[0].(int); ok {
log_mgr.ring.Set("capacity", int32(capacity))
log_mgr.ring.Reload()
}
}
}
// -----------------------------------------------trace location---------------------------------------------------
// log trace string error
var err_trace_syntax = errors.New("[log_syntax][trace_error][not valid trace string]")
// utils function.
// invoke runtime stack.
func stack(size int) []byte {
//var trace = make([]byte, 1024, size)
//runtime.Stack(trace, true)
trace := debug.Stack()
return trace[600:]
}