From 8e66e125a8a6f1b51476963b707a5d5379b0969a Mon Sep 17 00:00:00 2001 From: Antonin Stefanutti Date: Fri, 14 Jan 2022 15:33:03 +0100 Subject: [PATCH] chore(log): Better structured Maven logs --- pkg/util/maven/maven_command.go | 31 +------------------------------ pkg/util/maven/maven_log.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/pkg/util/maven/maven_command.go b/pkg/util/maven/maven_command.go index 96c819b8ba..908d81944e 100644 --- a/pkg/util/maven/maven_command.go +++ b/pkg/util/maven/maven_command.go @@ -18,7 +18,6 @@ limitations under the License. package maven import ( - "bufio" "context" "fmt" "io" @@ -124,35 +123,7 @@ func (c *Command) Do(ctx context.Context) error { Log.WithValues("MAVEN_OPTS", mavenOptions).Infof("executing: %s", strings.Join(cmd.Args, " ")) - stdOut, err := cmd.StdoutPipe() - if err != nil { - return err - } - - err = cmd.Start() - - if err != nil { - return err - } - - scanner := bufio.NewScanner(stdOut) - - Log.Debug("About to start parsing the Maven output") - for scanner.Scan() { - line := scanner.Text() - mavenLog, parseError := parseLog(line) - if parseError == nil { - normalizeLog(mavenLog) - } else { - // Why we are ignoring the parsing errors here: there are a few scenarios where this would likely occur. - // For example, if something outside of Maven outputs something (i.e.: the JDK, a misbehaved plugin, - // etc). The build may still have succeeded, though. - nonNormalizedLog(line) - } - } - Log.Debug("Finished parsing Maven output") - - return cmd.Wait() + return util.RunAndLog(ctx, cmd, mavenLogHandler, mavenLogHandler) } func NewContext(buildDir string) Context { diff --git a/pkg/util/maven/maven_log.go b/pkg/util/maven/maven_log.go index c7f6b6b745..0ed0c84f98 100644 --- a/pkg/util/maven/maven_log.go +++ b/pkg/util/maven/maven_log.go @@ -47,6 +47,18 @@ const ( var mavenLogger = log.WithName("maven.build") +func mavenLogHandler(s string) { + mavenLog, parseError := parseLog(s) + if parseError == nil { + normalizeLog(mavenLog) + } else { + // Why we are ignoring the parsing errors here: there are a few scenarios where this would likely occur. + // For example, if something outside of Maven outputs something (i.e.: the JDK, a misbehaved plugin, + // etc). The build may still have succeeded, though. + nonNormalizedLog(s) + } +} + func parseLog(line string) (l mavenLog, err error) { err = json.Unmarshal([]byte(line), &l) return