diff --git a/cmd/smee/main.go b/cmd/smee/main.go index 8cefba0f..6c839130 100644 --- a/cmd/smee/main.go +++ b/cmd/smee/main.go @@ -5,18 +5,19 @@ import ( "errors" "flag" "fmt" + "log/slog" "net" "net/netip" "net/url" "os" "os/signal" "path" + "path/filepath" "strings" "syscall" "time" "github.com/go-logr/logr" - "github.com/go-logr/zapr" "github.com/insomniacslk/dhcp/dhcpv4" "github.com/insomniacslk/dhcp/dhcpv4/server4" "github.com/tinkerbell/ipxedust" @@ -31,8 +32,6 @@ import ( "github.com/tinkerbell/smee/internal/metric" "github.com/tinkerbell/smee/internal/otel" "github.com/tinkerbell/smee/internal/syslog" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" "golang.org/x/sync/errgroup" ) @@ -488,22 +487,40 @@ func (c *config) dhcpHandler(ctx context.Context, log logr.Logger) (server.Handl return nil, errors.New("invalid dhcp mode") } -// defaultLogger is zap logr implementation. +// defaultLogger uses the slog logr implementation. func defaultLogger(level string) logr.Logger { - config := zap.NewProductionConfig() - config.OutputPaths = []string{"stdout"} + // source file and function can be long. This makes the logs less readable. + // truncate source file and function to last 3 parts for improved readability. + customAttr := func(_ []string, a slog.Attr) slog.Attr { + if a.Key == slog.SourceKey { + ss, ok := a.Value.Any().(*slog.Source) + if !ok || ss == nil { + return a + } + f := strings.Split(ss.Function, "/") + if len(f) > 3 { + ss.Function = filepath.Join(f[len(f)-3:]...) + } + p := strings.Split(ss.File, "/") + if len(p) > 3 { + ss.File = filepath.Join(p[len(p)-3:]...) + } + + return a + } + + return a + } + opts := &slog.HandlerOptions{AddSource: true, ReplaceAttr: customAttr} switch level { case "debug": - config.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel) + opts.Level = slog.LevelDebug default: - config.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel) - } - zapLogger, err := config.Build() - if err != nil { - panic(fmt.Sprintf("who watches the watchmen (%v)?", err)) + opts.Level = slog.LevelInfo } + log := slog.New(slog.NewJSONHandler(os.Stdout, opts)) - return zapr.NewLogger(zapLogger) + return logr.FromSlogHandler(log.Handler()) } func parseTrustedProxies(trustedProxies string) (result []string) { diff --git a/go.mod b/go.mod index 5d997330..d832edb6 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,11 @@ go 1.23 toolchain go1.23.2 require ( - github.com/ccoveille/go-safecast v1.1.0 + github.com/ccoveille/go-safecast v1.2.0 github.com/fsnotify/fsnotify v1.8.0 github.com/ghodss/yaml v1.0.0 github.com/go-logr/logr v1.4.2 github.com/go-logr/stdr v1.2.2 - github.com/go-logr/zapr v1.3.0 github.com/google/go-cmp v0.6.0 github.com/insomniacslk/dhcp v0.0.0-20240829085014-a3a4c1f04475 github.com/packethost/xff v0.0.0-20190305172552-d3e9190c41b3 @@ -24,7 +23,6 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.32.0 go.opentelemetry.io/otel/sdk v1.32.0 go.opentelemetry.io/otel/trace v1.32.0 - go.uber.org/zap v1.27.0 golang.org/x/net v0.31.0 golang.org/x/sync v0.9.0 golang.org/x/sys v0.27.0 @@ -87,7 +85,6 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0 // indirect go.opentelemetry.io/otel/metric v1.32.0 // indirect go.opentelemetry.io/proto/otlp v1.3.1 // indirect - go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.29.0 // indirect golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect golang.org/x/oauth2 v0.23.0 // indirect diff --git a/go.sum b/go.sum index c3e9a4fa..14f282aa 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s= dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/ccoveille/go-safecast v1.1.0 h1:iHKNWaZm+OznO7Eh6EljXPjGfGQsSfa6/sxPlIEKO+g= -github.com/ccoveille/go-safecast v1.1.0/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8= +github.com/ccoveille/go-safecast v1.2.0 h1:H4X7aosepsU1Mfk+098CTdKpsDH0cfYJ2RmwXFjgvfc= +github.com/ccoveille/go-safecast v1.2.0/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=