From 2b237d59bdd85a2c407d63759727498cbedf2db9 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 19 Feb 2024 14:42:59 +0100 Subject: [PATCH] added slog-multi + rewritten code to use it --- bazel/toolchains/go_module_deps.bzl | 16 ++++++++++++ cli/internal/cmd/BUILD.bazel | 1 + cli/internal/cmd/apply.go | 40 ++++++++++++++++++++++++----- go.mod | 3 +++ go.sum | 4 +++ 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/bazel/toolchains/go_module_deps.bzl b/bazel/toolchains/go_module_deps.bzl index 4f36e9b874..e43a9a5f35 100644 --- a/bazel/toolchains/go_module_deps.bzl +++ b/bazel/toolchains/go_module_deps.bzl @@ -4435,6 +4435,22 @@ def go_dependencies(): sum = "h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=", version = "v1.0.0", ) + go_repository( + name = "com_github_samber_lo", + build_file_generation = "on", + build_file_proto_mode = "disable_global", + importpath = "github.com/samber/lo", + sum = "h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=", + version = "v1.38.1", + ) + go_repository( + name = "com_github_samber_slog_multi", + build_file_generation = "on", + build_file_proto_mode = "disable_global", + importpath = "github.com/samber/slog-multi", + sum = "h1:6BVH9uHGAsiGkbbtQgAOQJMpKgV8unMrHhhJaw+X1EQ=", + version = "v1.0.2", + ) go_repository( name = "com_github_sassoftware_relic", build_file_generation = "on", diff --git a/cli/internal/cmd/BUILD.bazel b/cli/internal/cmd/BUILD.bazel index afccfc643b..8c300b62ad 100644 --- a/cli/internal/cmd/BUILD.bazel +++ b/cli/internal/cmd/BUILD.bazel @@ -97,6 +97,7 @@ go_library( "@com_github_google_uuid//:uuid", "@com_github_mattn_go_isatty//:go-isatty", "@com_github_rogpeppe_go_internal//diff", + "@com_github_samber_slog_multi//:slog-multi", "@com_github_siderolabs_talos_pkg_machinery//config/encoder", "@com_github_spf13_afero//:afero", "@com_github_spf13_cobra//:cobra", diff --git a/cli/internal/cmd/apply.go b/cli/internal/cmd/apply.go index 5b69baf3ca..ea7679a508 100644 --- a/cli/internal/cmd/apply.go +++ b/cli/internal/cmd/apply.go @@ -13,7 +13,9 @@ import ( "fmt" "io" "io/fs" + "log/slog" "net" + "os" "path/filepath" "slices" "strings" @@ -38,6 +40,7 @@ import ( "github.com/edgelesssys/constellation/v2/internal/kms/uri" "github.com/edgelesssys/constellation/v2/internal/semver" "github.com/edgelesssys/constellation/v2/internal/versions" + "github.com/samber/slog-multi" "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -224,7 +227,10 @@ func runApply(cmd *cobra.Command, _ []string) error { } fileHandler := file.NewHandler(afero.NewOsFs()) - logger := debugFileLogger{fileHandler: fileHandler, log: log} + logger, err := newDebugFileLogger(cmd, fileHandler) + if err != nil { + return err + } newDialer := func(validator atls.Validator) *dialer.Dialer { return dialer.New(nil, validator, &net.Dialer{}) @@ -860,13 +866,33 @@ type imageFetcher interface { ) (string, error) } -type debugFileLogger struct { - fileHandler file.Handler - log debugLog +func newDebugFileLogger(cmd *cobra.Command, fileHandler file.Handler) (debugLog, error) { + logLvl := slog.LevelInfo + debugLog, err := cmd.Flags().GetBool("debug") + if err != nil { + return nil, err + } + if debugLog { + logLvl = slog.LevelDebug + } + + fileWriter := &fileWriter{ + fileHandler: fileHandler, + } + return slog.New( + slogmulti.Fanout( + slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{AddSource: true, Level: logLvl}), // first handler: stderr at log level + slog.NewJSONHandler(fileWriter, &slog.HandlerOptions{AddSource: true, Level: slog.LevelDebug}), // second handler: debug JSON log to file + ), + ), nil } -func (l debugFileLogger) Debug(msg string, args ...any) { - l.log.Debug(msg, args...) +type fileWriter struct { + fileHandler file.Handler +} - _ = l.fileHandler.Write(constants.CLIDebugLogFile, []byte(msg+"\n"), file.OptAppend) +// Write satisfies the io.Writer interface by writing a message to file. +func (l *fileWriter) Write(msg []byte) (int, error) { + err := l.fileHandler.Write(constants.CLIDebugLogFile, msg, file.OptAppend) + return len(msg), err } diff --git a/go.mod b/go.mod index 82c33d2ac3..968bf44d8e 100644 --- a/go.mod +++ b/go.mod @@ -111,6 +111,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/regclient/regclient v0.5.5 github.com/rogpeppe/go-internal v1.11.0 + github.com/samber/slog-multi v1.0.2 github.com/schollz/progressbar/v3 v3.13.1 github.com/siderolabs/talos/pkg/machinery v1.4.6 github.com/sigstore/rekor v1.2.2 @@ -152,6 +153,8 @@ require ( sigs.k8s.io/yaml v1.4.0 ) +require github.com/samber/lo v1.38.1 // indirect + require ( cloud.google.com/go v0.110.8 // indirect cloud.google.com/go/iam v1.1.2 // indirect diff --git a/go.sum b/go.sum index e6b92cb26b..2134e1349a 100644 --- a/go.sum +++ b/go.sum @@ -894,6 +894,10 @@ github.com/rubenv/sql-migrate v1.5.2 h1:bMDqOnrJVV/6JQgQ/MxOpU+AdO8uzYYA/TxFUBzF github.com/rubenv/sql-migrate v1.5.2/go.mod h1:H38GW8Vqf8F0Su5XignRyaRcbXbJunSWxs+kmzlg0Is= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= +github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= +github.com/samber/slog-multi v1.0.2 h1:6BVH9uHGAsiGkbbtQgAOQJMpKgV8unMrHhhJaw+X1EQ= +github.com/samber/slog-multi v1.0.2/go.mod h1:uLAvHpGqbYgX4FSL0p1ZwoLuveIAJvBECtE07XmYvFo= github.com/sassoftware/relic v7.2.1+incompatible h1:Pwyh1F3I0r4clFJXkSI8bOyJINGqpgjJU3DYAZeI05A= github.com/sassoftware/relic v7.2.1+incompatible/go.mod h1:CWfAxv73/iLZ17rbyhIEq3K9hs5w6FpNMdUT//qR+zk= github.com/sassoftware/relic/v7 v7.5.5 h1:2ZUM6ovo3STCAp0hZnO9nQY9lOB8OyfneeYIi4YUxMU=