From ec7f29465ea95a02904eebc38a925798b56b5b6f Mon Sep 17 00:00:00 2001 From: miampf Date: Wed, 31 Jan 2024 16:08:53 +0100 Subject: [PATCH] rewrote every slog logger to add source --- bootstrapper/cmd/bootstrapper/main.go | 2 +- cli/internal/cmd/log.go | 4 ++-- debugd/cmd/debugd/debugd.go | 2 +- debugd/internal/cdbg/cmd/deploy.go | 3 +-- disk-mapper/cmd/main.go | 2 +- disk-mapper/internal/test/benchmark_test.go | 3 ++- disk-mapper/internal/test/integration_test.go | 3 ++- e2e/malicious-join/malicious-join.go | 3 ++- hack/bazel-deps-mirror/check.go | 4 ++-- hack/bazel-deps-mirror/fix.go | 4 ++-- hack/bazel-deps-mirror/upgrade.go | 4 ++-- hack/cli-k8s-compatibility/main.go | 3 ++- hack/oci-pin/codegen.go | 3 ++- hack/oci-pin/merge.go | 3 ++- hack/oci-pin/sum.go | 3 ++- hack/qemu-metadata-api/main.go | 3 ++- image/upload/internal/cmd/info.go | 4 ++-- image/upload/internal/cmd/measurementsenvelope.go | 4 ++-- image/upload/internal/cmd/measurementsmerge.go | 4 ++-- image/upload/internal/cmd/measurementsupload.go | 4 ++-- image/upload/internal/cmd/uplosi.go | 4 ++-- internal/api/attestationconfigapi/cli/delete.go | 6 +++--- internal/api/attestationconfigapi/cli/upload.go | 3 ++- internal/api/versionsapi/cli/add.go | 4 ++-- internal/api/versionsapi/cli/latest.go | 4 ++-- internal/api/versionsapi/cli/list.go | 4 ++-- internal/api/versionsapi/cli/rm.go | 4 ++-- internal/logger/log.go | 13 +++++++++++++ joinservice/cmd/main.go | 2 +- keyservice/cmd/main.go | 2 +- measurement-reader/cmd/main.go | 3 ++- s3proxy/cmd/main.go | 5 ++--- upgrade-agent/cmd/main.go | 2 +- verify/cmd/main.go | 2 +- 34 files changed, 72 insertions(+), 51 deletions(-) diff --git a/bootstrapper/cmd/bootstrapper/main.go b/bootstrapper/cmd/bootstrapper/main.go index c9267debfd..6ba683a1fc 100644 --- a/bootstrapper/cmd/bootstrapper/main.go +++ b/bootstrapper/cmd/bootstrapper/main.go @@ -47,7 +47,7 @@ func main() { gRPCDebug := flag.Bool("debug", false, "Enable gRPC debug logging") verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription) flag.Parse() - log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logger.VerbosityFromInt(*verbosity)})).WithGroup("bootstrapper") + log := logger.NewJSONLogger(logger.VerbosityFromInt(*verbosity)).WithGroup("bootstrapper") if *gRPCDebug { logger.ReplaceGRPCLogger(log.WithGroup("gRPC")) diff --git a/cli/internal/cmd/log.go b/cli/internal/cmd/log.go index 741a719b8c..d86f1686ff 100644 --- a/cli/internal/cmd/log.go +++ b/cli/internal/cmd/log.go @@ -8,8 +8,8 @@ package cmd import ( "log/slog" - "os" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -27,5 +27,5 @@ func newCLILogger(cmd *cobra.Command) (debugLog, error) { logLvl = slog.LevelDebug } - return slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logLvl})), nil + return logger.NewTextLogger(logLvl), nil } diff --git a/debugd/cmd/debugd/debugd.go b/debugd/cmd/debugd/debugd.go index ebf92cee2b..4140687f72 100644 --- a/debugd/cmd/debugd/debugd.go +++ b/debugd/cmd/debugd/debugd.go @@ -46,7 +46,7 @@ func main() { verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription) flag.Parse() - log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logger.VerbosityFromInt(*verbosity)})) + log := logger.NewJSONLogger(logger.VerbosityFromInt(*verbosity)) fs := afero.NewOsFs() streamer := streamer.New(fs) filetransferer := filetransfer.New(log.WithGroup("filetransfer"), streamer, filetransfer.DontShowProgress) diff --git a/debugd/internal/cdbg/cmd/deploy.go b/debugd/internal/cdbg/cmd/deploy.go index 4f4d2d8f0e..95c3d147e0 100644 --- a/debugd/internal/cdbg/cmd/deploy.go +++ b/debugd/internal/cdbg/cmd/deploy.go @@ -12,7 +12,6 @@ import ( "fmt" "log/slog" "net" - "os" "path/filepath" "strconv" "sync" @@ -62,7 +61,7 @@ func runDeploy(cmd *cobra.Command, _ []string) error { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logger.VerbosityFromInt(verbosity)})) + log := logger.NewTextLogger(logger.VerbosityFromInt(verbosity)) force, err := cmd.Flags().GetBool("force") if err != nil { return fmt.Errorf("getting force flag: %w", err) diff --git a/disk-mapper/cmd/main.go b/disk-mapper/cmd/main.go index 773d93d3a2..56b1c1812a 100644 --- a/disk-mapper/cmd/main.go +++ b/disk-mapper/cmd/main.go @@ -52,7 +52,7 @@ func main() { verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription) flag.Parse() - log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logger.VerbosityFromInt(*verbosity)})) + log := logger.NewJSONLogger(logger.VerbosityFromInt(*verbosity)) log.With(slog.String("version", constants.BinaryVersion().String()), slog.String("cloudProvider", *csp)). Info("Starting disk-mapper") diff --git a/disk-mapper/internal/test/benchmark_test.go b/disk-mapper/internal/test/benchmark_test.go index fafd043ee2..35486c921d 100644 --- a/disk-mapper/internal/test/benchmark_test.go +++ b/disk-mapper/internal/test/benchmark_test.go @@ -17,6 +17,7 @@ import ( "github.com/edgelesssys/constellation/v2/disk-mapper/internal/diskencryption" "github.com/martinjungblut/go-cryptsetup" + "github.com/edgelesssys/constellation/v2/internal/logger" ) func BenchmarkMapper(b *testing.B) { @@ -39,7 +40,7 @@ func BenchmarkMapper(b *testing.B) { } passphrase := "benchmark" - mapper, free, err := diskencryption.New(testPath, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo}))) + mapper, free, err := diskencryption.New(testPath, logger.NewTextLogger(slog.LevelInfo)) if err != nil { b.Fatal("Failed to create mapper:", err) } diff --git a/disk-mapper/internal/test/integration_test.go b/disk-mapper/internal/test/integration_test.go index 845f21e219..d3642916c4 100644 --- a/disk-mapper/internal/test/integration_test.go +++ b/disk-mapper/internal/test/integration_test.go @@ -27,6 +27,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/goleak" + "github.com/edgelesssys/constellation/v2/internal/logger" ) const ( @@ -103,7 +104,7 @@ func TestMapper(t *testing.T) { require.NoError(setup(1), "failed to setup test disk") defer func() { require.NoError(teardown(), "failed to delete test disk") }() - mapper, free, err := diskencryption.New(devicePath, slog.New(slog.NewTextHandler(os.Stderr, nil))) + mapper, free, err := diskencryption.New(devicePath, logger.NewTextLogger(slog.LevelInfo)) require.NoError(err, "failed to initialize crypt device") defer free() diff --git a/e2e/malicious-join/malicious-join.go b/e2e/malicious-join/malicious-join.go index c823bdc5f4..9810358800 100644 --- a/e2e/malicious-join/malicious-join.go +++ b/e2e/malicious-join/malicious-join.go @@ -20,11 +20,12 @@ import ( "github.com/edgelesssys/constellation/v2/internal/attestation/variant" "github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider" "github.com/edgelesssys/constellation/v2/internal/grpc/dialer" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/joinservice/joinproto" ) func main() { - log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) + log := logger.NewJSONLogger(slog.LevelDebug) jsEndpoint := flag.String("js-endpoint", "", "Join service endpoint to use.") csp := flag.String("csp", "", "Cloud service provider to use.") diff --git a/hack/bazel-deps-mirror/check.go b/hack/bazel-deps-mirror/check.go index 09a367904b..64bd68b738 100644 --- a/hack/bazel-deps-mirror/check.go +++ b/hack/bazel-deps-mirror/check.go @@ -11,12 +11,12 @@ import ( "errors" "fmt" "log/slog" - "os" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/bazelfiles" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/issues" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/mirror" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/rules" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -39,7 +39,7 @@ func runCheck(cmd *cobra.Command, _ []string) error { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) filesHelper, err := bazelfiles.New() diff --git a/hack/bazel-deps-mirror/fix.go b/hack/bazel-deps-mirror/fix.go index c7fd6e4bc6..9a327ee277 100644 --- a/hack/bazel-deps-mirror/fix.go +++ b/hack/bazel-deps-mirror/fix.go @@ -11,13 +11,13 @@ import ( "errors" "fmt" "log/slog" - "os" "github.com/bazelbuild/buildtools/build" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/bazelfiles" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/issues" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/mirror" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/rules" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -39,7 +39,7 @@ func runFix(cmd *cobra.Command, _ []string) error { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) fileHelper, err := bazelfiles.New() diff --git a/hack/bazel-deps-mirror/upgrade.go b/hack/bazel-deps-mirror/upgrade.go index 8d6142d8b2..e2c07d5c26 100644 --- a/hack/bazel-deps-mirror/upgrade.go +++ b/hack/bazel-deps-mirror/upgrade.go @@ -11,13 +11,13 @@ import ( "errors" "fmt" "log/slog" - "os" "github.com/bazelbuild/buildtools/build" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/bazelfiles" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/issues" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/mirror" "github.com/edgelesssys/constellation/v2/hack/bazel-deps-mirror/internal/rules" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -39,7 +39,7 @@ func runUpgrade(cmd *cobra.Command, _ []string) error { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) fileHelper, err := bazelfiles.New() diff --git a/hack/cli-k8s-compatibility/main.go b/hack/cli-k8s-compatibility/main.go index 35c04a2a57..5361fcdf4f 100644 --- a/hack/cli-k8s-compatibility/main.go +++ b/hack/cli-k8s-compatibility/main.go @@ -16,6 +16,7 @@ import ( "github.com/edgelesssys/constellation/v2/internal/api/versionsapi" "github.com/edgelesssys/constellation/v2/internal/constants" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/internal/versions" ) @@ -26,7 +27,7 @@ var ( ) func main() { - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) + log := logger.NewTextLogger(slog.LevelDebug) ctx := context.Background() flag.Parse() diff --git a/hack/oci-pin/codegen.go b/hack/oci-pin/codegen.go index 77ec64f601..910056ed06 100644 --- a/hack/oci-pin/codegen.go +++ b/hack/oci-pin/codegen.go @@ -15,6 +15,7 @@ import ( "github.com/edgelesssys/constellation/v2/hack/oci-pin/internal/extract" "github.com/edgelesssys/constellation/v2/hack/oci-pin/internal/inject" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -43,7 +44,7 @@ func runCodegen(cmd *cobra.Command, _ []string) error { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) registry, prefix, name, tag, err := splitRepoTag(flags.imageRepoTag) diff --git a/hack/oci-pin/merge.go b/hack/oci-pin/merge.go index 50e674e2c9..94bafd52bf 100644 --- a/hack/oci-pin/merge.go +++ b/hack/oci-pin/merge.go @@ -12,6 +12,7 @@ import ( "os" "github.com/edgelesssys/constellation/v2/hack/oci-pin/internal/sums" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -34,7 +35,7 @@ func runMerge(cmd *cobra.Command, _ []string) error { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) log.Debug(fmt.Sprintf("Merging sum file from %q into %q.", flags.inputs, flags.output)) diff --git a/hack/oci-pin/sum.go b/hack/oci-pin/sum.go index 4499d05546..d6be5154ae 100644 --- a/hack/oci-pin/sum.go +++ b/hack/oci-pin/sum.go @@ -15,6 +15,7 @@ import ( "github.com/edgelesssys/constellation/v2/hack/oci-pin/internal/extract" "github.com/edgelesssys/constellation/v2/hack/oci-pin/internal/sums" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -40,7 +41,7 @@ func runSum(cmd *cobra.Command, _ []string) error { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) registry, prefix, name, tag, err := splitRepoTag(flags.imageRepoTag) diff --git a/hack/qemu-metadata-api/main.go b/hack/qemu-metadata-api/main.go index 7e5547543f..dd5be683c7 100644 --- a/hack/qemu-metadata-api/main.go +++ b/hack/qemu-metadata-api/main.go @@ -15,6 +15,7 @@ import ( "github.com/edgelesssys/constellation/v2/hack/qemu-metadata-api/server" "github.com/edgelesssys/constellation/v2/hack/qemu-metadata-api/virtwrapper" + "github.com/edgelesssys/constellation/v2/internal/logger" "libvirt.org/go/libvirt" ) @@ -25,7 +26,7 @@ func main() { initSecretHash := flag.String("initsecrethash", "", "brcypt hash of the init secret") flag.Parse() - log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo})) + log := logger.NewJSONLogger(slog.LevelInfo) conn, err := libvirt.NewConnect(*libvirtURI) if err != nil { diff --git a/image/upload/internal/cmd/info.go b/image/upload/internal/cmd/info.go index 8bf5093d7c..cd629600ef 100644 --- a/image/upload/internal/cmd/info.go +++ b/image/upload/internal/cmd/info.go @@ -9,11 +9,11 @@ package cmd import ( "encoding/json" "fmt" - "log/slog" "os" "github.com/edgelesssys/constellation/v2/internal/api/versionsapi" "github.com/edgelesssys/constellation/v2/internal/constants" + "github.com/edgelesssys/constellation/v2/internal/logger" infoupload "github.com/edgelesssys/constellation/v2/internal/osimage/imageinfo" "github.com/spf13/cobra" ) @@ -49,7 +49,7 @@ func runInfo(cmd *cobra.Command, args []string) error { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) info, err := readInfoArgs(args) if err != nil { diff --git a/image/upload/internal/cmd/measurementsenvelope.go b/image/upload/internal/cmd/measurementsenvelope.go index 5661359986..878dcaf718 100644 --- a/image/upload/internal/cmd/measurementsenvelope.go +++ b/image/upload/internal/cmd/measurementsenvelope.go @@ -9,10 +9,10 @@ package cmd import ( "encoding/json" "fmt" - "log/slog" "os" "github.com/edgelesssys/constellation/v2/internal/attestation/measurements" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -53,7 +53,7 @@ func runEnvelopeMeasurements(cmd *cobra.Command, _ []string) error { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) f, err := os.Open(flags.in) diff --git a/image/upload/internal/cmd/measurementsmerge.go b/image/upload/internal/cmd/measurementsmerge.go index 65d032d6fd..53ec2de2ca 100644 --- a/image/upload/internal/cmd/measurementsmerge.go +++ b/image/upload/internal/cmd/measurementsmerge.go @@ -9,10 +9,10 @@ package cmd import ( "encoding/json" "fmt" - "log/slog" "os" "github.com/edgelesssys/constellation/v2/internal/attestation/measurements" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -44,7 +44,7 @@ func runMergeMeasurements(cmd *cobra.Command, args []string) error { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) mergedMeasurements, err := readMeasurementsArgs(args) diff --git a/image/upload/internal/cmd/measurementsupload.go b/image/upload/internal/cmd/measurementsupload.go index 07ab35cd9c..850883c630 100644 --- a/image/upload/internal/cmd/measurementsupload.go +++ b/image/upload/internal/cmd/measurementsupload.go @@ -8,10 +8,10 @@ package cmd import ( "fmt" - "log/slog" "os" "github.com/edgelesssys/constellation/v2/internal/constants" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/internal/osimage/measurementsuploader" "github.com/spf13/cobra" ) @@ -52,7 +52,7 @@ func runMeasurementsUpload(cmd *cobra.Command, _ []string) error { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) uploadC, uploadCClose, err := measurementsuploader.New(cmd.Context(), flags.region, flags.bucket, flags.distributionID, log) diff --git a/image/upload/internal/cmd/uplosi.go b/image/upload/internal/cmd/uplosi.go index 6ff7238ffe..6569074768 100644 --- a/image/upload/internal/cmd/uplosi.go +++ b/image/upload/internal/cmd/uplosi.go @@ -9,12 +9,12 @@ package cmd import ( "fmt" "io" - "log/slog" "os" "strconv" "time" "github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/internal/osimage" "github.com/edgelesssys/constellation/v2/internal/osimage/archive" nopupload "github.com/edgelesssys/constellation/v2/internal/osimage/nop" @@ -58,7 +58,7 @@ func runUplosi(cmd *cobra.Command, _ []string) error { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) archiveC, archiveCClose, err := archive.New(cmd.Context(), flags.region, flags.bucket, flags.distributionID, log) diff --git a/internal/api/attestationconfigapi/cli/delete.go b/internal/api/attestationconfigapi/cli/delete.go index e8ca11f3a2..d0b0f447f6 100644 --- a/internal/api/attestationconfigapi/cli/delete.go +++ b/internal/api/attestationconfigapi/cli/delete.go @@ -9,12 +9,12 @@ import ( "errors" "fmt" "log/slog" - "os" "path" "github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi" "github.com/edgelesssys/constellation/v2/internal/attestation/variant" "github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/internal/staticupload" "github.com/spf13/cobra" ) @@ -46,7 +46,7 @@ func newDeleteCmd() *cobra.Command { } func runDelete(cmd *cobra.Command, args []string) (retErr error) { - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})).WithGroup("attestationconfigapi") + log := logger.NewTextLogger(slog.LevelDebug).WithGroup("attestationconfigapi") deleteCfg, err := newDeleteConfig(cmd, ([3]string)(args[:3])) if err != nil { @@ -89,7 +89,7 @@ func runRecursiveDelete(cmd *cobra.Command, args []string) (retErr error) { return fmt.Errorf("creating delete config: %w", err) } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})).WithGroup("attestationconfigapi") + log := logger.NewTextLogger(slog.LevelDebug).WithGroup("attestationconfigapi") client, closeFn, err := staticupload.New(cmd.Context(), staticupload.Config{ Bucket: deleteCfg.bucket, Region: deleteCfg.region, diff --git a/internal/api/attestationconfigapi/cli/upload.go b/internal/api/attestationconfigapi/cli/upload.go index be2934f240..54036009ab 100644 --- a/internal/api/attestationconfigapi/cli/upload.go +++ b/internal/api/attestationconfigapi/cli/upload.go @@ -17,6 +17,7 @@ import ( "github.com/edgelesssys/constellation/v2/internal/attestation/variant" "github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider" "github.com/edgelesssys/constellation/v2/internal/file" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/internal/staticupload" "github.com/edgelesssys/constellation/v2/internal/verify" "github.com/spf13/afero" @@ -60,7 +61,7 @@ func envCheck(_ *cobra.Command, _ []string) error { func runUpload(cmd *cobra.Command, args []string) (retErr error) { ctx := cmd.Context() - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})).WithGroup("attestationconfigapi") + log := logger.NewTextLogger(slog.LevelDebug).WithGroup("attestationconfigapi") uploadCfg, err := newConfig(cmd, ([3]string)(args[:3])) if err != nil { diff --git a/internal/api/versionsapi/cli/add.go b/internal/api/versionsapi/cli/add.go index dd35a5662d..89c64c2b79 100644 --- a/internal/api/versionsapi/cli/add.go +++ b/internal/api/versionsapi/cli/add.go @@ -11,10 +11,10 @@ import ( "errors" "fmt" "log/slog" - "os" apiclient "github.com/edgelesssys/constellation/v2/internal/api/client" "github.com/edgelesssys/constellation/v2/internal/api/versionsapi" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" "golang.org/x/mod/semver" ) @@ -52,7 +52,7 @@ func runAdd(cmd *cobra.Command, _ []string) (retErr error) { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) log.Debug("Validating flags") diff --git a/internal/api/versionsapi/cli/latest.go b/internal/api/versionsapi/cli/latest.go index 00dffddfa1..797cfc64d5 100644 --- a/internal/api/versionsapi/cli/latest.go +++ b/internal/api/versionsapi/cli/latest.go @@ -11,9 +11,9 @@ import ( "errors" "fmt" "log/slog" - "os" "github.com/edgelesssys/constellation/v2/internal/api/versionsapi" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/spf13/cobra" ) @@ -38,7 +38,7 @@ func runLatest(cmd *cobra.Command, _ []string) (retErr error) { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) log.Debug("Validating flags") diff --git a/internal/api/versionsapi/cli/list.go b/internal/api/versionsapi/cli/list.go index 62cb876f3e..717ba6c77e 100644 --- a/internal/api/versionsapi/cli/list.go +++ b/internal/api/versionsapi/cli/list.go @@ -12,13 +12,13 @@ import ( "errors" "fmt" "log/slog" - "os" "github.com/spf13/cobra" "golang.org/x/mod/semver" apiclient "github.com/edgelesssys/constellation/v2/internal/api/client" "github.com/edgelesssys/constellation/v2/internal/api/versionsapi" + "github.com/edgelesssys/constellation/v2/internal/logger" ) func newListCmd() *cobra.Command { @@ -43,7 +43,7 @@ func runList(cmd *cobra.Command, _ []string) (retErr error) { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) log.Debug("Validating flags") diff --git a/internal/api/versionsapi/cli/rm.go b/internal/api/versionsapi/cli/rm.go index a637511820..51802b5fbe 100644 --- a/internal/api/versionsapi/cli/rm.go +++ b/internal/api/versionsapi/cli/rm.go @@ -13,7 +13,6 @@ import ( "io" "log" "log/slog" - "os" "regexp" "strings" "time" @@ -28,6 +27,7 @@ import ( "github.com/aws/smithy-go" apiclient "github.com/edgelesssys/constellation/v2/internal/api/client" "github.com/edgelesssys/constellation/v2/internal/api/versionsapi" + "github.com/edgelesssys/constellation/v2/internal/logger" gaxv2 "github.com/googleapis/gax-go/v2" "github.com/spf13/cobra" ) @@ -74,7 +74,7 @@ func runRemove(cmd *cobra.Command, _ []string) (retErr error) { if err != nil { return err } - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: flags.logLevel})) + log := logger.NewTextLogger(flags.logLevel) log.Debug(fmt.Sprintf("Parsed flags: %+v", flags)) log.Debug("Validating flags") diff --git a/internal/logger/log.go b/internal/logger/log.go index ad89aced68..75b76e08f6 100644 --- a/internal/logger/log.go +++ b/internal/logger/log.go @@ -26,6 +26,7 @@ package logger import ( "context" "log/slog" + "os" "runtime" "testing" "time" @@ -92,6 +93,18 @@ func middlewareLogger(l *slog.Logger) logging.Logger { }) } +// NewTextLogger creates a new slog.Logger that writes text formatted log messages +// to os.Stderr. +func NewTextLogger(level slog.Level) *slog.Logger { + return slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{AddSource: true, Level: level})) +} + +// NewJSONLogger creates a new slog.Logger that writes JSON formatted log messages +// to os.Stderr. +func NewJSONLogger(level slog.Level) *slog.Logger { + return slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{AddSource: true, Level: level})) +} + // NewTest creates a new slog.Logger that writes to a testing.T. func NewTest(t *testing.T) *slog.Logger { return slog.New(slog.NewTextHandler(testWriter{t: t}, nil)) diff --git a/joinservice/cmd/main.go b/joinservice/cmd/main.go index c2e74f8559..8aaab96540 100644 --- a/joinservice/cmd/main.go +++ b/joinservice/cmd/main.go @@ -51,7 +51,7 @@ func main() { verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription) flag.Parse() - log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logger.VerbosityFromInt(*verbosity)})) + log := logger.NewJSONLogger(logger.VerbosityFromInt(*verbosity)) log.With( slog.String("version", constants.BinaryVersion().String()), slog.String("cloudProvider", *provider), diff --git a/keyservice/cmd/main.go b/keyservice/cmd/main.go index 40933cd560..756509a93f 100644 --- a/keyservice/cmd/main.go +++ b/keyservice/cmd/main.go @@ -34,7 +34,7 @@ func main() { verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription) flag.Parse() - log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logger.VerbosityFromInt(*verbosity)})) + log := logger.NewTextLogger(logger.VerbosityFromInt(*verbosity)) log.With(slog.String("version", constants.BinaryVersion().String())). Info("Constellation Key Management Service") diff --git a/measurement-reader/cmd/main.go b/measurement-reader/cmd/main.go index cae3a80cc4..15ce68d2ea 100644 --- a/measurement-reader/cmd/main.go +++ b/measurement-reader/cmd/main.go @@ -13,13 +13,14 @@ import ( "github.com/edgelesssys/constellation/v2/internal/attestation/variant" "github.com/edgelesssys/constellation/v2/internal/constants" + "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/measurement-reader/internal/sorted" "github.com/edgelesssys/constellation/v2/measurement-reader/internal/tdx" "github.com/edgelesssys/constellation/v2/measurement-reader/internal/tpm" ) func main() { - log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo})) + log := logger.NewJSONLogger(slog.LevelInfo) variantString := os.Getenv(constants.AttestationVariant) attestationVariant, err := variant.FromString(variantString) if err != nil { diff --git a/s3proxy/cmd/main.go b/s3proxy/cmd/main.go index 0be83b25b1..a58bdf0e49 100644 --- a/s3proxy/cmd/main.go +++ b/s3proxy/cmd/main.go @@ -16,7 +16,6 @@ import ( "log/slog" "net" "net/http" - "os" "github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/s3proxy/internal/router" @@ -48,8 +47,8 @@ func main() { // logger := slog.New(handler) // logLevel.Set(flags.logLevel) - logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logger.VerbosityFromInt(flags.logLevel)})) - + logger := logger.NewJSONLogger(logger.VerbosityFromInt(flags.logLevel)) + if flags.forwardMultipartReqs { logger.Warn("configured to forward multipart uploads, this may leak data to AWS") } diff --git a/upgrade-agent/cmd/main.go b/upgrade-agent/cmd/main.go index 732e0aa34d..2eaabb91dc 100644 --- a/upgrade-agent/cmd/main.go +++ b/upgrade-agent/cmd/main.go @@ -27,7 +27,7 @@ func main() { verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription) flag.Parse() - log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logger.VerbosityFromInt(*verbosity)})).WithGroup("bootstrapper") + log := logger.NewJSONLogger(logger.VerbosityFromInt(*verbosity)).WithGroup("bootstrapper") if *gRPCDebug { logger.ReplaceGRPCLogger(log.WithGroup("gRPC")) diff --git a/verify/cmd/main.go b/verify/cmd/main.go index d3d5e3d2c4..58310ff81c 100644 --- a/verify/cmd/main.go +++ b/verify/cmd/main.go @@ -25,7 +25,7 @@ func main() { verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription) flag.Parse() - log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logger.VerbosityFromInt(*verbosity)})) + log := logger.NewJSONLogger(logger.VerbosityFromInt(*verbosity)) log.With(slog.String("version", constants.BinaryVersion().String()), slog.String("attestationVariant", *attestationVariant)). Info("Constellation Verification Service")