-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_logger.go
109 lines (91 loc) · 2.67 KB
/
base_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
package log
import (
"fmt"
"sync"
"github.com/no-src/log/content"
"github.com/no-src/log/formatter"
"github.com/no-src/log/level"
)
// baseLogger Implement basic logger operation
type baseLogger struct {
Writer
lvl level.Level // min log level
f formatter.Formatter
appendTime bool
timeFormat string
optMu sync.RWMutex // protect Option
}
func (l *baseLogger) Debug(format string, args ...any) {
l.log(level.DebugLevel, format, args...)
}
func (l *baseLogger) Info(format string, args ...any) {
l.log(level.InfoLevel, format, args...)
}
func (l *baseLogger) Warn(format string, args ...any) {
l.log(level.WarnLevel, format, args...)
}
func (l *baseLogger) Error(err error, format string, args ...any) {
l.logWithErr(err, level.ErrorLevel, format, args...)
}
func (l *baseLogger) ErrorIf(err error, format string, args ...any) error {
return errorIf(l.Error, err, format, args...)
}
// Log write a format log
func (l *baseLogger) Log(format string, args ...any) {
format = formatter.AppendRowTerminator(format)
if len(args) > 0 {
format = fmt.Sprintf(format, args...)
}
l.Write([]byte(format))
}
func (l *baseLogger) log(lvl level.Level, format string, args ...any) {
l.logWithErr(nil, lvl, format, args...)
}
func (l *baseLogger) logWithErr(err error, lvl level.Level, format string, args ...any) {
if checkLogLevel(l.lvl, lvl) {
l.optMu.RLock()
c := content.NewContent(lvl, err, l.appendTime, l.timeFormat, format, args...)
f := l.f
l.optMu.RUnlock()
data, _ := f.Serialize(c)
l.Log(string(data))
}
}
// Close the default implementation of Writer.Close.
// Nothing is going to be done here, provide this default implementation to avoid infinite loop call and stack overflow if the real struct does not implement the Writer.Close.
// As mentioned above, a panic will happen => runtime: goroutine stack exceeds 1000000000-byte limit.
// So, keep it here.
func (l *baseLogger) Close() error {
return nil
}
func (l *baseLogger) init(w Writer, lvl level.Level, appendTime bool) {
l.Writer = w
l.lvl = lvl
l.f = formatter.Default()
l.appendTime = appendTime
l.setTimeFormat(content.DefaultLogTimeFormat())
}
func (l *baseLogger) setFormatter(f formatter.Formatter) {
if f != nil {
l.optMu.Lock()
l.f = f
l.optMu.Unlock()
}
}
func (l *baseLogger) setTimeFormat(f string) {
if len(f) == 0 {
f = content.DefaultLogTimeFormat()
}
l.optMu.Lock()
l.timeFormat = f
l.optMu.Unlock()
}
func checkLogLevel(lvl level.Level, currentLevel level.Level) bool {
return currentLevel >= lvl
}
func errorIf(f func(err error, format string, args ...any), err error, format string, args ...any) error {
if err != nil {
f(err, format, args...)
}
return err
}