-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
128 lines (105 loc) · 2.86 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
package logging
import (
"context"
"fmt"
"log/slog"
"os"
"runtime"
"time"
)
type HandlerFunc func(next slog.Handler) slog.Handler
func (h HandlerFunc) Register(next slog.Handler) slog.Handler {
return h(next)
}
// Handler allows to chain multiple handlers.
// Order of execution is reverse to order of registration meaning first handler is executed last.
type Handler interface {
Register(next slog.Handler) slog.Handler
}
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 New(handlers ...Handler) *Logger {
if len(handlers) == 0 {
handlers = []Handler{
NewTextHandler(TextHandlerConfig{
Level: slog.LevelInfo,
Output: os.Stdout,
AddSource: false,
}),
}
}
// Chain handlers. Execution is in reverse order.
var slogHandler slog.Handler
for _, handler := range handlers {
slogHandler = handler.Register(slogHandler)
}
log := slog.New(slogHandler)
return &Logger{Log: log}
}
// Logger is a small wrapper around slog with some extra methods
// for easier migration from logrus.
type Logger struct {
Log *slog.Logger
}
func (l *Logger) Error(msg string) {
l.doLog(slog.LevelError, msg) //nolint:govet
}
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) //nolint:govet
}
func (l *Logger) Debug(msg string) {
l.doLog(slog.LevelDebug, msg) //nolint:govet
}
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) //nolint:govet
}
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) //nolint:govet
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, msg string, args ...any) {
ctx := context.Background()
if !l.Log.Handler().Enabled(ctx, lvl) {
return
}
var pcs [1]uintptr
runtime.Callers(3, pcs[:])
if len(args) > 0 {
r := slog.NewRecord(time.Now(), lvl, fmt.Sprintf(msg, args...), pcs[0])
_ = l.Log.Handler().Handle(ctx, r) //nolint:contextcheck
} else {
r := slog.NewRecord(time.Now(), lvl, msg, 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))}
}
func (l *Logger) WithGroup(name string) *Logger {
return &Logger{Log: l.Log.WithGroup(name)}
}