Skip to content

Commit

Permalink
chore(log): Better structured Maven logs
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Jan 17, 2022
1 parent 28c505c commit 8e66e12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
31 changes: 1 addition & 30 deletions pkg/util/maven/maven_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
package maven

import (
"bufio"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/maven/maven_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e66e12

Please sign in to comment.