-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
254 lines (210 loc) · 6.04 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
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
package log
import (
"fmt"
"io"
"log"
"os"
"strings"
)
// compilation time check for interface implementation.
var (
_ Logger = (*StdLog)(nil)
_ Logger = NopLog{}
)
const (
// ERR represents error logging level.
ERR Level = iota
// WRN represents warning logging level.
WRN
// INF represents info logging level.
INF
// DBG represents debug logging level.
DBG
)
const (
// ShortFmt represents format of the code line number.
// Contains resulting file name and line number.
ShortFmt LineNumFmt = iota
// LongFmt represents format of the code line number.
// Contains full file path, name and line number.
LongFmt
)
const (
// ErrParseLevel indicates that string given to function ParseLevel can't be parsed to Level.
ErrParseLevel Error = "string can't be parsed as Level, use: `error`, `warning`, `info`, `debug`"
)
// Logger formats the message according to standard format specifiers from the fmt package
// and writes the message to writer specified by the concrete interface implementation.
type Logger interface {
// Error formats and writes the error level message.
Error(format string, v ...any)
// Warning formats and writes the warning level message.
Warning(format string, v ...any)
// Info formats and writes the information level message.
Info(format string, v ...any)
// Debug formats and writes the debug level message.
Debug(format string, v ...any)
}
// Level represents an enumeration of logging levels.
type Level byte
func (l Level) String() string {
levels := [4]string{
"Error",
"Warning",
"Info",
"Debug",
}
if int(l) > len(levels)-1 {
return ""
}
return levels[l]
}
// LineNumFmt represents an enumeration of formats
// for printing the code line number in log output.
type LineNumFmt byte
// Error represents package level error related to logging work.
type Error string
func (e Error) Error() string { return string(e) }
// New returns a new instance of StdLog struct.
// Takes variadic options which will be applied to StdLog.
func New(options ...Option) *StdLog {
l := &StdLog{
err: log.New(os.Stderr, "\033[31mERR\033[0m: ", log.Ldate|log.Ltime),
wrn: log.New(os.Stderr, "\033[33mWRN\033[0m: ", log.Ldate|log.Ltime),
inf: log.New(os.Stderr, "\033[32mINF\033[0m: ", log.Ldate|log.Ltime),
dbg: log.New(os.Stderr, "\033[35mDBG\033[0m: ", log.Ldate|log.Ltime),
lvl: INF,
}
for _, option := range options {
option(l)
}
return l
}
// NewStdLog returns a new instance of StdLog struct.
// Takes variadic options which will be applied to StdLog.
// Deprecated: use New instead.
func NewStdLog(options ...Option) *StdLog { return New(options...) }
// StdLog represents wrapper around standard library logger
// which implements Logger interface.
type StdLog struct {
err, wrn, inf, dbg *log.Logger
lvl Level
}
func (l *StdLog) Error(format string, v ...any) {
_ = l.err.Output(2, fmt.Sprintf(format, v...))
}
func (l *StdLog) Info(format string, v ...any) {
if l.lvl < INF {
return
}
_ = l.inf.Output(2, fmt.Sprintf(format, v...))
}
func (l *StdLog) Warning(format string, v ...any) {
if l.lvl < WRN {
return
}
_ = l.wrn.Output(2, fmt.Sprintf(format, v...))
}
func (l *StdLog) Debug(format string, v ...any) {
if l.lvl < DBG {
return
}
_ = l.dbg.Output(2, fmt.Sprintf(format, v...))
}
// Option represents a functional option type which can be
// passed to the NewStdLog function to change its underlying
// properties.
type Option func(l *StdLog)
// WithLevel changes the underlying logging level of StdLog to the given on.
func WithLevel(level Level) Option { return func(l *StdLog) { l.lvl = level } }
// WithWriter changes the writer for each leveled loggers of StdLog to the given on.
func WithWriter(w io.Writer) Option {
return func(l *StdLog) {
l.err.SetOutput(w)
l.wrn.SetOutput(w)
l.inf.SetOutput(w)
l.dbg.SetOutput(w)
}
}
// WithNoColor disables the color output of StdLog.
func WithNoColor() Option {
return func(l *StdLog) {
l.err.SetPrefix("ERR: ")
l.wrn.SetPrefix("WRN: ")
l.inf.SetPrefix("INF: ")
l.dbg.SetPrefix("DBG: ")
}
}
// WithNoDateTime disables the output of date and time of StdLog.
func WithNoDateTime() Option {
return func(l *StdLog) {
flags := l.inf.Flags()
flags &= log.Ldate
flags &= log.Ltime
l.err.SetFlags(flags)
l.wrn.SetFlags(flags)
l.inf.SetFlags(flags)
l.dbg.SetFlags(flags)
}
}
// WithLineNum enables the printing of code line number in log output.
func WithLineNum(format LineNumFmt) Option {
return func(l *StdLog) {
flags := l.inf.Flags() // because all flags are equal for each leveled logger
switch format {
case ShortFmt:
flags |= log.Lshortfile
case LongFmt:
flags |= log.Llongfile
}
l.err.SetFlags(flags)
l.wrn.SetFlags(flags)
l.inf.SetFlags(flags)
l.dbg.SetFlags(flags)
}
}
// WithUTC sets the log time format to UTC.
func WithUTC() Option {
return func(l *StdLog) {
flags := l.inf.Flags() | log.LUTC
l.err.SetFlags(flags)
l.wrn.SetFlags(flags)
l.inf.SetFlags(flags)
l.dbg.SetFlags(flags)
}
}
// WithLevelAtPrefixEnd sets the level mark at the end of log prefix.
func WithLevelAtPrefixEnd() Option {
return func(l *StdLog) {
flags := l.inf.Flags() | log.Lmsgprefix
l.err.SetFlags(flags)
l.wrn.SetFlags(flags)
l.inf.SetFlags(flags)
l.dbg.SetFlags(flags)
}
}
// ParseLevel takes the string and tries to parse it to the Level.
func ParseLevel(lvl string) (Level, error) {
if lvl == "" {
return INF, ErrParseLevel
}
levels := map[string]Level{
strings.ToLower(WRN.String()): WRN,
strings.ToLower(ERR.String()): ERR,
strings.ToLower(INF.String()): INF,
strings.ToLower(DBG.String()): DBG,
}
level, ok := levels[strings.ToLower(lvl)]
if !ok {
return INF, fmt.Errorf("%s %w", lvl, ErrParseLevel)
}
return level, nil
}
// NopLog represents empty/disabled implementation of Logger interface.
type NopLog struct{}
// NewNopLog returns a new instance of NopLog.
func NewNopLog() NopLog { return NopLog{} }
func (l NopLog) Error(string, ...any) {}
func (l NopLog) Warning(string, ...any) {}
func (l NopLog) Info(string, ...any) {}
func (l NopLog) Debug(string, ...any) {}