Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adjusting the log level for integrations #2258

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions deploy/traits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ traits:
- name: color
type: bool
description: Colorize the log output
- name: level
type: string
description: Adjust the log level for the integrations (defaults to INFO)
- name: json
type: bool
description: Output the log in json format
Expand Down
4 changes: 4 additions & 0 deletions docs/modules/traits/pages/logging.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ The following configuration options are available:
| bool
| Colorize the log output

| logging.level
| string
| Adjust the log level for the integrations (defaults to INFO)

| logging.json
| bool
| Output the log in json format
Expand Down
6 changes: 6 additions & 0 deletions pkg/trait/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
)

const (
envVarQuarkusLogLevel = "QUARKUS_LOG_LEVEL"
envVarQuarkusLogConsoleColor = "QUARKUS_LOG_CONSOLE_COLOR"
envVarQuarkusLogConsoleJson = "QUARKUS_LOG_CONSOLE_JSON"
envVarQuarkusLogConsoleJsonPrettyPrint = "QUARKUS_LOG_CONSOLE_JSON_PRETTY_PRINT"
depQuarkusLoggingJson = "mvn:io.quarkus:quarkus-logging-json"
defaultLogLevel = "INFO"
)

// This trait is used to control logging options (such as color)
Expand All @@ -38,6 +40,8 @@ type loggingTrait struct {
BaseTrait `property:",squash"`
// Colorize the log output
Color *bool `property:"color" json:"color,omitempty"`
// Adjust the log level for the integrations (defaults to INFO)
Level string `property:"level" json:"level,omitempty"`
// Output the log in json format
Json *bool `property:"json" json:"json,omitempty"`
// Enable "pretty printing" of the json log
Expand All @@ -48,6 +52,7 @@ func newLoggingTraitTrait() Trait {
return &loggingTrait{
BaseTrait: NewBaseTrait("logging", 800),
Color: util.BoolP(true),
Level: defaultLogLevel,
Json: util.BoolP(false),
JsonPrettyPrint: util.BoolP(false),
}
Expand Down Expand Up @@ -76,6 +81,7 @@ func (l loggingTrait) Apply(environment *Environment) error {
return nil
}

envvar.SetVal(&environment.EnvVars, envVarQuarkusLogLevel, l.Level)
envvar.SetVal(&environment.EnvVars, envVarQuarkusLogConsoleJson, strconv.FormatBool(*l.Json))
envvar.SetVal(&environment.EnvVars, envVarQuarkusLogConsoleJsonPrettyPrint, strconv.FormatBool(*l.JsonPrettyPrint))

Expand Down
23 changes: 20 additions & 3 deletions pkg/trait/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"testing"
)

func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint bool) *Environment {
func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint bool, logLevel string) *Environment {
c, err := camel.DefaultCatalog()
if err != nil {
panic(err)
Expand All @@ -54,6 +54,7 @@ func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint b
"color": color,
"json": json,
"json-pretty-print": jsonPrettyPrint,
"level": logLevel,
}),
},
},
Expand All @@ -80,7 +81,7 @@ func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint b
}

func createDefaultLoggingTestEnv(t *testing.T) *Environment {
return createLoggingTestEnv(t, true, false, false)
return createLoggingTestEnv(t, true, false, false, defaultLogLevel)
}

func NewLoggingTestCatalog() *Catalog {
Expand All @@ -97,6 +98,7 @@ func TestEmptyLoggingTrait(t *testing.T) {
quarkusConsoleColor := false
jsonFormat := false
jsonPrettyPrint := false
logLevelIsInfo := false

for _, e := range env.EnvVars {
if e.Name == envVarQuarkusLogConsoleColor {
Expand All @@ -116,16 +118,23 @@ func TestEmptyLoggingTrait(t *testing.T) {
jsonPrettyPrint = true
}
}

if e.Name == envVarQuarkusLogLevel {
if e.Value == "INFO" {
logLevelIsInfo = true
}
}
}

assert.True(t, quarkusConsoleColor)
assert.True(t, logLevelIsInfo)
assert.False(t, jsonFormat)
assert.False(t, jsonPrettyPrint)
assert.NotEmpty(t, env.ExecutedTraits)
}

func TestJsonLoggingTrait(t *testing.T) {
env := createLoggingTestEnv(t, true, true, false)
env := createLoggingTestEnv(t, true, true, false, "TRACE")
err := NewLoggingTestCatalog().apply(env)

assert.Nil(t, err)
Expand All @@ -134,6 +143,7 @@ func TestJsonLoggingTrait(t *testing.T) {
quarkusConsoleColor := false
jsonFormat := true
jsonPrettyPrint := false
logLevelIsTrace := false

for _, e := range env.EnvVars {
if e.Name == envVarQuarkusLogConsoleColor {
Expand All @@ -153,10 +163,17 @@ func TestJsonLoggingTrait(t *testing.T) {
jsonPrettyPrint = true
}
}

if e.Name == envVarQuarkusLogLevel {
if e.Value == "TRACE" {
logLevelIsTrace = true
}
}
}

assert.False(t, quarkusConsoleColor)
assert.True(t, jsonFormat)
assert.False(t, jsonPrettyPrint)
assert.True(t, logLevelIsTrace)
assert.NotEmpty(t, env.ExecutedTraits)
}