Repository: apache/camel-k
Pull request #2267 (2021-05-05 09:16:00)

Added support for configuring the log format
<!-- Description -->


<!--
Enter your extended release note in the below block. If the PR requires
additional action from users switching to the new release, include the string
"action required". If no release note is required, write "NONE". 

You can (optionally) mark this PR with labels "kind/bug" or "kind/feature" to make sure
the text is added to the right section of the release notes. 
-->

**Release Note**
```release-note
Added support for configuring the log format
```


Patch:
diff --git a/deploy/traits.yaml b/deploy/traits.yaml
index eb0aeccf0..a62ef6699 100755
--- a/deploy/traits.yaml
+++ b/deploy/traits.yaml
@@ -567,7 +567,9 @@ traits:
   - Kubernetes
   - Knative
   - OpenShift
-  description: This trait is used to control logging options (such as color)
+  description: This trait is used to control logging options (such as color and the
+    format). The logging backend is provided by Quarkus and configuration details
+    for things like the the log format can be found on https://quarkus.io/guides/logging
   properties:
   - name: enabled
     type: bool
@@ -576,6 +578,9 @@ traits:
   - name: color
     type: bool
     description: Colorize the log output
+  - name: format
+    type: string
+    description: Log message format
   - name: level
     type: string
     description: Adjust the log level for the integrations (defaults to INFO)
diff --git a/docs/modules/traits/pages/logging.adoc b/docs/modules/traits/pages/logging.adoc
index 07426df5e..b50ecd4e4 100755
--- a/docs/modules/traits/pages/logging.adoc
+++ b/docs/modules/traits/pages/logging.adoc
@@ -1,7 +1,8 @@
 = Logging Trait
 
 // Start of autogenerated code - DO NOT EDIT! (description)
-This trait is used to control logging options (such as color)
+This trait is used to control logging options (such as color and the format). The logging backend is provided by
+Quarkus and configuration details for things like the the log format can be found on https://quarkus.io/guides/logging
 
 
 This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.
@@ -29,6 +30,10 @@ The following configuration options are available:
 | bool
 | Colorize the log output
 
+| logging.format
+| string
+| Log message format
+
 | logging.level
 | string
 | Adjust the log level for the integrations (defaults to INFO)
diff --git a/pkg/trait/logging.go b/pkg/trait/logging.go
index d89f791bc..3e3fee74c 100644
--- a/pkg/trait/logging.go
+++ b/pkg/trait/logging.go
@@ -27,19 +27,24 @@ import (
 const (
 	envVarQuarkusLogLevel                  = "QUARKUS_LOG_LEVEL"
 	envVarQuarkusLogConsoleColor           = "QUARKUS_LOG_CONSOLE_COLOR"
+	envVarQuarkusLogConsoleFormat          = "QUARKUS_LOG_CONSOLE_FORMAT"
 	envVarQuarkusLogConsoleJson            = "QUARKUS_LOG_CONSOLE_JSON"
 	envVarQuarkusLogConsoleJsonPrettyPrint = "QUARKUS_LOG_CONSOLE_JSON_PRETTY_PRINT"
 	depQuarkusLoggingJson                  = "mvn:io.quarkus:quarkus-logging-json"
+	defaultLogFormat                       = ""
 	defaultLogLevel                        = "INFO"
 )
 
-// This trait is used to control logging options (such as color)
+// This trait is used to control logging options (such as color and the format). The logging backend is provided by
+// Quarkus and configuration details for things like the the log format can be found on https://quarkus.io/guides/logging
 //
 // +camel-k:trait=logging
 type loggingTrait struct {
 	BaseTrait `property:",squash"`
 	// Colorize the log output
 	Color *bool `property:"color" json:"color,omitempty"`
+	// Log message format
+	Format string `property:"format" json:"format,omitempty"`
 	// Adjust the log level for the integrations (defaults to INFO)
 	Level string `property:"level" json:"level,omitempty"`
 	// Output the log in json format
@@ -52,6 +57,7 @@ func newLoggingTraitTrait() Trait {
 	return &loggingTrait{
 		BaseTrait:       NewBaseTrait("logging", 800),
 		Color:           util.BoolP(true),
+		Format:          defaultLogFormat,
 		Level:           defaultLogLevel,
 		Json:            util.BoolP(false),
 		JsonPrettyPrint: util.BoolP(false),
@@ -82,6 +88,11 @@ func (l loggingTrait) Apply(environment *Environment) error {
 	}
 
 	envvar.SetVal(&environment.EnvVars, envVarQuarkusLogLevel, l.Level)
+
+	if l.Format != defaultLogFormat {
+		envvar.SetVal(&environment.EnvVars, envVarQuarkusLogConsoleFormat, l.Format)
+	}
+
 	envvar.SetVal(&environment.EnvVars, envVarQuarkusLogConsoleJson, strconv.FormatBool(*l.Json))
 	envvar.SetVal(&environment.EnvVars, envVarQuarkusLogConsoleJsonPrettyPrint, strconv.FormatBool(*l.JsonPrettyPrint))
 


Test patch:
diff --git a/pkg/trait/logging_test.go b/pkg/trait/logging_test.go
index 90df329b3..e165b1e3d 100644
--- a/pkg/trait/logging_test.go
+++ b/pkg/trait/logging_test.go
@@ -29,7 +29,7 @@ import (
 	"testing"
 )
 
-func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint bool, logLevel string) *Environment {
+func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint bool, logLevel string, logFormat string) *Environment {
 	c, err := camel.DefaultCatalog()
 	if err != nil {
 		panic(err)
@@ -52,6 +52,7 @@ func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint b
 				Traits: map[string]v1.TraitSpec{
 					"logging": test.TraitSpecFromMap(t, map[string]interface{}{
 						"color":             color,
+						"format":            logFormat,
 						"json":              json,
 						"json-pretty-print": jsonPrettyPrint,
 						"level":             logLevel,
@@ -81,7 +82,7 @@ func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint b
 }
 
 func createDefaultLoggingTestEnv(t *testing.T) *Environment {
-	return createLoggingTestEnv(t, true, false, false, defaultLogLevel)
+	return createLoggingTestEnv(t, true, false, false, defaultLogLevel, defaultLogFormat)
 }
 
 func NewLoggingTestCatalog() *Catalog {
@@ -99,6 +100,7 @@ func TestEmptyLoggingTrait(t *testing.T) {
 	jsonFormat := false
 	jsonPrettyPrint := false
 	logLevelIsInfo := false
+	logFormatIsNotDefault := false
 
 	for _, e := range env.EnvVars {
 		if e.Name == envVarQuarkusLogConsoleColor {
@@ -124,17 +126,23 @@ func TestEmptyLoggingTrait(t *testing.T) {
 				logLevelIsInfo = true
 			}
 		}
+
+		if e.Name == envVarQuarkusLogConsoleFormat {
+			logFormatIsNotDefault = true
+		}
 	}
 
 	assert.True(t, quarkusConsoleColor)
 	assert.True(t, logLevelIsInfo)
 	assert.False(t, jsonFormat)
 	assert.False(t, jsonPrettyPrint)
+	assert.False(t, logFormatIsNotDefault)
 	assert.NotEmpty(t, env.ExecutedTraits)
 }
 
 func TestJsonLoggingTrait(t *testing.T) {
-	env := createLoggingTestEnv(t, true, true, false, "TRACE")
+	// When running, this log should look like "09:07:00 INFO  (main) Profile prod activated."
+	env := createLoggingTestEnv(t, true, true, false, "TRACE", "%d{HH:mm:ss} %-5p (%t) %s%e%n")
 	err := NewLoggingTestCatalog().apply(env)
 
 	assert.Nil(t, err)
@@ -144,6 +152,7 @@ func TestJsonLoggingTrait(t *testing.T) {
 	jsonFormat := true
 	jsonPrettyPrint := false
 	logLevelIsTrace := false
+	logFormatIsNotDefault := false
 
 	for _, e := range env.EnvVars {
 		if e.Name == envVarQuarkusLogConsoleColor {
@@ -169,11 +178,18 @@ func TestJsonLoggingTrait(t *testing.T) {
 				logLevelIsTrace = true
 			}
 		}
+
+		if e.Name == envVarQuarkusLogConsoleFormat {
+			if e.Value == "%d{HH:mm:ss} %-5p (%t) %s%e%n" {
+				logFormatIsNotDefault = true
+			}
+		}
 	}
 
 	assert.False(t, quarkusConsoleColor)
 	assert.True(t, jsonFormat)
 	assert.False(t, jsonPrettyPrint)
 	assert.True(t, logLevelIsTrace)
+	assert.True(t, logFormatIsNotDefault)
 	assert.NotEmpty(t, env.ExecutedTraits)
 }