diff --git a/pkg/common/testflag.go b/pkg/common/testflag.go new file mode 100644 index 00000000000..4988563d3e8 --- /dev/null +++ b/pkg/common/testflag.go @@ -0,0 +1,20 @@ +package common + +import ( + "context" +) + +type testFlagContextKey string + +const testFlagContextKeyVal = testFlagContextKey("test-context") + +// TestContext returns whether the context has the test flag set +func TestContext(ctx context.Context) bool { + val := ctx.Value(testFlagContextKeyVal) + return val != nil +} + +// WithTextContext sets the test flag in the context +func WithTestContext(ctx context.Context) context.Context { + return context.WithValue(ctx, testFlagContextKeyVal, true) +} diff --git a/pkg/runner/logger.go b/pkg/runner/logger.go index 8464801575a..8da4bf1ba8a 100644 --- a/pkg/runner/logger.go +++ b/pkg/runner/logger.go @@ -48,6 +48,12 @@ func WithJobLogger(ctx context.Context, jobName string, secrets map[string]strin nextColor++ logger := logrus.New() + if common.TestContext(ctx) { + fieldLogger := common.Logger(ctx) + if fieldLogger != nil { + logger = fieldLogger.(*logrus.Logger) + } + } logger.SetFormatter(formatter) logger.SetOutput(os.Stdout) logger.SetLevel(logrus.GetLevel())