-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.go
160 lines (136 loc) · 3.56 KB
/
logs.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
// logs.go
package retrypool
import (
"context"
"fmt"
"log/slog"
"os"
"runtime"
"sync/atomic"
)
// Logger provides structured logging with source tracking
type Logger interface {
Debug(ctx context.Context, msg string, keysAndValues ...any)
Info(ctx context.Context, msg string, keysAndValues ...any)
Warn(ctx context.Context, msg string, keysAndValues ...any)
Error(ctx context.Context, msg string, keysAndValues ...any)
WithFields(fields map[string]any) Logger
Enable()
Disable()
}
type LogFormat string
type LogLevel = slog.Level
const (
TextFormat LogFormat = "text"
JSONFormat LogFormat = "json"
)
// loggerConfig holds configuration for the logger
type loggerConfig struct {
format LogFormat
level LogLevel
output *os.File // Allows for custom output (defaults to os.Stdout)
}
// LogOption defines functional options for logger configuration
type LogOption func(*loggerConfig)
// WithFormat sets the log format
func WithFormat(format LogFormat) LogOption {
return func(cfg *loggerConfig) {
cfg.format = format
}
}
// WithOutput sets a custom output file
func WithOutput(output *os.File) LogOption {
return func(cfg *loggerConfig) {
cfg.output = output
}
}
// defaultLogger implements Logger with slog
type defaultLogger struct {
logger *slog.Logger
enabled atomic.Bool // Allows for runtime enable/disable
}
// NewLogger creates a new logger with the given options
func NewLogger(level LogLevel, opts ...LogOption) Logger {
cfg := &loggerConfig{
format: TextFormat,
level: level,
output: os.Stdout,
}
for _, opt := range opts {
opt(cfg)
}
handlerOpts := &slog.HandlerOptions{
Level: cfg.level,
AddSource: false,
}
var handler slog.Handler
switch cfg.format {
case JSONFormat:
handler = slog.NewJSONHandler(cfg.output, handlerOpts)
default:
handler = slog.NewTextHandler(cfg.output, handlerOpts)
}
l := &defaultLogger{
logger: slog.New(handler),
}
l.enabled.Store(true)
return l
}
// addSource adds file:line information to log entries
func (l *defaultLogger) addSource(keysAndValues []any) []any {
if pc, file, line, ok := runtime.Caller(2); ok {
if fn := runtime.FuncForPC(pc); fn != nil {
// Add function name, file and line number
return append(keysAndValues,
"caller", fmt.Sprintf("%s:%d", file, line),
"function", fn.Name(),
)
}
return append(keysAndValues, "source", fmt.Sprintf("%s:%d", file, line))
}
return keysAndValues
}
func (l *defaultLogger) Debug(ctx context.Context, msg string, keysAndValues ...any) {
if !l.enabled.Load() {
return
}
l.logger.DebugContext(ctx, msg, l.addSource(keysAndValues)...)
}
func (l *defaultLogger) Info(ctx context.Context, msg string, keysAndValues ...any) {
if !l.enabled.Load() {
return
}
l.logger.InfoContext(ctx, msg, l.addSource(keysAndValues)...)
}
func (l *defaultLogger) Warn(ctx context.Context, msg string, keysAndValues ...any) {
if !l.enabled.Load() {
return
}
l.logger.WarnContext(ctx, msg, l.addSource(keysAndValues)...)
}
func (l *defaultLogger) Error(ctx context.Context, msg string, keysAndValues ...any) {
if !l.enabled.Load() {
return
}
l.logger.ErrorContext(ctx, msg, l.addSource(keysAndValues)...)
}
func (l *defaultLogger) WithFields(fields map[string]any) Logger {
if len(fields) == 0 {
return l
}
args := make([]any, 0, len(fields)*2)
for k, v := range fields {
args = append(args, k, v)
}
return &defaultLogger{
logger: l.logger.With(args...),
}
}
// Enable enables logging
func (l *defaultLogger) Enable() {
l.enabled.Store(true)
}
// Disable disables logging
func (l *defaultLogger) Disable() {
l.enabled.Store(false)
}