-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
236 lines (187 loc) · 4.82 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
// Copyright 2024 mlycore. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Logger defines a general logger which could write specific logs
package log
import (
"io"
"os"
"strings"
"sync"
)
type Logger struct {
Writer io.Writer
mu sync.Mutex
formatter Formatter
epool sync.Pool
// Sink Sink
Level int
// TODO: remove this later
LevelStr string
CallPath int
Async bool
Color bool
}
func (l *Logger) SetWriter(w io.Writer) *Logger {
l.Writer = w
return l
}
func (l *Logger) SetColor(enabled bool) *Logger {
l.Color = enabled
return l
}
func (l *Logger) SetFormatter(f Formatter) *Logger {
l.formatter = f
return l
}
func (l *Logger) EnableAsync() *Logger {
l.Async = true
return l
}
// SetLevel set the level of log
func (l *Logger) SetLevel(level int) {
l.mu.Lock()
defer l.mu.Unlock()
l.Level = level
l.LevelStr = getLogLevel(level)
}
// SetLevelByName set the log level by name
func (l *Logger) SetLevelByName(level string) {
if strings.EqualFold(level, EnvLogLevelError) {
l.SetLevel(LogLevelError)
}
if strings.EqualFold(level, EnvLogLevelWarn) {
l.SetLevel(LogLevelWarn)
}
if strings.EqualFold(level, EnvLogLevelInfo) {
l.SetLevel(LogLevelInfo)
}
if strings.EqualFold(level, EnvLogLevelDebug) {
l.SetLevel(LogLevelDebug)
}
if strings.EqualFold(level, EnvLogLevelTrace) {
l.SetLevel(LogLevelTrace)
}
}
// SetCallPath set caller path
func (l *Logger) SetCallPath(callPath int) {
l.CallPath = callPath
}
/*
func (l *Logger) doPrint(format string, v ...interface{}) {
e := l.NewLogEntry()
defer l.PutLogEntry(e)
e.reset()
e.SetDefaultTimestamp()
e.SetLevel(l.LevelStr)
msg := formattedMessage(format, v...)
e.SetMsg(msg)
_, _ = l.Writer.Write(e.buf)
}
func (l *Logger) doPrintln(msg string) {
// TODO: make functions meta a optional argument
// fields.File, fields.Func, fields.Line = getFuncInfo(l.CallPath)
e := l.NewLogEntry()
defer l.PutLogEntry(e)
e.reset()
// e.SetColor(l.Color, 0)
e.SetDefaultTimestamp()
e.SetLevel(l.LevelStr)
e.SetMsg(msg)
// e.SetColor(l.Color, 1)
e.buf = append(e.buf, '\n')
_, _ = l.Writer.Write(e.buf)
}
func (l *Logger) doPrintln0(v ...any) {
// TODO: make functions meta a optional argument
// fields.File, fields.Func, fields.Line = getFuncInfo(l.CallPath)
e := l.NewLogEntry()
defer l.PutLogEntry(e)
e.reset()
e.SetDefaultTimestamp()
e.SetLevel(l.LevelStr)
e.SetArgs(v)
_, _ = l.Writer.Write(e.buf)
}
*/
func (l *Logger) NewLogEntry() *LogEntry {
return l.epool.Get().(*LogEntry)
}
func (l *Logger) GetLogEntry() *LogEntry {
return l.epool.Get().(*LogEntry)
}
func (l *Logger) PutLogEntry(e *LogEntry) {
e.reset()
l.epool.Put(e)
}
func (l *Logger) _printf(levelGate int, format string, v ...interface{}) {
msg := formattedMessage(format, v...)
e := l.GetLogEntry().SetMsg(msg)
defer l.PutLogEntry(e)
if levelGate >= l.Level {
e.SetLevel(LogLevelMap[levelGate])
}
l.formatter.Render(e)
_, _ = l.Writer.Write(e.Bytes())
if levelGate == LogLevelFatal {
os.Exit(1)
}
}
func (l *Logger) _println(levelGate int, msg string) {
e := l.GetLogEntry().SetMsg(msg).SetNewline()
defer l.PutLogEntry(e)
if levelGate >= l.Level {
e.SetLevel(LogLevelMap[levelGate])
}
l.formatter.Render(e)
_, _ = l.Writer.Write(e.Bytes())
if levelGate == LogLevelFatal {
os.Exit(1)
}
}
func (l *Logger) Traceln(msg string) {
l._println(LogLevelTrace, msg)
}
func (l *Logger) Debugln(msg string) {
l._println(LogLevelDebug, msg)
}
func (l *Logger) Infoln(msg string) {
l._println(LogLevelInfo, msg)
}
func (l *Logger) Warnln(msg string) {
l._println(LogLevelWarn, msg)
}
func (l *Logger) Errorln(msg string) {
l._println(LogLevelError, msg)
}
func (l *Logger) Fatalln(msg string) {
l._println(LogLevelFatal, msg)
}
func (l *Logger) Tracef(format string, v ...interface{}) {
l._printf(LogLevelTrace, format, v...)
}
func (l *Logger) Debugf(format string, v ...interface{}) {
l._printf(LogLevelDebug, format, v...)
}
func (l *Logger) Infof(format string, v ...interface{}) {
l._printf(LogLevelInfo, format, v...)
}
func (l *Logger) Warnf(format string, v ...interface{}) {
l._printf(LogLevelWarn, format, v...)
}
func (l *Logger) Errorf(format string, v ...interface{}) {
l._printf(LogLevelError, format, v...)
}
func (l *Logger) Fatalf(format string, v ...interface{}) {
l._printf(LogLevelFatal, format, v...)
}