-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
304 lines (277 loc) · 8.8 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
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
package loggo
import (
"context"
"errors"
"fmt"
"io"
"os"
"runtime"
"sync"
"text/template"
"time"
)
// Logger is the structure that holds the logger information.
// It includes the log level Threshold, output destination, message template, and time provider.
type Logger struct {
Context context.Context // Context for the logger
Threshold Level // Minimum log level to output
mu sync.RWMutex // Ensures thread-safe access to the logger
output io.Writer // Destination for log output
template string // Template for log messages
now TimeProvider // Function to get the current time
timeFormat string // Format for the time in the log message
maxSize int // Maximum size of the log message
callerProvider CallerProvider // Function to get the caller information
preHooks []Hook // Pre-hooks to run before logging
postHooks []Hook // Post-hooks to run after logging
}
// New creates a new Logger with the given Threshold and options.
// The default output is os.Stdout, the default template is "%s [%5s]: %s", and the default time provider is time.Now.
//
// Parameters:
// - Threshold: Minimum log level to output.
// - options: Variadic options to configure the Logger.
//
// Returns:
// - A pointer to the newly created Logger.
//
// Example:
//
// logger := loggo.New(loggo.LevelInfo, loggo.WithOutput(os.Stderr))
// logger.Info("This is an info message")
func New(threshold Level, options ...Option) *Logger {
defaultCaller := func() (pc uintptr, file string, line int, ok bool) {
pc, file, line, ok = runtime.Caller(5)
return
}
log := &Logger{
Threshold: threshold,
Context: context.Background(),
output: os.Stdout,
template: "{{.Time}} [{{printf \"%5s\" .Level}}]: {{.Message}}",
now: time.Now,
timeFormat: "2006-01-02 15:04:05",
maxSize: 1000,
callerProvider: defaultCaller,
preHooks: []Hook{},
postHooks: []Hook{},
}
for _, option := range options {
option(log)
}
return log
}
// Log logs a message at the given log level.
// If the log level is below the Threshold, the message is not logged. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - level: The log level of the message.
// - message: The message to log.
//
// Example:
//
// logger := loggo.New(loggo.LevelInfo)
// logger.Log(loggo.LevelInfo, "This is an info message")
func (l *Logger) Log(level Level, message string) {
_ = l.LogE(level, message)
}
// LogE logs a message at the given log level and returns an error if the message could not be logged.
// If the log level is below the Threshold, the message is not logged.
//
// Parameters:
// - level: The log level of the message.
// - message: The message to log.
//
// Returns:
// - An error if the message could not be logged, nil otherwise.
//
// Example:
//
// logger := loggo.New(loggo.LevelInfo)
// err := logger.LogE(loggo.LevelInfo, "This is an info message")
// if err != nil {
// log.Fatal(err)
// }
func (l *Logger) LogE(level Level, message string) error {
for _, hook := range l.preHooks {
hook(l, &message)
}
if l.Threshold > level {
return nil
}
data := getTemplateData(level, message, l)
tmpl, err := template.New("log").Parse(l.template + "\n")
if err != nil {
return errors.New("error parsing template: " + err.Error())
}
l.mu.Lock()
defer l.mu.Unlock()
if err = tmpl.Execute(l.output, data); err != nil {
return errors.New("error executing template: " + err.Error())
}
for _, hook := range l.postHooks {
hook(l, &message)
}
return nil
}
// Logf logs a formatted message at the given log level.
// If the log level is below the Threshold, the message is not logged. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - level: The log level of the message.
// - format: The format string for the message.
// - args: The arguments for the format string.
//
// Example:
//
// logger := loggo.New(loggo.LevelInfo)
// logger.Logf(loggo.LevelInfo, "This is an info message with a %s", "format")
func (l *Logger) Logf(level Level, format string, args ...any) {
l.Log(level, fmt.Sprintf(format, args...))
}
// LogfE logs a formatted message at the given log level and returns an error if the message could not be logged.
// If the log level is below the Threshold, the message is not logged.
//
// Parameters:
// - level: The log level of the message.
// - format: The format string for the message.
// - args: The arguments for the format string.
//
// Returns:
// - An error if the message could not be logged, nil otherwise.
//
// Example:
//
// logger := loggo.New(loggo.LevelInfo)
// err := logger.LogfE(loggo.LevelInfo, "This is an info message with a %s", "format")
// if err != nil {
// log.Fatal(err)
// }
func (l *Logger) LogfE(level Level, format string, args ...any) error {
return l.LogE(level, fmt.Sprintf(format, args...))
}
// Debug logs a message at the LevelDebug. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - message: The debug message to log.
//
// Example:
//
// logger := loggo.New(loggo.LevelDebug)
// logger.Debug("This is a debug message")
func (l *Logger) Debug(message string) {
l.Log(LevelDebug, message)
}
// Debugf logs a formatted message at the LevelDebug. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - format: The format string for the debug message.
// - args: The arguments for the format string.
//
// Example:
//
// logger := loggo.New(loggo.LevelDebug)
// logger.Debugf("This is a debug message with a %s", "format")
func (l *Logger) Debugf(format string, args ...any) {
l.Logf(LevelDebug, format, args...)
}
// Info logs a message at the LevelInfo. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - message: The info message to log.
//
// Example:
//
// logger := loggo.New(loggo.LevelInfo)
// logger.Info("This is an info message")
func (l *Logger) Info(message string) {
l.Log(LevelInfo, message)
}
// Infof logs a formatted message at the LevelInfo. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - format: The format string for the info message.
// - args: The arguments for the format string.
//
// Example:
//
// logger := loggo.New(loggo.LevelInfo)
// logger.Infof("This is an info message with a %s", "format")
func (l *Logger) Infof(format string, args ...any) {
l.Logf(LevelInfo, format, args...)
}
// Warn logs a message at the LevelWarn. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - message: The warn message to log.
//
// Example:
//
// logger := loggo.New(loggo.LevelWarn)
// logger.Warn("This is a warn message")
func (l *Logger) Warn(message string) {
l.Log(LevelWarn, message)
}
// Warnf logs a formatted message at the LevelWarn. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - format: The format string for the warn message.
// - args: The arguments for the format string.
//
// Example:
//
// logger := loggo.New(loggo.LevelWarn)
// logger.Warnf("This is a warn message with a %s", "format")
func (l *Logger) Warnf(format string, args ...any) {
l.Logf(LevelWarn, format, args...)
}
// Error logs a message at the LevelError. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - message: The error message to log.
//
// Example:
//
// logger := loggo.New(loggo.LevelError)
// logger.Error("This is an error message")
func (l *Logger) Error(message string) {
l.Log(LevelError, message)
}
// Errorf logs a formatted message at the LevelError. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - format: The format string for the error message.
// - args: The arguments for the format string.
//
// Example:
//
// logger := loggo.New(loggo.LevelError)
// logger.Errorf("This is an error message with a %s", "format")
func (l *Logger) Errorf(format string, args ...any) {
l.Logf(LevelError, format, args...)
}
// Fatal logs a message at the LevelFatal. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - message: The fatal message to log.
//
// Example:
//
// logger := loggo.New(loggo.LevelFatal)
// logger.Fatal("This is a fatal message")
func (l *Logger) Fatal(message string) {
l.Log(LevelFatal, message)
}
// Fatalf logs a formatted message at the LevelFatal. If an error occurs while logging the message, it is ignored.
//
// Parameters:
// - format: The format string for the fatal message.
// - args: The arguments for the format string.
//
// Example:
//
// logger := loggo.New(loggo.LevelFatal)
// logger.Fatalf("This is a fatal message with a %s", "format")
func (l *Logger) Fatalf(format string, args ...any) {
l.Logf(LevelFatal, format, args...)
}