Skip to content

Commit ca7b83c

Browse files
committed
feat(logger): add slog-dedup to prevent duplicate log entries and update dependencies
1 parent b673b8c commit ca7b83c

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ require golang.org/x/sync v0.19.0
4545
require (
4646
github.com/jmespath/go-jmespath v0.4.0
4747
github.com/prometheus/common v0.67.5
48+
github.com/veqryn/slog-dedup v0.6.0
4849
)
4950

5051
require (
@@ -337,6 +338,7 @@ require (
337338
k8s.io/klog/v2 v2.130.1 // indirect
338339
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
339340
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
341+
modernc.org/b/v2 v2.1.2 // indirect
340342
mvdan.cc/gofumpt v0.9.2 // indirect
341343
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
342344
sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
692692
github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
693693
github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg=
694694
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
695+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
696+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
695697
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
696698
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
697699
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
@@ -806,6 +808,8 @@ github.com/urfave/cli v1.22.17 h1:SYzXoiPfQjHBbkYxbew5prZHS1TOLT3ierW8SYLqtVQ=
806808
github.com/urfave/cli v1.22.17/go.mod h1:b0ht0aqgH/6pBYzzxURyrM4xXNgsoT/n2ZzwQiEhNVo=
807809
github.com/vbatts/tar-split v0.12.1 h1:CqKoORW7BUWBe7UL/iqTVvkTBOF8UvOMKOIZykxnnbo=
808810
github.com/vbatts/tar-split v0.12.1/go.mod h1:eF6B6i6ftWQcDqEn3/iGFRFRo8cBIMSJVOpnNdfTMFA=
811+
github.com/veqryn/slog-dedup v0.6.0 h1:h3pm+Wl3IuQkq2f7bv4V4GJsJRyETQlPMNjCfXU929s=
812+
github.com/veqryn/slog-dedup v0.6.0/go.mod h1:sJzvkleUbwNS92fs14L5n5ZsmCcaZGyZEtCdEwgCBU4=
809813
github.com/weppos/publicsuffix-go v0.15.1-0.20210511084619-b1f36a2d6c0b h1:FsyNrX12e5BkplJq7wKOLk0+C6LZ+KGXvuEcKUYm5ss=
810814
github.com/weppos/publicsuffix-go v0.15.1-0.20210511084619-b1f36a2d6c0b/go.mod h1:HYux0V0Zi04bHNwOHy4cXJVz/TQjYonnF6aoYhj+3QE=
811815
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
@@ -1118,6 +1122,12 @@ k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f h1:GA7//TjRY9yWGy1poLzYYJ
11181122
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f/go.mod h1:R/HEjbvWI0qdfb8viZUeVZm0X6IZnxAydC7YU42CMw4=
11191123
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 h1:M3sRQVHv7vB20Xc2ybTt7ODCeFj6JSWYFzOFnYeS6Ro=
11201124
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
1125+
modernc.org/b/v2 v2.1.2 h1:PX71mrgWbZV3325fh6yVnzAuMU1qU+OX/bud9wmqbII=
1126+
modernc.org/b/v2 v2.1.2/go.mod h1:Xyvaj/0l3N2tUButg4o32FUWXhhQ9tCePmQwQYVJLXQ=
1127+
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
1128+
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
1129+
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
1130+
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
11211131
mvdan.cc/gofumpt v0.9.2 h1:zsEMWL8SVKGHNztrx6uZrXdp7AX8r421Vvp23sz7ik4=
11221132
mvdan.cc/gofumpt v0.9.2/go.mod h1:iB7Hn+ai8lPvofHd9ZFGVg2GOr8sBUw1QUWjNbmIL/s=
11231133
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 h1:/Rv+M11QRah1itp8VhT6HoVx1Ray9eB4DBr+K+/sCJ8=

internal/logger/logger.go

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"log/slog"
66
"os"
7+
8+
slogdedup "github.com/veqryn/slog-dedup"
79
)
810

911
type Logger struct {
@@ -40,44 +42,51 @@ func ErrAttr(err error) slog.Attr {
4042

4143
// New returns a new Logger with the given log level.
4244
func New(logLevel slog.Level) *Logger {
45+
jh := slog.NewJSONHandler(
46+
os.Stderr,
47+
&slog.HandlerOptions{
48+
// AddSource: true,
49+
Level: logLevel,
50+
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
51+
// Customize the name of the time key.
52+
if a.Key == slog.TimeKey {
53+
a.Key = "time"
54+
}
55+
56+
// Customize the name of the level key and the output string, including
57+
// custom level values.
58+
if a.Key == slog.LevelKey {
59+
// Handle custom level values.
60+
level := a.Value.Any().(slog.Level)
61+
62+
switch {
63+
case level < LevelInfo:
64+
a.Value = slog.StringValue(LevelDebugName)
65+
case level < LevelWarning:
66+
a.Value = slog.StringValue(LevelInfoName)
67+
case level < LevelError:
68+
a.Value = slog.StringValue(LevelWarningName)
69+
case level < LevelCritical:
70+
a.Value = slog.StringValue(LevelErrorName)
71+
default:
72+
a.Value = slog.StringValue(LevelCriticalName)
73+
}
74+
}
75+
76+
return a
77+
},
78+
},
79+
)
80+
81+
overwriter := slog.New(
82+
slogdedup.NewOverwriteHandler(jh, nil),
83+
)
84+
85+
slog.SetDefault(overwriter)
86+
slog.SetLogLoggerLevel(logLevel)
87+
4388
return &Logger{
44-
slog.New(
45-
slog.NewJSONHandler(
46-
os.Stderr,
47-
&slog.HandlerOptions{
48-
// AddSource: true,
49-
Level: logLevel,
50-
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
51-
// Customize the name of the time key.
52-
if a.Key == slog.TimeKey {
53-
a.Key = "time"
54-
}
55-
56-
// Customize the name of the level key and the output string, including
57-
// custom level values.
58-
if a.Key == slog.LevelKey {
59-
// Handle custom level values.
60-
level := a.Value.Any().(slog.Level)
61-
62-
switch {
63-
case level < LevelInfo:
64-
a.Value = slog.StringValue(LevelDebugName)
65-
case level < LevelWarning:
66-
a.Value = slog.StringValue(LevelInfoName)
67-
case level < LevelError:
68-
a.Value = slog.StringValue(LevelWarningName)
69-
case level < LevelCritical:
70-
a.Value = slog.StringValue(LevelErrorName)
71-
default:
72-
a.Value = slog.StringValue(LevelCriticalName)
73-
}
74-
}
75-
76-
return a
77-
},
78-
},
79-
),
80-
),
89+
overwriter,
8190
logLevel,
8291
}
8392
}

0 commit comments

Comments
 (0)