-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
142 lines (119 loc) · 3.6 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
package log
import (
"io"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// DefaultLogger returns a new default logger that logs at info level.
func DefaultLogger() *Log {
return NewLog(NewConfig())
}
// DebugLogger returns a new default logger that logs at debug level.
func DebugLogger() *Log {
return NewLog(NewConfig().WithLevel(LevelDebug))
}
// ErrorLogger returns a new default logger that logs at error level.
func ErrorLogger() *Log {
return NewLog(NewConfig().WithLevel(LevelDebug))
}
type Logger interface {
Tag(name string, value any)
Printf(format string, values ...any)
Trace(format string, values ...any)
Debug(format string, values ...any)
Info(format string, values ...any)
Warn(format string, values ...any)
Error(format string, values ...any)
Fatal(format string, values ...any)
}
type LoggerConfig interface {
Level() Level
Tags() map[string]any
Output() io.Writer
}
var _ Logger = (*Log)(nil)
type Log struct {
output io.Writer
globalTags map[string]any
logger zerolog.Logger
// Slice of key/value pairs in sequence.
tags []any
// Perf: Copying the level into the struct performed better.
level Level
}
func NewLog(config LoggerConfig) *Log {
return &Log{
output: config.Output(),
globalTags: config.Tags(),
logger: log.Output(config.Output()).With().Fields(config.Tags()).Logger(),
tags: []any{},
level: config.Level(),
}
}
func (self *Log) Tag(name string, value any) {
self.tags = append(self.tags, name, value)
}
func (self *Log) Printf(format string, values ...any) {
// Log at INFO to match logrus.
if LevelInfo >= self.level {
self.printf(format, values...)
}
}
// nolint:goprintffuncname // reason: keep in line with logger function naming
func (self *Log) Trace(format string, values ...any) {
if LevelTrace >= self.level {
self.trace(format, values...)
}
}
// nolint:goprintffuncname // reason: keep in line with logger function naming
func (self *Log) Debug(format string, values ...any) {
if LevelDebug >= self.level {
self.debug(format, values...)
}
}
// nolint:goprintffuncname // reason: keep in line with logger function naming
func (self *Log) Info(format string, values ...any) {
if LevelInfo >= self.level {
self.info(format, values...)
}
}
// nolint:goprintffuncname // reason: keep in line with logger function naming
func (self *Log) Warn(format string, values ...any) {
if LevelWarn >= self.level {
self.warn(format, values...)
}
}
// nolint:goprintffuncname // reason: keep in line with logger function naming
func (self *Log) Error(format string, values ...any) {
if LevelError >= self.level {
self.error(format, values...)
}
}
// nolint:goprintffuncname // reason: keep in line with logger function naming
func (self *Log) Fatal(format string, values ...any) {
if LevelFatal >= self.level {
self.fatal(format, values...)
}
}
// nolint:goprintffuncname // reason: keep in line with logger function naming
func (self *Log) Panic(format string, values ...any) {
if LevelPanic >= self.level {
self.panic(format, values...)
}
}
// Localize Log to the next Context. Called in context.Localize().
// This will copy all log tags on to the localized Log value.
// If a fresh Logger is desired, use WithContext() to override.
func (self *Log) Localize() any {
newLog := &Log{
output: self.output,
globalTags: self.globalTags,
logger: log.Output(self.output).With().Fields(self.globalTags).Logger(),
tags: make([]any, 0, len(self.tags)),
level: self.level,
}
for i := 0; i < len(self.tags); i += 2 {
newLog.tags = append(newLog.tags, self.tags[i].(string), self.tags[i+1])
}
return newLog
}