From 0756e597bd807f279d078e467b6bf67ac101be82 Mon Sep 17 00:00:00 2001 From: "Jose R. Gonzalez" Date: Thu, 12 Sep 2024 13:47:39 -0500 Subject: [PATCH] ensure per-check logger carries the check name throughout execution Signed-off-by: Jose R. Gonzalez --- internal/engine/engine.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/engine/engine.go b/internal/engine/engine.go index 452e8194..244aafc5 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -217,9 +217,11 @@ func (c *craneEngine) ExecuteChecks(ctx context.Context) error { // execute checks logger.V(log.DBG).Info("executing checks") for _, executedCheck := range c.checks { + logger := logger.WithValues("check", executedCheck.Name()) + ctx := logr.NewContext(ctx, logger) c.results.TestedImage = c.image - logger.V(log.DBG).Info("running check", "check", executedCheck.Name()) + logger.V(log.DBG).Info("running check") if executedCheck.Metadata().Level == check.LevelOptional || executedCheck.Metadata().Level == check.LevelWarn { logger.Info(fmt.Sprintf("Check %s is not currently being enforced.", executedCheck.Name())) } @@ -230,7 +232,7 @@ func (c *craneEngine) ExecuteChecks(ctx context.Context) error { checkElapsedTime := time.Since(checkStartTime) if err != nil { - logger.WithValues("result", "ERROR", "err", err.Error()).Info("check completed", "check", executedCheck.Name()) + logger.WithValues("result", "ERROR", "err", err.Error()).Info("check completed") result := certification.Result{Check: executedCheck, ElapsedTime: checkElapsedTime} c.results.Errors = appendUnlessOptional(c.results.Errors, *result.WithError(err)) continue @@ -239,16 +241,16 @@ func (c *craneEngine) ExecuteChecks(ctx context.Context) error { if !checkPassed { // if a test doesn't pass but is of level warn include it in warning results, instead of failed results if executedCheck.Metadata().Level == check.LevelWarn { - logger.WithValues("result", "WARNING").Info("check completed", "check", executedCheck.Name()) + logger.WithValues("result", "WARNING").Info("check completed") c.results.Warned = appendUnlessOptional(c.results.Warned, certification.Result{Check: executedCheck, ElapsedTime: checkElapsedTime}) continue } - logger.WithValues("result", "FAILED").Info("check completed", "check", executedCheck.Name()) + logger.WithValues("result", "FAILED").Info("check completed") c.results.Failed = appendUnlessOptional(c.results.Failed, certification.Result{Check: executedCheck, ElapsedTime: checkElapsedTime}) continue } - logger.WithValues("result", "PASSED").Info("check completed", "check", executedCheck.Name()) + logger.WithValues("result", "PASSED").Info("check completed") c.results.Passed = appendUnlessOptional(c.results.Passed, certification.Result{Check: executedCheck, ElapsedTime: checkElapsedTime}) }