Skip to content

Commit

Permalink
feat: add pre/post stage identifier fields to json log output
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Wolf <markus.wolf@new-work.se>
  • Loading branch information
2 people authored and github-actions committed May 20, 2022
1 parent fab351a commit d33ca1d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
15 changes: 8 additions & 7 deletions pkg/runner/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
return common.NewErrorExecutor(err)
}

preSteps = append(preSteps, useStepLogger(rc, stepModel, step.pre()))
preSteps = append(preSteps, useStepLogger(rc, stepModel, stepStagePre, step.pre()))

stepExec := step.main()
steps = append(steps, useStepLogger(rc, stepModel, func(ctx context.Context) error {
steps = append(steps, useStepLogger(rc, stepModel, stepStageMain, func(ctx context.Context) error {
logger := common.Logger(ctx)
err := stepExec(ctx)
if err != nil {
Expand All @@ -72,11 +72,12 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
return nil
}))

// run the post exector in reverse order
postExec := useStepLogger(rc, stepModel, stepStagePost, step.post())
if postExecutor != nil {
postExecutor = useStepLogger(rc, stepModel, step.post()).Finally(postExecutor)
// run the post exector in reverse order
postExecutor = postExec.Finally(postExecutor)
} else {
postExecutor = useStepLogger(rc, stepModel, step.post())
postExecutor = postExec
}
}

Expand Down Expand Up @@ -108,9 +109,9 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
Finally(info.closeContainer())
}

func useStepLogger(rc *RunContext, stepModel *model.Step, executor common.Executor) common.Executor {
func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor {
return func(ctx context.Context) error {
ctx = withStepLogger(ctx, stepModel.ID, stepModel.String())
ctx = withStepLogger(ctx, stepModel.ID, stepModel.String(), stage.String())

rawLogger := common.Logger(ctx).WithField("raw_output", true)
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {
Expand Down
3 changes: 2 additions & 1 deletion pkg/runner/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ func WithCompositeLogger(ctx context.Context, masks *[]string) context.Context {
return common.WithLogger(ctx, common.Logger(ctx).WithFields(logrus.Fields{}).WithContext(ctx))
}

func withStepLogger(ctx context.Context, stepID string, stepName string) context.Context {
func withStepLogger(ctx context.Context, stepID string, stepName string, stageName string) context.Context {
rtn := common.Logger(ctx).WithFields(logrus.Fields{
"step": stepName,
"stepID": stepID,
"stage": stageName,
})
return common.WithLogger(ctx, rtn)
}
Expand Down

0 comments on commit d33ca1d

Please sign in to comment.