-
Notifications
You must be signed in to change notification settings - Fork 535
Expand file tree
/
Copy pathslog.go
More file actions
100 lines (85 loc) · 2.58 KB
/
Copy pathslog.go
File metadata and controls
100 lines (85 loc) · 2.58 KB
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024 Datadog, Inc.
package tracer
import (
"context"
"log/slog"
"strings"
"github.com/DataDog/dd-trace-go/v2/internal/log"
)
// groupOrAttrs holds either a group name or a list of slog.Attrs.
type groupOrAttrs struct {
group string // group name if non-empty
attrs []slog.Attr // attrs if non-empty
}
// slogHandler implements the slog.Handler interface to dispatch messages to our
// internal logger.
type slogHandler struct {
goas []groupOrAttrs
}
func (h slogHandler) Enabled(_ context.Context, lvl slog.Level) bool {
if lvl <= slog.LevelDebug {
return log.DebugEnabled()
}
// TODO(fg): Implement generic log level checking in the internal logger.
// But we're we're not concerned with slog perf, so this is okay for now.
return true
}
func (h slogHandler) Handle(_ context.Context, r slog.Record) error {
goas := h.goas
if r.NumAttrs() == 0 {
// If the record has no Attrs, remove groups at the end of the list; they are empty.
for len(goas) > 0 && goas[len(goas)-1].group != "" {
goas = goas[:len(goas)-1]
}
}
parts := make([]string, 0, len(goas)+r.NumAttrs())
var formatGroup strings.Builder
for _, goa := range goas {
if goa.group != "" {
formatGroup.WriteString(goa.group + ".")
} else {
for _, a := range goa.attrs {
parts = append(parts, formatGroup.String()+a.String())
}
}
}
r.Attrs(func(a slog.Attr) bool {
parts = append(parts, formatGroup.String()+a.String())
return true
})
extra := strings.Join(parts, " ")
switch r.Level {
case slog.LevelDebug:
log.Debug("%s %s", r.Message, extra)
case slog.LevelInfo:
log.Info("%s %s", r.Message, extra)
case slog.LevelWarn:
log.Warn("%s %s", r.Message, extra)
case slog.LevelError:
log.Error("%s %s", r.Message, extra)
}
return nil
}
func (h slogHandler) withGroupOrAttrs(goa groupOrAttrs) slogHandler {
h.goas = append(h.goas, goa)
return h
}
// WithGroup returns a new Handler whose group consist of
// both the receiver's groups and the arguments.
func (h slogHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}
return h.withGroupOrAttrs(groupOrAttrs{group: name})
}
// WithAttrs returns a new Handler whose attributes consist of
// both the receiver's attributes and the arguments.
func (h slogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
if len(attrs) == 0 {
return h
}
return h.withGroupOrAttrs(groupOrAttrs{attrs: attrs})
}