From 8641db9d5f754df1e6a662dc3788988bd75a714a Mon Sep 17 00:00:00 2001 From: pablochacin Date: Tue, 30 Jul 2024 19:57:29 +0200 Subject: [PATCH] use slog (#19) Signed-off-by: Pablo Chacin --- apiserver.go | 18 +++++++++++------- cache_server.go | 15 ++++++++++----- cmd/server.go | 25 +++++++++++++++---------- go.mod | 4 +--- go.sum | 21 ++++----------------- log.go | 17 +++++++++++++++++ 6 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 log.go diff --git a/apiserver.go b/apiserver.go index 90f647c..3d7e30a 100644 --- a/apiserver.go +++ b/apiserver.go @@ -6,9 +6,8 @@ import ( "encoding/json" "fmt" "io" + "log/slog" "net/http" - - "github.com/sirupsen/logrus" ) // BuildRequest defines a request to the build service @@ -38,13 +37,13 @@ type BuildResponse struct { // APIServerConfig defines the configuration for the APIServer type APIServerConfig struct { BuildService BuildService - Log *logrus.Logger + Log *slog.Logger } // APIServer defines a k6build API server type APIServer struct { srv BuildService - log *logrus.Logger + log *slog.Logger } // NewAPIServer creates a new build service API server @@ -52,7 +51,12 @@ type APIServer struct { func NewAPIServer(config APIServerConfig) *APIServer { log := config.Log if log == nil { - log = &logrus.Logger{Out: io.Discard} + log = slog.New( + slog.NewTextHandler( + io.Discard, + &slog.HandlerOptions{}, + ), + ) } return &APIServer{ srv: config.BuildService, @@ -82,7 +86,7 @@ func (a *APIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - a.log.Debugf("processing request %s", req.String()) + a.log.Debug("processing", "request", req.String()) artifact, err := a.srv.Build( //nolint:contextcheck context.Background(), @@ -96,7 +100,7 @@ func (a *APIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - a.log.Debugf("returning artifact %s", artifact.String()) + a.log.Debug("returning", "artifact", artifact.String()) resp.Artifact = artifact w.WriteHeader(http.StatusOK) diff --git a/cache_server.go b/cache_server.go index dcd6986..8ce0b9d 100644 --- a/cache_server.go +++ b/cache_server.go @@ -6,9 +6,8 @@ import ( "errors" "fmt" "io" + "log/slog" "net/http" - - "github.com/sirupsen/logrus" ) // CacheServerResponse is the response to a cache server request @@ -21,21 +20,27 @@ type CacheServerResponse struct { type CacheServer struct { baseURL string cache Cache - log *logrus.Logger + log *slog.Logger } // CacheServerConfig defines the configuration for the APIServer type CacheServerConfig struct { BaseURL string Cache Cache - Log *logrus.Logger + Log *slog.Logger } // NewCacheServer returns a CacheServer backed by a cache func NewCacheServer(config CacheServerConfig) http.Handler { log := config.Log + if log == nil { - log = &logrus.Logger{Out: io.Discard} + log = slog.New( + slog.NewTextHandler( + io.Discard, + &slog.HandlerOptions{}, + ), + ) } cacheSrv := &CacheServer{ baseURL: config.BaseURL, diff --git a/cmd/server.go b/cmd/server.go index 87887b8..b451d1a 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -2,13 +2,13 @@ package cmd import ( "fmt" + "log/slog" "net/http" "os" "github.com/grafana/k6build" "github.com/grafana/k6catalog" "github.com/grafana/k6foundry" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -34,7 +34,6 @@ func NewServer() *cobra.Command { //nolint:funlen copyGoEnv bool port int verbose bool - log *logrus.Logger logLevel string ) @@ -48,15 +47,20 @@ func NewServer() *cobra.Command { //nolint:funlen // this is needed to prevent cobra to print errors reported by subcommands in the stderr SilenceErrors: true, RunE: func(cmd *cobra.Command, _ []string) error { - ll, err := logrus.ParseLevel(logLevel) + // set log + ll, err := k6build.ParseLogLevel(logLevel) if err != nil { return fmt.Errorf("parsing log level %w", err) } - log = &logrus.Logger{ - Out: os.Stderr, - Formatter: new(logrus.TextFormatter), - Level: ll, - } + + log := slog.New( + slog.NewTextHandler( + os.Stderr, + &slog.HandlerOptions{ + Level: ll, + }, + ), + ) catalog, err := k6catalog.NewCatalogFromJSON(catalog) if err != nil { @@ -64,6 +68,7 @@ func NewServer() *cobra.Command { //nolint:funlen } builderOpts := k6foundry.NativeBuilderOpts{ + Logger: log, GoOpts: k6foundry.GoOpts{ Env: buildEnv, CopyGoEnv: copyGoEnv, @@ -118,10 +123,10 @@ func NewServer() *cobra.Command { //nolint:funlen srv.Handle("/cache/", http.StripPrefix("/cache", cacheSrv)) listerAddr := fmt.Sprintf("localhost:%d", port) - log.Infof("starting server at %s", listerAddr) + log.Info("starting server", "address", listerAddr) err = http.ListenAndServe(listerAddr, srv) //nolint:gosec if err != nil { - log.Infof("server ended with error %s", err.Error()) + log.Info("server ended", "error", err.Error()) } log.Info("ending server") diff --git a/go.mod b/go.mod index 46645be..dd6d777 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22.2 require ( github.com/grafana/clireadme v0.1.0 github.com/grafana/k6catalog v0.1.0 - github.com/grafana/k6foundry v0.1.4 + github.com/grafana/k6foundry v0.2.0 github.com/spf13/cobra v1.8.0 ) @@ -13,10 +13,8 @@ require ( github.com/Masterminds/semver v1.5.0 // indirect github.com/google/go-cmp v0.6.0 github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/sirupsen/logrus v1.9.3 github.com/spf13/pflag v1.0.5 // indirect golang.org/x/mod v0.18.0 // indirect - golang.org/x/sys v0.21.0 // indirect ) retract v0.0.0 // premature publishing diff --git a/go.sum b/go.sum index 9661de2..e88bfa2 100644 --- a/go.sum +++ b/go.sum @@ -1,37 +1,24 @@ github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/grafana/clireadme v0.1.0 h1:KYEYSnYdSzmHf3bufaK6fQZ5j4dzvM/T+G6Ba+qNnAM= github.com/grafana/clireadme v0.1.0/go.mod h1:Wy4KIG2ZBGMYAYyF9l7qAy+yoJVasqk/txsRgoRI3gc= github.com/grafana/k6catalog v0.1.0 h1:jLmbmB3EUJ+zyQG3hWy6dWbtMjvTkvJNx1d4LX8it6I= github.com/grafana/k6catalog v0.1.0/go.mod h1:8R9eXAh2nb69+drkj0rZ4aemso0jcwCbPP6Q3E5LqCw= -github.com/grafana/k6foundry v0.1.4 h1:6kPQeU1nSJiV6Y42sVRESuVfpgt/uiCwoYD729hRypM= -github.com/grafana/k6foundry v0.1.4/go.mod h1:b6n4InFgXl+3yPobmlyJfcJmLozU9CI9IIUuq8YqEiM= +github.com/grafana/k6foundry v0.2.0 h1:+aE5wuCP0XNGNsxM7UiPj9hyw4RdWeW929PuGwLWIlg= +github.com/grafana/k6foundry v0.2.0/go.mod h1:b6n4InFgXl+3yPobmlyJfcJmLozU9CI9IIUuq8YqEiM= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/log.go b/log.go new file mode 100644 index 0000000..98e345f --- /dev/null +++ b/log.go @@ -0,0 +1,17 @@ +package k6build + +import ( + "fmt" + "log/slog" +) + +// ParseLogLevel parses the level from a string +func ParseLogLevel(levelString string) (slog.Level, error) { + var level slog.Level + err := level.UnmarshalText([]byte(levelString)) + if err != nil { + return level, fmt.Errorf("parsing log level from string: %w", err) + } + + return level, nil +}