Skip to content

Commit

Permalink
Use log/slog:
Browse files Browse the repository at this point in the history
slog is one less import and by default
provides improved time format and file,
function, and line number attributes.

Signed-off-by: Jacob Weinstock <jakobweinstock@gmail.com>
  • Loading branch information
jacobweinstock committed Nov 19, 2024
1 parent 24e97c2 commit f3a2ddf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
43 changes: 30 additions & 13 deletions cmd/smee/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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:]...)
}

Check warning on line 507 in cmd/smee/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/main.go#L492-L507

Added lines #L492 - L507 were not covered by tests

return a

Check warning on line 509 in cmd/smee/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/main.go#L509

Added line #L509 was not covered by tests
}

return a

Check warning on line 512 in cmd/smee/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/main.go#L512

Added line #L512 was not covered by tests
}
opts := &slog.HandlerOptions{AddSource: true, ReplaceAttr: customAttr}

Check warning on line 514 in cmd/smee/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/main.go#L514

Added line #L514 was not covered by tests
switch level {
case "debug":
config.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
opts.Level = slog.LevelDebug

Check warning on line 517 in cmd/smee/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/main.go#L517

Added line #L517 was not covered by tests
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

Check warning on line 519 in cmd/smee/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/main.go#L519

Added line #L519 was not covered by tests
}
log := slog.New(slog.NewJSONHandler(os.Stdout, opts))

Check warning on line 521 in cmd/smee/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/main.go#L521

Added line #L521 was not covered by tests

return zapr.NewLogger(zapLogger)
return logr.FromSlogHandler(log.Handler())

Check warning on line 523 in cmd/smee/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/smee/main.go#L523

Added line #L523 was not covered by tests
}

func parseTrustedProxies(trustedProxies string) (result []string) {
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f3a2ddf

Please sign in to comment.