-
Notifications
You must be signed in to change notification settings - Fork 2
/
logging.go
157 lines (130 loc) · 3.17 KB
/
logging.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
package logging
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"runtime"
"time"
"golang.org/x/time/rate"
)
type Config struct {
Ctx context.Context
RateLimiter RateLimiterConfig
Level slog.Level
AddSource bool
Output io.Writer
Export ExportConfig
}
type RateLimiterConfig struct {
Limit rate.Limit
Burst int
Inform bool
}
type ExportConfig struct {
ExportFunc ExportFunc
MinLevel slog.Level
}
func MustParseLevel(lvlStr string) slog.Level {
var lvl slog.Level
err := lvl.UnmarshalText([]byte(lvlStr))
if err != nil {
panic("parsing log level from level string " + lvlStr)
}
return lvl
}
func init() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{})))
}
func New(cfg *Config) *Logger {
out := cfg.Output
if out == nil {
out = os.Stdout
}
if cfg.Ctx == nil {
cfg.Ctx = context.Background()
}
var replace func(groups []string, a slog.Attr) slog.Attr
if cfg.AddSource {
replace = func(groups []string, a slog.Attr) slog.Attr {
// Remove the directory from the source's filename.
if a.Key == slog.SourceKey {
source := a.Value.Any().(*slog.Source)
source.File = filepath.Base(source.File)
}
return a
}
}
// Initial logger.
var handler slog.Handler = slog.NewTextHandler(out, &slog.HandlerOptions{
AddSource: cfg.AddSource,
Level: cfg.Level,
ReplaceAttr: replace,
})
// Export logs handler.
if cfg.Export.ExportFunc != nil {
handler = NewExportHandler(cfg.Ctx, handler, cfg.Export)
}
// Rate limiter handler.
if cfg.RateLimiter.Limit != 0 {
handler = NewRateLimiterHandler(cfg.Ctx, handler, cfg.RateLimiter)
}
log := slog.New(handler)
return &Logger{log: log}
}
func NewTestLog() *Logger {
return New(&Config{Level: slog.LevelDebug})
}
type Logger struct {
log *slog.Logger
}
func (l *Logger) Error(msg string) {
l.doLog(slog.LevelError, msg)
}
func (l *Logger) Errorf(format string, a ...any) {
l.doLog(slog.LevelError, format, a...)
}
func (l *Logger) Infof(format string, a ...any) {
l.doLog(slog.LevelInfo, format, a...)
}
func (l *Logger) Info(msg string) {
l.doLog(slog.LevelInfo, msg)
}
func (l *Logger) Debug(msg string) {
l.doLog(slog.LevelDebug, msg)
}
func (l *Logger) Debugf(format string, a ...any) {
l.doLog(slog.LevelDebug, format, a...)
}
func (l *Logger) Warn(msg string) {
l.doLog(slog.LevelWarn, msg)
}
func (l *Logger) Warnf(format string, a ...any) {
l.doLog(slog.LevelWarn, format, a...)
}
func (l *Logger) Fatal(msg string) {
l.doLog(slog.LevelError, msg)
os.Exit(1)
}
func (l *Logger) IsEnabled(lvl slog.Level) bool {
ctx := context.Background()
return l.log.Handler().Enabled(ctx, lvl)
}
func (l *Logger) doLog(lvl slog.Level, format string, args ...any) {
ctx := context.Background()
if !l.log.Handler().Enabled(ctx, lvl) {
return
}
var pcs [1]uintptr
runtime.Callers(3, pcs[:])
r := slog.NewRecord(time.Now(), lvl, fmt.Sprintf(format, args...), pcs[0])
_ = l.log.Handler().Handle(ctx, r) //nolint:contextcheck
}
func (l *Logger) With(args ...any) *Logger {
return &Logger{log: l.log.With(args...)}
}
func (l *Logger) WithField(k, v string) *Logger {
return &Logger{log: l.log.With(slog.String(k, v))}
}