-
Notifications
You must be signed in to change notification settings - Fork 10
/
slog_handler.go
188 lines (159 loc) · 3.87 KB
/
slog_handler.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package coralogix
import (
"context"
"log/slog"
"runtime"
)
type CoralogixHandler struct {
cxLogger *CoralogixLogger
opts slog.HandlerOptions
data map[string]interface{}
groups []string
}
type source struct {
Function string `json:"function"`
File string `json:"file"`
Line int `json:"line"`
}
type logMessage struct {
Message string `json:"message"`
Data map[string]any `json:"data,omitempty"`
Source *source `json:"source,omitempty"`
}
func NewCoralogixHandler(privateKey, applicationName, subsystemName string, opts *slog.HandlerOptions) *CoralogixHandler {
logger := NewCoralogixLogger(
privateKey,
applicationName,
subsystemName,
)
return &CoralogixHandler{
cxLogger: logger,
opts: *opts,
}
}
func (h *CoralogixHandler) cloneData() map[string]interface{} {
clone := map[string]interface{}{}
for k, v := range h.data {
clone[k] = v
}
return clone
}
func (h *CoralogixHandler) cloneGroups() []string {
clone := make([]string, len(h.groups))
for i, group := range h.groups {
clone[i] = group
}
return clone
}
// Handle handles the provided log record.
func (h *CoralogixHandler) Handle(ctx context.Context, r slog.Record) error {
fs := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fs.Next()
log := logMessage{
Message: r.Message,
Data: h.cloneData(),
}
grouped := groupToMap(log.Data, h.groups)
if r.NumAttrs() > 0 {
r.Attrs(func(a slog.Attr) bool {
attrToMap(grouped, a)
return true
})
}
if h.opts.AddSource {
log.Source = &source{
Function: f.Function,
File: f.File,
Line: f.Line,
}
}
category := ""
if v, ok := log.Data["Category"]; ok {
category = v.(string)
delete(log.Data, "Category")
}
className := ""
if v, ok := log.Data["ClassName"]; ok {
className = v.(string)
delete(log.Data, "ClassName")
}
threadId := ""
if v, ok := log.Data["ThreadId"]; ok {
threadId = v.(string)
delete(log.Data, "ThreadId")
}
h.cxLogger.Log(levelSlogToCoralogix(r.Level), log, category, className, f.Function, threadId)
return nil
}
// WithAttrs returns a new Coralogix whose attributes consists of handler's attributes followed by attrs.
func (h *CoralogixHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
data := h.cloneData()
grouped := groupToMap(data, h.groups)
for _, attr := range attrs {
attrToMap(grouped, attr)
}
return &CoralogixHandler{
cxLogger: h.cxLogger,
opts: h.opts,
data: data,
groups: h.cloneGroups(),
}
}
// WithGroup returns a new Coralogix with a group, provided the group's name.
func (h *CoralogixHandler) WithGroup(name string) slog.Handler {
return &CoralogixHandler{
cxLogger: h.cxLogger,
opts: h.opts,
data: h.cloneData(),
groups: append(h.cloneGroups(), name),
}
}
// Enabled reports whether the logger emits log records at the given context and level.
// Note: We handover the decision down to the next handler.
func (h *CoralogixHandler) Enabled(ctx context.Context, level slog.Level) bool {
minLevel := slog.LevelInfo
if h.opts.Level != nil {
minLevel = h.opts.Level.Level()
}
return level >= minLevel
}
func (h *CoralogixHandler) Stop() {
h.cxLogger.Destroy()
}
func attrToMap(m map[string]any, a slog.Attr) {
switch v := a.Value.Any().(type) {
case error:
m[a.Key] = v.Error()
case []slog.Attr:
m2 := map[string]any{}
for _, a2 := range v {
attrToMap(m2, a2)
m[a.Key] = m2
}
default:
m[a.Key] = v
}
}
func groupToMap(m map[string]any, groups []string) map[string]any {
for _, group := range groups {
if _, ok := m[group]; !ok {
m[group] = map[string]any{}
}
m = m[group].(map[string]any)
}
return m
}
func levelSlogToCoralogix(level slog.Level) uint {
switch level {
case slog.LevelDebug:
return Level.DEBUG
case slog.LevelInfo:
return Level.INFO
case slog.LevelWarn:
return Level.WARNING
case slog.LevelError:
return Level.ERROR
default:
return uint(level)
}
}