Skip to content

Commit d0e3731

Browse files
authored
Switch to slog (#240)
Signed-off-by: Luca Comellini <[email protected]>
1 parent 86bfc1d commit d0e3731

File tree

6 files changed

+21
-35
lines changed

6 files changed

+21
-35
lines changed

.golangci.yml

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
linters:
22
enable:
3-
- goimports
4-
- misspell
5-
- revive
3+
- goimports
4+
- misspell
5+
- revive
66

77
issues:
88
exclude-rules:
9-
- path: _test.go
10-
linters:
11-
- errcheck
9+
- path: _test.go
10+
linters:
11+
- errcheck
1212

1313
linters-settings:
1414
errcheck:
1515
exclude-functions:
1616
# Used in HTTP handlers, any error is handled by the server itself.
1717
- (net/http.ResponseWriter).Write
18-
# Never check for logger errors.
19-
- (github.com/go-kit/log.Logger).Log
2018
revive:
2119
rules:
2220
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter

go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.22
55
require (
66
github.com/alecthomas/kingpin/v2 v2.4.0
77
github.com/coreos/go-systemd/v22 v22.5.0
8-
github.com/go-kit/log v0.2.1
98
github.com/mdlayher/vsock v1.2.1
109
github.com/prometheus/common v0.58.0
1110
golang.org/x/crypto v0.26.0
@@ -17,7 +16,6 @@ require (
1716
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
1817
github.com/beorn7/perks v1.0.1 // indirect
1918
github.com/cespare/xxhash/v2 v2.3.0 // indirect
20-
github.com/go-logfmt/logfmt v0.5.1 // indirect
2119
github.com/jpillora/backoff v1.0.0 // indirect
2220
github.com/kr/text v0.2.0 // indirect
2321
github.com/mdlayher/socket v0.4.1 // indirect

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
1212
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1313
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1414
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
15-
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
16-
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
17-
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
18-
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
1915
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
2016
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2117
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=

web/handler.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package web
1818
import (
1919
"encoding/hex"
2020
"fmt"
21+
"log/slog"
2122
"net/http"
2223
"strings"
2324
"sync"
2425

25-
"github.com/go-kit/log"
2626
"golang.org/x/crypto/bcrypt"
2727
)
2828

@@ -78,7 +78,7 @@ HeadersLoop:
7878
type webHandler struct {
7979
tlsConfigPath string
8080
handler http.Handler
81-
logger log.Logger
81+
logger *slog.Logger
8282
cache *cache
8383
// bcryptMtx is there to ensure that bcrypt.CompareHashAndPassword is run
8484
// only once in parallel as this is CPU intensive.
@@ -88,7 +88,7 @@ type webHandler struct {
8888
func (u *webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8989
c, err := getConfig(u.tlsConfigPath)
9090
if err != nil {
91-
u.logger.Log("msg", "Unable to parse configuration", "err", err)
91+
u.logger.Error("Unable to parse configuration", "err", err.Error())
9292
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
9393
return
9494
}

web/tls_config.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"crypto/x509"
1919
"errors"
2020
"fmt"
21+
"log/slog"
2122
"net"
2223
"net/http"
2324
"net/url"
@@ -27,8 +28,6 @@ import (
2728
"strings"
2829

2930
"github.com/coreos/go-systemd/v22/activation"
30-
"github.com/go-kit/log"
31-
"github.com/go-kit/log/level"
3231
"github.com/mdlayher/vsock"
3332
config_util "github.com/prometheus/common/config"
3433
"golang.org/x/sync/errgroup"
@@ -267,7 +266,7 @@ func ConfigToTLSConfig(c *TLSConfig) (*tls.Config, error) {
267266

268267
// ServeMultiple starts the server on the given listeners. The FlagConfig is
269268
// also passed on to Serve.
270-
func ServeMultiple(listeners []net.Listener, server *http.Server, flags *FlagConfig, logger log.Logger) error {
269+
func ServeMultiple(listeners []net.Listener, server *http.Server, flags *FlagConfig, logger *slog.Logger) error {
271270
errs := new(errgroup.Group)
272271
for _, l := range listeners {
273272
l := l
@@ -284,13 +283,13 @@ func ServeMultiple(listeners []net.Listener, server *http.Server, flags *FlagCon
284283
// Or instead uses systemd socket activated listeners if WebSystemdSocket in the
285284
// FlagConfig is true.
286285
// The FlagConfig is also passed on to ServeMultiple.
287-
func ListenAndServe(server *http.Server, flags *FlagConfig, logger log.Logger) error {
286+
func ListenAndServe(server *http.Server, flags *FlagConfig, logger *slog.Logger) error {
288287
if flags.WebSystemdSocket == nil && (flags.WebListenAddresses == nil || len(*flags.WebListenAddresses) == 0) {
289288
return ErrNoListeners
290289
}
291290

292291
if flags.WebSystemdSocket != nil && *flags.WebSystemdSocket {
293-
level.Info(logger).Log("msg", "Listening on systemd activated listeners instead of port listeners.")
292+
logger.Info("Listening on systemd activated listeners instead of port listeners.")
294293
listeners, err := activation.Listeners()
295294
if err != nil {
296295
return err
@@ -344,11 +343,11 @@ func parseVsockPort(address string) (uint32, error) {
344343

345344
// Server starts the server on the given listener. Based on the file path
346345
// WebConfigFile in the FlagConfig, TLS or basic auth could be enabled.
347-
func Serve(l net.Listener, server *http.Server, flags *FlagConfig, logger log.Logger) error {
348-
level.Info(logger).Log("msg", "Listening on", "address", l.Addr().String())
346+
func Serve(l net.Listener, server *http.Server, flags *FlagConfig, logger *slog.Logger) error {
347+
logger.Info("Listening on", "address", l.Addr().String())
349348
tlsConfigPath := *flags.WebConfigFile
350349
if tlsConfigPath == "" {
351-
level.Info(logger).Log("msg", "TLS is disabled.", "http2", false, "address", l.Addr().String())
350+
logger.Info("TLS is disabled.", "http2", false, "address", l.Addr().String())
352351
return server.Serve(l)
353352
}
354353

@@ -381,10 +380,10 @@ func Serve(l net.Listener, server *http.Server, flags *FlagConfig, logger log.Lo
381380
server.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
382381
}
383382
// Valid TLS config.
384-
level.Info(logger).Log("msg", "TLS is enabled.", "http2", c.HTTPConfig.HTTP2, "address", l.Addr().String())
383+
logger.Info("TLS is enabled.", "http2", c.HTTPConfig.HTTP2, "address", l.Addr().String())
385384
case errNoTLSConfig:
386385
// No TLS config, back to plain HTTP.
387-
level.Info(logger).Log("msg", "TLS is disabled.", "http2", false, "address", l.Addr().String())
386+
logger.Info("TLS is disabled.", "http2", false, "address", l.Addr().String())
388387
return server.Serve(l)
389388
default:
390389
// Invalid TLS config.
@@ -512,6 +511,6 @@ func (tv *TLSVersion) MarshalYAML() (interface{}, error) {
512511
// tlsConfigPath, TLS or basic auth could be enabled.
513512
//
514513
// Deprecated: Use ListenAndServe instead.
515-
func Listen(server *http.Server, flags *FlagConfig, logger log.Logger) error {
514+
func Listen(server *http.Server, flags *FlagConfig, logger *slog.Logger) error {
516515
return ListenAndServe(server, flags, logger)
517516
}

web/tls_config_test.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"io"
25+
"log/slog"
2526
"net"
2627
"net/http"
2728
"os"
@@ -41,7 +42,7 @@ func OfString(i string) *string {
4142

4243
var (
4344
port = getPort()
44-
testlogger = &testLogger{}
45+
testlogger = slog.New(slog.NewTextHandler(os.Stdout, nil))
4546

4647
ErrorMap = map[string]*regexp.Regexp{
4748
"HTTP Response to HTTPS": regexp.MustCompile(`server gave HTTP response to HTTPS client`),
@@ -74,12 +75,6 @@ var (
7475
}
7576
)
7677

77-
type testLogger struct{}
78-
79-
func (t *testLogger) Log(keyvals ...interface{}) error {
80-
return nil
81-
}
82-
8378
func getPort() string {
8479
listener, err := net.Listen("tcp", ":0")
8580
if err != nil {

0 commit comments

Comments
 (0)