-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
344 lines (288 loc) · 9.38 KB
/
entry.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
// Copyright (c) Jeevanandam M (https://github.com/jeevatkm)
// go-aah/log source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package log
import (
"bytes"
"encoding/json"
"fmt"
slog "log"
"strings"
"sync"
"time"
)
var (
entryPool *sync.Pool
bufPool *sync.Pool
_ Loggerer = (*Entry)(nil)
)
// Fields type is used to log fields values in the logger.
type Fields map[string]interface{}
// Entry represents a log entry and contains the timestamp when the entry
// was created, level, etc.
type Entry struct {
Level level `json:"-"`
Line int `json:"line,omitempty"`
AppName string `json:"app_name,omitempty"`
InstanceName string `json:"instance_name,omitempty"`
RequestID string `json:"request_id,omitempty"`
Principal string `json:"principal,omitempty"`
Message string `json:"message,omitempty"`
File string `json:"file,omitempty"`
Fields Fields `json:"fields,omitempty"`
Time time.Time `json:"-"`
logger *Logger
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Entry methods
//___________________________________
// MarshalJSON method for formating entry to JSON.
func (e *Entry) MarshalJSON() ([]byte, error) {
type alias Entry
ne := struct {
Level string `json:"level,omitempty"`
Time string `json:"timestamp,omitempty"`
*alias
}{
Level: e.Level.String(),
Time: formatTime(e.Time),
alias: (*alias)(e),
}
// delete skip fields
for _, v := range strings.Fields("appname insname reqid principal") {
delete(ne.Fields, v)
}
return json.Marshal(ne)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Entry logger methods
//_______________________________________
// Error logs message as `ERROR`. Arguments handled in the mananer of `fmt.Print`.
func (e *Entry) Error(v ...interface{}) {
if e.logger.level >= LevelError {
e.output(LevelError, fmt.Sprint(v...))
}
}
// Errorf logs message as `ERROR`. Arguments handled in the mananer of `fmt.Printf`.
func (e *Entry) Errorf(format string, v ...interface{}) {
if e.logger.level >= LevelError {
e.output(LevelError, fmt.Sprintf(format, v...))
}
}
// Warn logs message as `WARN`. Arguments handled in the mananer of `fmt.Print`.
func (e *Entry) Warn(v ...interface{}) {
if e.logger.level >= LevelWarn {
e.output(LevelWarn, fmt.Sprint(v...))
}
}
// Warnf logs message as `WARN`. Arguments handled in the mananer of `fmt.Printf`.
func (e *Entry) Warnf(format string, v ...interface{}) {
if e.logger.level >= LevelWarn {
e.output(LevelWarn, fmt.Sprintf(format, v...))
}
}
// Info logs message as `INFO`. Arguments handled in the mananer of `fmt.Print`.
func (e *Entry) Info(v ...interface{}) {
if e.logger.level >= LevelInfo {
e.output(LevelInfo, fmt.Sprint(v...))
}
}
// Infof logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.
func (e *Entry) Infof(format string, v ...interface{}) {
if e.logger.level >= LevelInfo {
e.output(LevelInfo, fmt.Sprintf(format, v...))
}
}
// Debug logs message as `DEBUG`. Arguments handled in the mananer of `fmt.Print`.
func (e *Entry) Debug(v ...interface{}) {
if e.logger.level >= LevelDebug {
e.output(LevelDebug, fmt.Sprint(v...))
}
}
// Debugf logs message as `DEBUG`. Arguments handled in the mananer of `fmt.Printf`.
func (e *Entry) Debugf(format string, v ...interface{}) {
if e.logger.level >= LevelDebug {
e.output(LevelDebug, fmt.Sprintf(format, v...))
}
}
// Trace logs message as `TRACE`. Arguments handled in the mananer of `fmt.Print`.
func (e *Entry) Trace(v ...interface{}) {
if e.logger.level >= LevelTrace {
e.output(LevelTrace, fmt.Sprint(v...))
}
}
// Tracef logs message as `TRACE`. Arguments handled in the mananer of `fmt.Printf`.
func (e *Entry) Tracef(format string, v ...interface{}) {
if e.logger.level >= LevelTrace {
e.output(LevelTrace, fmt.Sprintf(format, v...))
}
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Entry methods - Drop-in replacement
// for Go standard logger
//_______________________________________
// Print logs message as `INFO`. Arguments handled in the mananer of `fmt.Print`.
func (e *Entry) Print(v ...interface{}) {
e.output(LevelInfo, fmt.Sprint(v...))
}
// Printf logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.
func (e *Entry) Printf(format string, v ...interface{}) {
e.output(LevelInfo, fmt.Sprintf(format, v...))
}
// Println logs message as `INFO`. Arguments handled in the mananer of `fmt.Printf`.
func (e *Entry) Println(v ...interface{}) {
e.output(LevelInfo, fmt.Sprint(v...))
}
// Fatal logs message as `FATAL` and call to os.Exit(1).
func (e *Entry) Fatal(v ...interface{}) {
e.output(LevelFatal, fmt.Sprint(v...))
exit(1)
}
// Fatalf logs message as `FATAL` and call to os.Exit(1).
func (e *Entry) Fatalf(format string, v ...interface{}) {
e.output(LevelFatal, fmt.Sprintf(format, v...))
exit(1)
}
// Fatalln logs message as `FATAL` and call to os.Exit(1).
func (e *Entry) Fatalln(v ...interface{}) {
e.output(LevelFatal, fmt.Sprint(v...))
exit(1)
}
// Panic logs message as `PANIC` and call to panic().
func (e *Entry) Panic(v ...interface{}) {
e.output(LevelPanic, fmt.Sprint(v...))
panic(e)
}
// Panicf logs message as `PANIC` and call to panic().
func (e *Entry) Panicf(format string, v ...interface{}) {
e.output(LevelPanic, fmt.Sprintf(format, v...))
panic(e)
}
// Panicln logs message as `PANIC` and call to panic().
func (e *Entry) Panicln(v ...interface{}) {
e.output(LevelPanic, fmt.Sprint(v...))
panic(e)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Entry level methods
//___________________________________
// IsLevelInfo method returns true if log level is INFO otherwise false.
func (e *Entry) IsLevelInfo() bool {
return e.logger.IsLevelInfo()
}
// IsLevelError method returns true if log level is ERROR otherwise false.
func (e *Entry) IsLevelError() bool {
return e.logger.IsLevelError()
}
// IsLevelWarn method returns true if log level is WARN otherwise false.
func (e *Entry) IsLevelWarn() bool {
return e.logger.IsLevelWarn()
}
// IsLevelDebug method returns true if log level is DEBUG otherwise false.
func (e *Entry) IsLevelDebug() bool {
return e.logger.IsLevelDebug()
}
// IsLevelTrace method returns true if log level is TRACE otherwise false.
func (e *Entry) IsLevelTrace() bool {
return e.logger.IsLevelTrace()
}
// IsLevelFatal method returns true if log level is FATAL otherwise false.
func (e *Entry) IsLevelFatal() bool {
return e.logger.IsLevelFatal()
}
// IsLevelPanic method returns true if log level is PANIC otherwise false.
func (e *Entry) IsLevelPanic() bool {
return e.logger.IsLevelPanic()
}
// ToGoLogger method wraps the current log writer into Go Logger instance.
func (e *Entry) ToGoLogger() *slog.Logger {
return e.logger.ToGoLogger()
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Entry context/field methods
//_______________________________________
// WithFields method to add multiple key-value pairs into log.
func (e *Entry) WithFields(fields Fields) Loggerer {
ne := acquireEntry(e.logger)
ne.addFields(e.Fields)
ne.addFields(fields)
return ne
}
// WithField method to add single key-value into log
func (e *Entry) WithField(key string, value interface{}) Loggerer {
return e.WithFields(Fields{key: value})
}
// Reset method resets the `Entry` values for reuse.
func (e *Entry) Reset() {
e.AppName = ""
e.RequestID = ""
e.Principal = ""
e.Level = LevelUnknown
e.Time = time.Time{}
e.Message = ""
e.File = ""
e.Line = 0
e.Fields = make(Fields)
e.logger = nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Fields Unexported methods
//___________________________________
func (f Fields) str(key string) string {
if v, found := f[key]; found {
return fmt.Sprint(v)
}
return ""
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported methods
//___________________________________
func (e *Entry) output(lvl level, msg string) {
e.Time = time.Now()
e.Level = lvl
e.Message = msg
e.processFields()
e.logger.output(e)
}
func (e *Entry) addFields(fields Fields) {
for k, v := range fields {
e.Fields[k] = v
}
}
func (e *Entry) processFields() {
e.addFields(e.logger.ctx)
e.AppName = e.Fields.str("appname")
e.InstanceName = e.Fields.str("insname")
e.RequestID = e.Fields.str("reqid")
e.Principal = e.Fields.str("principal")
}
func (e *Entry) isSkipField(key string) bool {
return (key == "appname" || key == "insname" || key == "reqid" || key == "principal")
}
func newEntry() *Entry {
return &Entry{
Fields: make(Fields),
}
}
func acquireEntry(logger *Logger) *Entry {
e := entryPool.Get().(*Entry)
e.logger = logger
return e
}
func releaseEntry(e *Entry) {
e.Reset()
entryPool.Put(e)
}
func acquireBuffer() *bytes.Buffer {
return bufPool.Get().(*bytes.Buffer)
}
func releaseBuffer(buf *bytes.Buffer) {
if buf != nil {
buf.Reset()
bufPool.Put(buf)
}
}
func init() {
bufPool = &sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
entryPool = &sync.Pool{New: func() interface{} { return newEntry() }}
}