Skip to content

Commit

Permalink
use slog instead of zap (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Aug 17, 2023
1 parent 00f53f5 commit 8a871c1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 83 deletions.
4 changes: 2 additions & 2 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"os"
"strings"
"testing"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -108,7 +108,7 @@ func (c *test[R]) newMockConfig(t *testing.T) (*client.MetalMockClient, *bytes.B
fs: fs,
client: client,
out: &out,
log: zaptest.NewLogger(t).Sugar(),
log: slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{})),
comp: &completion.Completion{},
}
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newLoginCmd(c *config) *cobra.Command {
TokenHandler: handler,
Console: console,
Debug: viper.GetBool("debug"),
Log: c.log.Desugar(),
Log: c.log,
}

fmt.Fprintln(c.out)
Expand Down
32 changes: 7 additions & 25 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package cmd
import (
"fmt"
"io"
"log/slog"
"os"
"strings"

metalgo "github.com/metal-stack/metal-go"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metalctl/cmd/completion"
"github.com/metal-stack/metalctl/pkg/api"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand All @@ -25,7 +24,7 @@ type config struct {
driverURL string
comp *completion.Completion
client metalgo.Client
log *zap.SugaredLogger
log *slog.Logger
describePrinter printers.Printer
listPrinter printers.Printer
}
Expand Down Expand Up @@ -182,11 +181,12 @@ func initConfigWithViperCtx(c *config) error {
c.describePrinter = defaultToYAMLPrinter(c.out)

if c.log == nil {
logger, err := newLogger()
if err != nil {
return fmt.Errorf("error creating logger: %w", err)
opts := &slog.HandlerOptions{}
if viper.GetBool("debug") {
opts.Level = slog.LevelDebug
}
c.log = logger
jsonHandler := slog.NewJSONHandler(os.Stdout, opts)
c.log = slog.New(jsonHandler)
}

if c.client != nil {
Expand Down Expand Up @@ -225,24 +225,6 @@ func initConfigWithViperCtx(c *config) error {
return nil
}

func newLogger() (*zap.SugaredLogger, error) {
cfg := zap.NewProductionConfig()
if viper.GetBool("debug") {
cfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
cfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
cfg.EncoderConfig.TimeKey = "timestamp"
cfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder

l, err := cfg.Build()
if err != nil {
return nil, err
}

return l.Sugar(), nil
}

func recursiveAutoGenDisable(cmd *cobra.Command) {
cmd.DisableAutoGenTag = true
for _, child := range cmd.Commands() {
Expand Down
33 changes: 16 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/go-openapi/runtime v0.26.0
github.com/go-openapi/strfmt v0.21.7
github.com/google/go-cmp v0.5.9
github.com/metal-stack/metal-go v0.22.8
github.com/metal-stack/metal-lib v0.12.5
github.com/metal-stack/metal-go v0.22.12
github.com/metal-stack/metal-lib v0.13.1
github.com/metal-stack/updater v1.1.5
github.com/metal-stack/v v1.0.3
github.com/olekukonko/tablewriter v0.0.5
Expand All @@ -19,7 +19,6 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.24.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.27.4
)
Expand All @@ -30,12 +29,12 @@ require (
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230426101702-58e86b294756 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230626094100-7e9e0395ebec // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/akutz/memconn v0.1.0 // indirect
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/avast/retry-go/v4 v4.3.4 // indirect
github.com/avast/retry-go/v4 v4.5.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.18.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.18.22 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.21 // indirect
Expand All @@ -49,14 +48,14 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.9 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.18.10 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cheggaaa/pb/v3 v3.1.4 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/coreos/go-iptables v0.6.0 // indirect
github.com/coreos/go-oidc/v3 v3.6.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dblohm7/wingoes v0.0.0-20230803162905-5c6286bb8c6e // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/emicklei/go-restful-openapi/v2 v2.9.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
Expand Down Expand Up @@ -151,21 +150,21 @@ require (
go.mongodb.org/mongo-driver v1.12.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
go4.org/mem v0.0.0-20220726221520-4f986261bf13 // indirect
go4.org/netipx v0.0.0-20230303233057-f1b76eb4bb35 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
go4.org/netipx v0.0.0-20230728180743-ad4cb58a6516 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.11.0 // indirect
golang.org/x/tools v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect
golang.zx2c4.com/wireguard/windows v0.5.3 // indirect
Expand All @@ -180,5 +179,5 @@ require (
nhooyr.io/websocket v1.8.7 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
tailscale.com v1.46.0 // indirect
tailscale.com v1.48.0 // indirect
)
Loading

0 comments on commit 8a871c1

Please sign in to comment.