From 83b0a5b1f2cbc19338fa57659c51695744e89730 Mon Sep 17 00:00:00 2001 From: Thomas Eddy <2780096+ImTomEddy@users.noreply.github.com> Date: Wed, 19 Jul 2023 22:45:44 +0100 Subject: [PATCH] Add new CLI flag to log just the job id and not the entire job name (#1920) * Add new CLI flag to log just the job id and not the entire job name * Up the action test timeout to 20m from 15m --- .github/actions/run-tests/action.yml | 2 +- cmd/input.go | 1 + cmd/root.go | 2 ++ pkg/runner/logger.go | 34 ++++++++++++++++++++-------- pkg/runner/runner.go | 1 + 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/.github/actions/run-tests/action.yml b/.github/actions/run-tests/action.yml index 09cb3dae..f548c0a6 100644 --- a/.github/actions/run-tests/action.yml +++ b/.github/actions/run-tests/action.yml @@ -54,7 +54,7 @@ runs: } } }; - var args = ['test', '-v', '-cover', '-coverprofile=coverage.txt', '-covermode=atomic', '-timeout', '15m']; + var args = ['test', '-v', '-cover', '-coverprofile=coverage.txt', '-covermode=atomic', '-timeout', '20m']; var filter = process.env.FILTER; if(filter) { args.push('-run'); diff --git a/cmd/input.go b/cmd/input.go index 64556e1d..1ae27930 100644 --- a/cmd/input.go +++ b/cmd/input.go @@ -55,6 +55,7 @@ type Input struct { replaceGheActionTokenWithGithubCom string matrix []string actionCachePath string + logPrefixJobID bool } func (i *Input) resolve(path string) string { diff --git a/cmd/root.go b/cmd/root.go index f7b6515e..679d20eb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -75,6 +75,7 @@ func Execute(ctx context.Context, version string) { rootCmd.PersistentFlags().StringVarP(&input.workdir, "directory", "C", ".", "working directory") rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output") rootCmd.PersistentFlags().BoolVar(&input.jsonLogger, "json", false, "Output logs in json format") + rootCmd.PersistentFlags().BoolVar(&input.logPrefixJobID, "log-prefix-job-id", false, "Output the job id within non-json logs instead of the entire name") rootCmd.PersistentFlags().BoolVarP(&input.noOutput, "quiet", "q", false, "disable logging of output from steps") rootCmd.PersistentFlags().BoolVarP(&input.dryrun, "dryrun", "n", false, "dryrun mode") rootCmd.PersistentFlags().StringVarP(&input.secretfile, "secret-file", "", ".secrets", "file with list of secrets to read from (e.g. --secret-file .secrets)") @@ -584,6 +585,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str BindWorkdir: input.bindWorkdir, LogOutput: !input.noOutput, JSONLogger: input.jsonLogger, + LogPrefixJobID: input.logPrefixJobID, Env: envs, Secrets: secrets, Vars: vars, diff --git a/pkg/runner/logger.go b/pkg/runner/logger.go index 162fc57a..5a98210b 100644 --- a/pkg/runner/logger.go +++ b/pkg/runner/logger.go @@ -85,7 +85,8 @@ func WithJobLogger(ctx context.Context, jobID string, jobName string, config *Co defer mux.Unlock() nextColor++ formatter = &jobLogFormatter{ - color: colors[nextColor%len(colors)], + color: colors[nextColor%len(colors)], + logPrefixJobID: config.LogPrefixJobID, } } @@ -176,7 +177,8 @@ func (f *maskedFormatter) Format(entry *logrus.Entry) ([]byte, error) { } type jobLogFormatter struct { - color int + color int + logPrefixJobID bool } func (f *jobLogFormatter) Format(entry *logrus.Entry) ([]byte, error) { @@ -194,7 +196,14 @@ func (f *jobLogFormatter) Format(entry *logrus.Entry) ([]byte, error) { func (f *jobLogFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry) { entry.Message = strings.TrimSuffix(entry.Message, "\n") - jobName := entry.Data["job"] + + var job any + if f.logPrefixJobID { + job = entry.Data["jobID"] + } else { + job = entry.Data["job"] + } + debugFlag := "" if entry.Level == logrus.DebugLevel { debugFlag = "[DEBUG] " @@ -203,26 +212,33 @@ func (f *jobLogFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry) { if entry.Data["raw_output"] == true { fmt.Fprintf(b, "\x1b[%dm|\x1b[0m %s", f.color, entry.Message) } else if entry.Data["dryrun"] == true { - fmt.Fprintf(b, "\x1b[1m\x1b[%dm\x1b[7m*DRYRUN*\x1b[0m \x1b[%dm[%s] \x1b[0m%s%s", gray, f.color, jobName, debugFlag, entry.Message) + fmt.Fprintf(b, "\x1b[1m\x1b[%dm\x1b[7m*DRYRUN*\x1b[0m \x1b[%dm[%s] \x1b[0m%s%s", gray, f.color, job, debugFlag, entry.Message) } else { - fmt.Fprintf(b, "\x1b[%dm[%s] \x1b[0m%s%s", f.color, jobName, debugFlag, entry.Message) + fmt.Fprintf(b, "\x1b[%dm[%s] \x1b[0m%s%s", f.color, job, debugFlag, entry.Message) } } func (f *jobLogFormatter) print(b *bytes.Buffer, entry *logrus.Entry) { entry.Message = strings.TrimSuffix(entry.Message, "\n") - jobName := entry.Data["job"] + + var job any + if f.logPrefixJobID { + job = entry.Data["jobID"] + } else { + job = entry.Data["job"] + } + debugFlag := "" if entry.Level == logrus.DebugLevel { debugFlag = "[DEBUG] " } if entry.Data["raw_output"] == true { - fmt.Fprintf(b, "[%s] | %s", jobName, entry.Message) + fmt.Fprintf(b, "[%s] | %s", job, entry.Message) } else if entry.Data["dryrun"] == true { - fmt.Fprintf(b, "*DRYRUN* [%s] %s%s", jobName, debugFlag, entry.Message) + fmt.Fprintf(b, "*DRYRUN* [%s] %s%s", job, debugFlag, entry.Message) } else { - fmt.Fprintf(b, "[%s] %s%s", jobName, debugFlag, entry.Message) + fmt.Fprintf(b, "[%s] %s%s", job, debugFlag, entry.Message) } } diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index ecb70e7e..09c17319 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -32,6 +32,7 @@ type Config struct { ForceRebuild bool // force rebuilding local docker image action LogOutput bool // log the output from docker run JSONLogger bool // use json or text logger + LogPrefixJobID bool // switches from the full job name to the job id Env map[string]string // env for containers Inputs map[string]string // manually passed action inputs Secrets map[string]string // list of secrets