You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2021/05/06 08:05:23 UTC

[camel-k] branch main updated: Normalizes Maven build logs

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new 06b464c  Normalizes Maven build logs
06b464c is described below

commit 06b464c6e9d1e5979654811e3ec621178ea4e6aa
Author: Otavio Rodolfo Piske <op...@redhat.com>
AuthorDate: Tue May 4 17:44:35 2021 +0200

    Normalizes Maven build logs
    
    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
---
 pkg/util/maven/maven.go     | 38 ++++++++++++++++++++++-----
 pkg/util/maven/maven_log.go | 63 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+), 7 deletions(-)

diff --git a/pkg/util/maven/maven.go b/pkg/util/maven/maven.go
index 081e35e..3979799 100644
--- a/pkg/util/maven/maven.go
+++ b/pkg/util/maven/maven.go
@@ -18,6 +18,7 @@ limitations under the License.
 package maven
 
 import (
+	"bufio"
 	"context"
 	"fmt"
 	"io"
@@ -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 {
@@ -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.
diff --git a/pkg/util/maven/maven_log.go b/pkg/util/maven/maven_log.go
new file mode 100644
index 0000000..57647e4
--- /dev/null
+++ b/pkg/util/maven/maven_log.go
@@ -0,0 +1,63 @@
+/*
+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)
+	}
+}