Skip to content

Commit 6b05957

Browse files
KnisterPetercpleeZauberNerd
authored
feat: add step name to logger field (#1027)
* feat: add step name to logger field This change does add the step name to the logger fields. This does not change the output for our users, but for the json logger, it does make each step output traceable. * fix: remove new logger Since logrus and context both are immutable for our case, we can just add a new field and store the logger in the context. Co-authored-by: Casey Lee <[email protected]> Co-authored-by: Björn Brauer <[email protected]>
1 parent aab2af0 commit 6b05957

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

pkg/runner/job_executor.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ func newJobExecutor(info jobInfo) common.Executor {
3737
}
3838
stepExec := info.newStepExecutor(step)
3939
steps = append(steps, func(ctx context.Context) error {
40-
err := stepExec(ctx)
41-
if err != nil {
42-
common.Logger(ctx).Errorf("%v", err)
43-
common.SetJobError(ctx, err)
44-
} else if ctx.Err() != nil {
45-
common.Logger(ctx).Errorf("%v", ctx.Err())
46-
common.SetJobError(ctx, ctx.Err())
47-
}
48-
return nil
40+
stepName := step.String()
41+
return (func(ctx context.Context) error {
42+
err := stepExec(ctx)
43+
if err != nil {
44+
common.Logger(ctx).Errorf("%v", err)
45+
common.SetJobError(ctx, err)
46+
} else if ctx.Err() != nil {
47+
common.Logger(ctx).Errorf("%v", ctx.Err())
48+
common.SetJobError(ctx, ctx.Err())
49+
}
50+
return nil
51+
})(withStepLogger(ctx, stepName))
4952
})
5053
}
5154

pkg/runner/logger.go

+5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ func WithJobLogger(ctx context.Context, jobName string, config *Config, masks *[
7272
return common.WithLogger(ctx, rtn)
7373
}
7474

75+
func withStepLogger(ctx context.Context, stepName string) context.Context {
76+
rtn := common.Logger(ctx).WithFields(logrus.Fields{"step": stepName})
77+
return common.WithLogger(ctx, rtn)
78+
}
79+
7580
type entryProcessor func(entry *logrus.Entry) *logrus.Entry
7681

7782
func valueMasker(insecureSecrets bool, secrets map[string]string, masks *[]string) entryProcessor {

0 commit comments

Comments
 (0)