-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.go
63 lines (49 loc) · 1.34 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
package traceid
import (
"context"
"log/slog"
)
var LogKey string = "traceId"
/*
You can pass slog.Handler, and all the logs would automatically log
the traceId if logging with context is used.
slog.Log()
slog.DebugContext()
slog.InfoContext()
slog.WarnContext()
slog.ErrorContext()
Example:
ctx := traceid.NewContext(context.Background())
handler := traceid.LogHandler(slog.NewJSONHandler(os.Stdout, nil))
logger := slog.New(handler)
logger.InfoContext(ctx, "message")
This would log
{"time":"2025-04-01T13:17:43.097789397Z","level":"INFO","msg":"message","traceId":"0195f180-2939-7bc4-bffe-838eb3c62526"}
*/
func LogHandler(handler slog.Handler) slog.Handler {
return &logHandler{
handler: handler,
}
}
type logHandler struct {
handler slog.Handler
}
func (l *logHandler) Enabled(ctx context.Context, level slog.Level) bool {
return l.handler.Enabled(ctx, level)
}
func (l *logHandler) Handle(ctx context.Context, record slog.Record) error {
if traceID := FromContext(ctx); traceID != "" {
record.AddAttrs(slog.String(LogKey, traceID))
}
return l.handler.Handle(ctx, record)
}
func (l *logHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &logHandler{
handler: l.handler.WithAttrs(attrs),
}
}
func (l *logHandler) WithGroup(name string) slog.Handler {
return &logHandler{
handler: l.handler.WithGroup(name),
}
}