Skip to content

Commit

Permalink
Normalizes Maven build logs
Browse files Browse the repository at this point in the history
Normalizes the Maven build log format so that the build messages go
through the operator log. This makes the logs more easily parseable
by log collection tools due to the format of the previous Maven build
log being different.

This solves GH issue #2268
  • Loading branch information
orpiske committed May 5, 2021
1 parent bd0a199 commit bc62501
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 7 deletions.
38 changes: 31 additions & 7 deletions pkg/util/maven/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package maven

import (
"bufio"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -117,12 +118,6 @@ func Run(ctx Context) error {

cmd := exec.CommandContext(c, mvnCmd, args...)
cmd.Dir = ctx.Path
cmd.Stderr = os.Stderr
if ctx.Stdout != nil {
cmd.Stdout = ctx.Stdout
} else {
cmd.Stdout = os.Stdout
}

var mavenOptions string
if len(ctx.ExtraMavenOpts) > 0 {
Expand Down Expand Up @@ -168,7 +163,36 @@ func Run(ctx Context) error {
Log.WithValues("timeout", timeout.String(), "MAVEN_OPTS", mavenOptions).
Infof("executing: %s", strings.Join(cmd.Args, " "))

return cmd.Run()
stdOut, error := cmd.StdoutPipe()
if error != nil {
return nil
}

error = cmd.Start()

if error != nil {
return error
}

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 {
// Do not abort the build because parsing failed ... the build may have succeeded
Log.Error(parseError, "Unable to parse maven log")
} else {
NormalizeLog(mavenLog)
}
}
Log.Debug("Finished parsing Maven output")

return cmd.Wait()
}

// ParseGAV decode a maven artifact id to a dependency definition.
Expand Down
69 changes: 69 additions & 0 deletions pkg/util/maven/maven_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package maven

import (
"encoding/json"
"github.com/apache/camel-k/pkg/util/log"
)

type MavenLog struct {
Level string `json:"level"`
Ts string `json:"ts"`
Logger string `json:"logger"`
Msg string `json:"msg"`
Class string `json:"class"`
CallerMethodName string `json:"caller_method_name"`
CallerFileName string `json:"caller_file_name"`
CallerLineNumber int `json:"caller_line_number"`
Thread string `json:"thread"`
}

const (
TRACE = "TRACE"
DEBUG = "DEBUG"
INFO = "INFO"
WARN = "WARN"
ERROR = "ERROR"
FATAL = "FATAL"
)

func ParseLog(line string) (mavenLog MavenLog, error error) {
error = json.Unmarshal([]byte(line), &mavenLog)

return mavenLog, error
}

func NormalizeLog(mavenLog MavenLog) {
logger := log.WithName("maven.build")

switch mavenLog.Level {
case DEBUG, TRACE:
{
logger.Debug(mavenLog.Msg)
}
case INFO, WARN:
{
logger.Info(mavenLog.Msg)
}
case ERROR, FATAL:
{
logger.Errorf(nil, mavenLog.Msg)
}
}
}

0 comments on commit bc62501

Please sign in to comment.