-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslog.go
51 lines (46 loc) · 1.42 KB
/
slog.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
package main
import (
"log/slog"
"os"
"fortio.org/log"
)
var slogger *slog.Logger
func SetupSlogLogger() {
level := new(slog.LevelVar) // Info by default
if log.GetLogLevel() == log.Debug {
level.Set(slog.LevelDebug)
}
// Ouch... slog jsonhandler doesn't have a hook or config for configuring how is the timestamp being logged.
// Makes it the same field name and format as both zap and fortio.
replace := func(groups []string, a slog.Attr) slog.Attr {
if len(groups) == 0 && a.Key == slog.TimeKey {
return slog.Attr{
Key: "ts",
Value: slog.Float64Value(log.TimeToTS(a.Value.Time())),
}
}
return a
}
slogger = slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: level, ReplaceAttr: replace}))
}
func SlogLog1(id string, numLogged int64, numExtraNotLogged int) {
// iterate numCalls time
for i := int64(1); i <= numLogged; i++ {
// Not optimized version - otherwise we can mutate the KeyVals
slogger.Info("A visible log entry with 3 attributes",
slog.Any("iteration", i),
slog.String("id", id),
slog.String("logger", "sloggr"), // same byte lentgh as `fortio`
)
for j := 1; j <= numExtraNotLogged; j++ {
// Not optimized version - otherwise we'd check
// if log.LogDebug() {...}
slogger.Debug("Not visible entry with 4 attributes",
slog.String("id", id),
slog.Int64("iteration", i),
slog.Int("sub-iteration", j),
slog.String("logger", "sloggr"),
)
}
}
}