From 3fd770cedb2e076e5da84a29137be316aaa405aa Mon Sep 17 00:00:00 2001 From: Bence Csati <113284287+csatib02@users.noreply.github.com> Date: Fri, 26 Jul 2024 10:03:11 +0200 Subject: [PATCH] fix(e2e): default kics log-level to error to avoid excessive log messages (#1955) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: use log-level: error for kics e2e tests Signed-off-by: Bence Csati * fix(plugin): set default log level in python plugin sdk * fix: create new context logger Signed-off-by: Bence Csati fix: create new ctx logger Signed-off-by: Bence Csati fix: create new ctx logger Signed-off-by: Bence Csati fix: create new ctx logger Signed-off-by: Bence Csati fix: create new ctx logger Signed-off-by: Bence Csati fix: create new ctx logger Signed-off-by: Bence Csati --------- Signed-off-by: Bence Csati Co-authored-by: Zsolt Kacsándi --- core/log/context.go | 2 +- e2e/kics_test.go | 14 ++++++++++++-- e2e/suite_test.go | 4 +++- plugins/sdk-go/plugin/config.go | 7 ++++--- plugins/sdk-go/plugin/logger.go | 4 ++-- plugins/sdk-python/plugin/server/config.py | 2 +- plugins/sdk-python/plugin/server/server.py | 1 + plugins/store/kics/main.go | 4 ++++ 8 files changed, 28 insertions(+), 10 deletions(-) diff --git a/core/log/context.go b/core/log/context.go index 569109fdb..5b844f99d 100644 --- a/core/log/context.go +++ b/core/log/context.go @@ -39,7 +39,7 @@ func GetLoggerFromContextOrDiscard(ctx context.Context) *log.Entry { if !ok { logger = log.NewEntry(&log.Logger{ Out: io.Discard, - Level: 0, + Level: log.PanicLevel, }).WithContext(ctx) } return logger diff --git a/e2e/kics_test.go b/e2e/kics_test.go index c934a21a4..a2b35ef6b 100644 --- a/e2e/kics_test.go +++ b/e2e/kics_test.go @@ -22,7 +22,9 @@ import ( "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" + "github.com/sirupsen/logrus" + "github.com/openclarity/vmclarity/core/log" "github.com/openclarity/vmclarity/scanner" scannercommon "github.com/openclarity/vmclarity/scanner/common" "github.com/openclarity/vmclarity/scanner/families" @@ -51,6 +53,10 @@ var _ = ginkgo.Describe("Running KICS scan", func() { ginkgo.Skip("KICS plugin image not provided") } + log.InitLogger(logrus.ErrorLevel.String(), ginkgo.GinkgoWriter) + logger := logrus.WithContext(ctx) + specCtx := log.SetLoggerForContext(ctx, logger) + input, err := filepath.Abs("./testdata") gomega.Expect(err).NotTo(gomega.HaveOccurred()) notifier := &Notifier{} @@ -74,7 +80,7 @@ var _ = ginkgo.Describe("Running KICS scan", func() { }, }, }, - }).Run(ctx, notifier) + }).Run(specCtx, notifier) gomega.Expect(errs).To(gomega.BeEmpty()) gomega.Eventually(func() bool { @@ -106,6 +112,10 @@ var _ = ginkgo.Describe("Running a KICS scan", func() { ginkgo.Skip("KICS plugin image not provided") } + log.InitLogger(logrus.ErrorLevel.String(), ginkgo.GinkgoWriter) + logger := logrus.WithContext(ctx) + specCtx := log.SetLoggerForContext(ctx, logger) + input, err := filepath.Abs("./testdata") gomega.Expect(err).NotTo(gomega.HaveOccurred()) notifier := &Notifier{} @@ -129,7 +139,7 @@ var _ = ginkgo.Describe("Running a KICS scan", func() { }, }, }, - }).Run(ctx, notifier) + }).Run(specCtx, notifier) gomega.Expect(errs).To(gomega.BeEmpty()) gomega.Eventually(func() bool { diff --git a/e2e/suite_test.go b/e2e/suite_test.go index 7cb2dd1dc..ba0d37ba5 100644 --- a/e2e/suite_test.go +++ b/e2e/suite_test.go @@ -31,6 +31,8 @@ import ( uibackendclient "github.com/openclarity/vmclarity/uibackend/client" ) +const DefaultLogLevel = logrus.DebugLevel + var ( testEnv types.Environment cfg *config.Config @@ -52,7 +54,7 @@ func beforeSuite(ctx context.Context) { var err error ginkgo.By("initializing test environment") - log.InitLogger(logrus.DebugLevel.String(), ginkgo.GinkgoWriter) + log.InitLogger(DefaultLogLevel.String(), ginkgo.GinkgoWriter) logger := logrus.WithContext(ctx) ctx = log.SetLoggerForContext(ctx, logger) diff --git a/plugins/sdk-go/plugin/config.go b/plugins/sdk-go/plugin/config.go index 818a0f085..69f47a311 100644 --- a/plugins/sdk-go/plugin/config.go +++ b/plugins/sdk-go/plugin/config.go @@ -16,23 +16,24 @@ package plugin import ( + "log/slog" "os" ) const ( EnvLogLevel = "PLUGIN_SERVER_LOG_LEVEL" - DefaultLogLevel = "info" + DefaultLogLevel = slog.LevelError EnvListenAddress = "PLUGIN_SERVER_LISTEN_ADDRESS" DefaultListenAddress = "http://0.0.0.0:8080" ) -func getLogLevel() string { +func GetLogLevel() string { if logLevel := os.Getenv(EnvLogLevel); logLevel != "" { return logLevel } - return DefaultLogLevel + return DefaultLogLevel.String() } func getListenAddress() string { diff --git a/plugins/sdk-go/plugin/logger.go b/plugins/sdk-go/plugin/logger.go index 87c05ba0c..e1ef15e2d 100644 --- a/plugins/sdk-go/plugin/logger.go +++ b/plugins/sdk-go/plugin/logger.go @@ -24,8 +24,8 @@ var logger *slog.Logger func init() { var logLevel slog.Level - if err := logLevel.UnmarshalText([]byte(getLogLevel())); err != nil { - logLevel = slog.LevelInfo + if err := logLevel.UnmarshalText([]byte(GetLogLevel())); err != nil { + logLevel = DefaultLogLevel } logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ diff --git a/plugins/sdk-python/plugin/server/config.py b/plugins/sdk-python/plugin/server/config.py index d989ce83f..53781ef48 100644 --- a/plugins/sdk-python/plugin/server/config.py +++ b/plugins/sdk-python/plugin/server/config.py @@ -2,7 +2,7 @@ from urllib.parse import urlparse ENV_LOG_LEVEL = "PLUGIN_SERVER_LOG_LEVEL" -DEFAULT_LOG_LEVEL = "info" +DEFAULT_LOG_LEVEL = logging.ERROR ENV_LISTEN_ADDRESS = "PLUGIN_SERVER_LISTEN_ADDRESS" DEFAULT_LISTEN_ADDRESS = "http://0.0.0.0:8080" diff --git a/plugins/sdk-python/plugin/server/server.py b/plugins/sdk-python/plugin/server/server.py index 58e4e5f45..451168f7f 100644 --- a/plugins/sdk-python/plugin/server/server.py +++ b/plugins/sdk-python/plugin/server/server.py @@ -20,6 +20,7 @@ # Init logger _logger = logging.getLogger('plugin.scanner') +_logger.setLevel(_config.get_log_level()) _logger.addHandler(logging.StreamHandler(sys.stdout)) diff --git a/plugins/store/kics/main.go b/plugins/store/kics/main.go index 8a0754610..cdf04bca6 100644 --- a/plugins/store/kics/main.go +++ b/plugins/store/kics/main.go @@ -70,6 +70,10 @@ func (s *Scanner) SetStatus(newStatus *types.Status) { func (s *Scanner) Start(config types.Config) { go func() { logger := plugin.GetLogger() + err := printer.LogLevel(plugin.GetLogLevel(), false) //nolint:forbidigo + if err != nil { + logger.Error("Failed to set log level", slog.Any("error", err)) + } ctx, cancel := context.WithTimeout(context.Background(), time.Duration(config.TimeoutSeconds)*time.Second) s.cancel = cancel