From 1bc68f7d198d778797f31a2a97efb37de8f17f79 Mon Sep 17 00:00:00 2001 From: rosstimothy <39066650+rosstimothy@users.noreply.github.com> Date: Wed, 8 Jan 2025 12:14:31 -0500 Subject: [PATCH] Convert logging in build.assets/tooling to use slog (#50805) --- .../cmd/protoc-gen-eventschema/debug.go | 7 ++++--- .../cmd/protoc-gen-eventschema/main.go | 14 ++++++++------ .../tooling/cmd/render-helm-ref/main.go | 19 ++++++++++++------- build.assets/tooling/go.mod | 2 -- build.assets/tooling/go.sum | 5 ----- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/build.assets/tooling/cmd/protoc-gen-eventschema/debug.go b/build.assets/tooling/cmd/protoc-gen-eventschema/debug.go index 575dbe83e17a1..4da934ce4e019 100644 --- a/build.assets/tooling/cmd/protoc-gen-eventschema/debug.go +++ b/build.assets/tooling/cmd/protoc-gen-eventschema/debug.go @@ -25,14 +25,15 @@ package main // inspect what is happening inside the plugin. import ( + "context" "io" + "log/slog" "os" "github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/protoc-gen-gogo/generator" plugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin" "github.com/gravitational/trace" - log "github.com/sirupsen/logrus" ) const pluginInputPathEnvironment = "TELEPORT_PROTOC_READ_FILE" @@ -40,10 +41,10 @@ const pluginInputPathEnvironment = "TELEPORT_PROTOC_READ_FILE" func readRequest() (*plugin.CodeGeneratorRequest, error) { inputPath := os.Getenv(pluginInputPathEnvironment) if inputPath == "" { - log.Error(trace.BadParameter("When built with the 'debug' tag, the input path must be set through the environment variable: %s", pluginInputPathEnvironment)) + slog.ErrorContext(context.Background(), "When built with the 'debug' tag, the input path must be set through the TELEPORT_PROTOC_READ_FILE environment variable") os.Exit(-1) } - log.Infof("This is a debug build, the protoc request is read from the file: '%s'", inputPath) + slog.InfoContext(context.Background(), "This is a debug build, the protoc request is read from provided file", "file", inputPath) req, err := readRequestFromFile(inputPath) if err != nil { diff --git a/build.assets/tooling/cmd/protoc-gen-eventschema/main.go b/build.assets/tooling/cmd/protoc-gen-eventschema/main.go index 8480ad31a7594..a2d2210e0386a 100644 --- a/build.assets/tooling/cmd/protoc-gen-eventschema/main.go +++ b/build.assets/tooling/cmd/protoc-gen-eventschema/main.go @@ -19,21 +19,23 @@ package main import ( + "context" + "log/slog" "os" - - log "github.com/sirupsen/logrus" ) func main() { - log.SetLevel(log.DebugLevel) - log.SetOutput(os.Stderr) + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) + slog.SetDefault(logger) + + ctx := context.Background() req, err := readRequest() if err != nil { - log.WithError(err).Error("Failed to read request") + logger.ErrorContext(ctx, "Failed to read request", "error", err) os.Exit(-1) } if err := handleRequest(req); err != nil { - log.WithError(err).Error("Failed to generate schema") + logger.ErrorContext(ctx, "Failed to generate schema", "error", err) os.Exit(-1) } } diff --git a/build.assets/tooling/cmd/render-helm-ref/main.go b/build.assets/tooling/cmd/render-helm-ref/main.go index 2026c2147672a..5cfa13b35ebeb 100644 --- a/build.assets/tooling/cmd/render-helm-ref/main.go +++ b/build.assets/tooling/cmd/render-helm-ref/main.go @@ -20,15 +20,16 @@ package main import ( "bufio" + "context" "encoding/json" "flag" "fmt" + "log/slog" "os" "regexp" "strings" "github.com/gravitational/trace" - log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" "helm.sh/helm/v3/pkg/chart/loader" ) @@ -52,14 +53,15 @@ func main() { flag.StringVar(&outputPath, "output", "-", "Path of the generated markdown reference, '-' means stdout.") flag.Parse() + ctx := context.Background() if chartPath == "" { - log.Error(trace.BadParameter("chart path must be specified")) + slog.ErrorContext(ctx, "chart path must be specified") os.Exit(1) } reference, err := parseAndRender(chartPath) if err != nil { - log.Errorf("failed parsing chart and rendering reference: %s", err) + slog.ErrorContext(ctx, "failed parsing chart and rendering reference", "error", err) os.Exit(1) } @@ -69,10 +71,10 @@ func main() { } err = os.WriteFile(outputPath, reference, 0o644) if err != nil { - log.Errorf("failed writing file: %s", err) + slog.ErrorContext(ctx, "failed writing file", "error", err) os.Exit(1) } - log.Infof("File %s successfully written", outputPath) + slog.InfoContext(ctx, "File successfully written", "file_path", outputPath) } func parseAndRender(chartPath string) ([]byte, error) { @@ -106,7 +108,10 @@ func parseAndRender(chartPath string) ([]byte, error) { if value.Kind != "" && value.Default == "" { defaultValue, err := getDefaultForValue(value.Name, chrt.Values) if err != nil { - log.Warnf("failed to get default for value %s, error: %s", value.Name, err) + slog.WarnContext(context.Background(), "failed to look up default value", + "value", value.Name, + "error", err, + ) } else { value.Default = string(defaultValue) } @@ -227,7 +232,7 @@ func cleanLine(line string) string { return "" } if line2[0] != '#' { - log.Warnf("Misformatted line: %s", line) + slog.WarnContext(context.Background(), "Misformatted line", "line", line) return "" } return line2[2:] diff --git a/build.assets/tooling/go.mod b/build.assets/tooling/go.mod index e87bf5d8680d4..d726a7c08fd94 100644 --- a/build.assets/tooling/go.mod +++ b/build.assets/tooling/go.mod @@ -10,7 +10,6 @@ require ( github.com/gogo/protobuf v1.3.2 github.com/google/go-github/v41 v41.0.0 github.com/gravitational/trace v1.4.0 - github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.10.0 github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 @@ -48,7 +47,6 @@ require ( github.com/xhit/go-str2duration/v2 v2.1.0 // indirect golang.org/x/crypto v0.31.0 // indirect golang.org/x/net v0.33.0 // indirect - golang.org/x/sys v0.28.0 // indirect golang.org/x/text v0.21.0 // indirect golang.org/x/tools v0.26.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect diff --git a/build.assets/tooling/go.sum b/build.assets/tooling/go.sum index 7e613ad03bde2..7391483f6ceb1 100644 --- a/build.assets/tooling/go.sum +++ b/build.assets/tooling/go.sum @@ -881,8 +881,6 @@ github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfF github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk= github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k= github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4= github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= @@ -1183,7 +1181,6 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -1197,8 +1194,6 @@ golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= -golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=