-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
163 lines (131 loc) · 2.62 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
package log
import (
"fmt"
"strings"
"sync"
"time"
)
type Logger interface {
Default() Recorder
Recorder(level string) Recorder
SetDefault(recorder Recorder) Logger
SetRecorder(recorder Recorder, level ...string) Logger
Record(record *Record) Logger
}
type logger struct {
sync.RWMutex
levelRecorder map[string]Recorder
defaultRecorder Recorder
}
func NewLogger() Logger {
return &logger{
levelRecorder: make(map[string]Recorder),
}
}
func (l *logger) Default() Recorder {
return l.defaultRecorder
}
func (l *logger) SetRecorder(recorder Recorder, level ...string) Logger {
l.Lock()
defer l.Unlock()
for i := range level {
l.levelRecorder[level[i]] = recorder
}
return l
}
func (l *logger) SetDefault(recorder Recorder) Logger {
l.defaultRecorder = recorder
return l
}
func (l *logger) Recorder(level string) Recorder {
return l.levelRecorder[level]
}
func (l *logger) Record(record *Record) Logger {
if record == nil {
return l
}
recorder := l.levelRecorder[record.Level]
if recorder == nil {
recorder = l.defaultRecorder
}
if recorder != nil {
recorder.Record(record)
}
return l
}
type Log interface {
Debug(v ...interface{})
Info(v ...interface{})
Warning(v ...interface{})
Error(v ...interface{})
Tag(key, val string) Log
Logger() Logger
}
type log struct {
tag Tag
logger Logger
mutex sync.RWMutex
}
func NewLog(logger Logger) Log {
return &log{
logger: logger,
}
}
func (l *log) format(v ...interface{}) string {
var builder = pool.Get().(*strings.Builder)
defer pool.Put(builder)
builder.Reset()
for i, arg := range v {
if i > 0 {
builder.WriteByte(' ')
}
builder.WriteString(fmt.Sprint(arg))
}
return builder.String()
}
func (l *log) record(level, message string) *Record {
l.mutex.RLock()
defer l.mutex.RUnlock()
var record = new(Record)
record.Time = time.Now()
record.Level = level
record.Message = message
if l.tag != nil {
record.Tag = l.tag
l.tag = nil
}
return record
}
func (l *log) output(level string, v ...interface{}) {
message := l.format(v...)
record := l.record(level, message)
l.logger.Record(record)
}
func (l *log) Tag(key, val string) Log {
l.mutex.Lock()
defer l.mutex.Unlock()
tag := l.tag.Clone()
if tag == nil {
tag = make(Tag)
}
tag.Add(key, val)
return &log{
tag: tag,
logger: l.logger,
}
}
func (l *log) Logger() Logger {
return l.logger
}
func (l *log) Debug(v ...interface{}) {
l.output("DEBUG", v...)
}
func (l *log) Info(v ...interface{}) {
l.output("INFO", v...)
}
func (l *log) Warning(v ...interface{}) {
l.output("WARNING", v...)
}
func (l *log) Error(v ...interface{}) {
l.output("ERROR", v...)
}