Skip to content

Commit

Permalink
feat(traits): logging refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Mar 22, 2024
1 parent 196020d commit 3899c5b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 88 deletions.
37 changes: 36 additions & 1 deletion pkg/trait/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ func (l loggingTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
}

func (l loggingTrait) Apply(e *Environment) error {
if e.CamelCatalog.Runtime.Capabilities["logging"].RuntimeProperties != nil {
l.setCatalogConfiguration(e)
} else {
l.setEnvConfiguration(e)
}

return nil
}

// Deprecated: to be removed in future release in favor of func setCatalogConfiguration().
func (l loggingTrait) setEnvConfiguration(e *Environment) {
envvar.SetVal(&e.EnvVars, envVarQuarkusLogLevel, l.Level)

if l.Format != "" {
Expand All @@ -80,6 +91,30 @@ func (l loggingTrait) Apply(e *Environment) error {
envvar.SetVal(&e.EnvVars, envVarQuarkusConsoleColor, True)
}
}
}

return nil
func (l loggingTrait) setCatalogConfiguration(e *Environment) {
if e.ApplicationProperties == nil {
e.ApplicationProperties = make(map[string]string)
}
e.ApplicationProperties["camel.k.logging.level"] = l.Level
if l.Format != "" {
e.ApplicationProperties["camel.k.logging.format"] = l.Format
}
if pointer.BoolDeref(l.JSON, false) {
e.ApplicationProperties["camel.k.logging.json"] = True
if pointer.BoolDeref(l.JSONPrettyPrint, false) {
e.ApplicationProperties["camel.k.logging.jsonPrettyPrint"] = True
}
} else {
// If the trait is false OR unset, we default to false.
e.ApplicationProperties["camel.k.logging.json"] = False
if pointer.BoolDeref(l.Color, true) {
e.ApplicationProperties["camel.k.logging.color"] = True
}
}

for _, cp := range e.CamelCatalog.Runtime.Capabilities["logging"].RuntimeProperties {
e.ApplicationProperties[CapabilityPropertyKey(cp.Key, e.ApplicationProperties)] = cp.Value
}
}
110 changes: 23 additions & 87 deletions pkg/trait/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,101 +117,37 @@ func TestEmptyLoggingTrait(t *testing.T) {
assert.NotEmpty(t, conditions)
assert.NotEmpty(t, env.ExecutedTraits)

quarkusConsoleColor := false
jsonFormat := false
jsonPrettyPrint := false
logLevelIsInfo := false
logFormatIsNotDefault := false

for _, e := range env.EnvVars {
if e.Name == envVarQuarkusConsoleColor {
if e.Value == "true" {
quarkusConsoleColor = true
}
}

if e.Name == envVarQuarkusLogConsoleJSON {
if e.Value == "true" {
jsonFormat = true
}
}

if e.Name == envVarQuarkusLogConsoleJSONPrettyPrint {
if e.Value == "true" {
jsonPrettyPrint = true
}
}

if e.Name == envVarQuarkusLogLevel {
if e.Value == "INFO" {
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)
assert.Equal(t, "INFO", env.ApplicationProperties["camel.k.logging.level"])
assert.Equal(t, "", env.ApplicationProperties["camel.k.logging.format"])
assert.Equal(t, "false", env.ApplicationProperties["camel.k.logging.json"])
assert.Equal(t, "", env.ApplicationProperties["camel.k.logging.jsonPrettyPrint"])
assert.Equal(t, "true", env.ApplicationProperties["camel.k.logging.color"])

assert.Equal(t, "${camel.k.logging.level}", env.ApplicationProperties["quarkus.log.level"])
assert.Equal(t, "${camel.k.logging.format}", env.ApplicationProperties["quarkus.log.console.format"])
assert.Equal(t, "${camel.k.logging.json}", env.ApplicationProperties["quarkus.log.console.json"])
assert.Equal(t, "${camel.k.logging.jsonPrettyPrint}", env.ApplicationProperties["quarkus.log.console.json.pretty-print"])
assert.Equal(t, "${camel.k.logging.color}", env.ApplicationProperties["quarkus.console.color"])
}

func TestJsonLoggingTrait(t *testing.T) {
// 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")
env := createLoggingTestEnv(t, true, true, true, "TRACE", "%d{HH:mm:ss} %-5p (%t) %s%e%n")
conditions, err := NewLoggingTestCatalog().apply(env)

require.NoError(t, err)
assert.NotEmpty(t, conditions)
assert.NotEmpty(t, env.ExecutedTraits)

quarkusConsoleColor := false
jsonFormat := true
jsonPrettyPrint := false
logLevelIsTrace := false
logFormatIsNotDefault := false

for _, e := range env.EnvVars {
if e.Name == envVarQuarkusConsoleColor {
if e.Value == "true" {
quarkusConsoleColor = true
}
}

if e.Name == envVarQuarkusLogConsoleJSON {
if e.Value == "true" {
jsonFormat = true
}
}

if e.Name == envVarQuarkusLogConsoleJSONPrettyPrint {
if e.Value == "true" {
jsonPrettyPrint = true
}
}

if e.Name == envVarQuarkusLogLevel {
if e.Value == "TRACE" {
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)
assert.Equal(t, "TRACE", env.ApplicationProperties["camel.k.logging.level"])
assert.Equal(t, "%d{HH:mm:ss} %-5p (%t) %s%e%n", env.ApplicationProperties["camel.k.logging.format"])
assert.Equal(t, "true", env.ApplicationProperties["camel.k.logging.json"])
assert.Equal(t, "true", env.ApplicationProperties["camel.k.logging.jsonPrettyPrint"])
assert.Equal(t, "", env.ApplicationProperties["camel.k.logging.color"])

assert.Equal(t, "${camel.k.logging.level}", env.ApplicationProperties["quarkus.log.level"])
assert.Equal(t, "${camel.k.logging.format}", env.ApplicationProperties["quarkus.log.console.format"])
assert.Equal(t, "${camel.k.logging.json}", env.ApplicationProperties["quarkus.log.console.json"])
assert.Equal(t, "${camel.k.logging.jsonPrettyPrint}", env.ApplicationProperties["quarkus.log.console.json.pretty-print"])
assert.Equal(t, "${camel.k.logging.color}", env.ApplicationProperties["quarkus.console.color"])
}

0 comments on commit 3899c5b

Please sign in to comment.