-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
39 lines (33 loc) · 954 Bytes
/
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
package tanukirpc
import (
gocontext "context"
"fmt"
"log/slog"
"github.com/mackee/tanukirpc/internal/requestid"
)
var defaultLoggerKeys = []fmt.Stringer{requestid.RequestIDKey}
type loggerHandler struct {
slog.Handler
keys []fmt.Stringer
}
func (l *loggerHandler) Handle(ctx gocontext.Context, record slog.Record) error {
for _, key := range l.keys {
if v := ctx.Value(key); v != nil {
record.AddAttrs(slog.Any(key.String(), v))
}
}
return l.Handler.Handle(ctx, record)
}
// NewLogger returns a new logger with the given logger.
// This logger output with the informwation with request ID.
// If the given logger is nil, it returns use the default logger.
// keys is the whitelist of keys that use read from context.Context.
func NewLogger(logger *slog.Logger, keys []fmt.Stringer) *slog.Logger {
if logger == nil {
logger = slog.Default()
}
return slog.New(&loggerHandler{
Handler: logger.Handler(),
keys: keys,
})
}