Skip to content

Commit

Permalink
Merge pull request #552 from jacobweinstock/use-slog
Browse files Browse the repository at this point in the history
Move to slog logr implementation:

## Description

slog is one less import and by default provides improved time format and file, function, and line number attributes.

<!--- Please describe what this PR is going to change -->

## Why is this needed

<!--- Link to issue you have raised -->

Fixes: #

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->


## How are existing users impacted? What migration steps/scripts do we need?

<!--- Fixes a bug, unblocks installation, removes a component of the stack etc -->
<!--- Requires a DB migration script, etc. -->


## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [ ] added unit or e2e tests
- [ ] provided instructions on how to upgrade
  • Loading branch information
jacobweinstock authored Nov 19, 2024
2 parents 24e97c2 + f3a2ddf commit 4e5c46c
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:]...)
}

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) {
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 4e5c46c

Please sign in to comment.