-
Notifications
You must be signed in to change notification settings - Fork 4
/
debug.go
65 lines (58 loc) · 1.39 KB
/
debug.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
package main
import (
"os"
"strings"
"sync"
"golang.org/x/exp/slog"
)
var (
debugMode bool
initLogOnce sync.Once
logger *slog.Logger
)
// debugLog writes the provided message and key/value pairs to stdout using structured logging
func debugLog(msg string, kvs ...any) {
if !debugMode {
return
}
initLogOnce.Do(func() {
opts := slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
switch a.Key {
case slog.SourceKey:
// trim "source" down to relative path within this module
val := a.Value.String()
if idx := strings.Index(val, "github.com/CrowdStrike/perseus/"); idx != -1 {
a.Value = slog.StringValue(val[idx+31:])
}
case slog.LevelKey:
// don't output "level" since we're only ever generating debug logs
a = slog.Attr{}
default:
}
return a
},
}
if inK8S() {
logger = slog.New(opts.NewJSONHandler(os.Stdout))
} else {
logger = slog.New(opts.NewTextHandler(os.Stdout))
}
})
if len(kvs) == 0 {
logger.LogDepth(1, slog.LevelDebug, msg)
return
}
nattrs := len(kvs) / 2
attrs := make([]slog.Attr, nattrs)
for i := 0; i < nattrs; i += 2 {
attrs[i] = slog.Any(kvs[i].(string), kvs[i+1])
}
logger.LogAttrsDepth(1, slog.LevelDebug, msg, attrs...)
}
func inK8S() bool {
_, ok := os.LookupEnv("KUBERNETES_SERVICE_HOST")
return ok
}