Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow existing logger from context #898

Merged
merged 1 commit into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pkg/common/testflag.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 6 additions & 0 deletions pkg/runner/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down