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

feat: Set log level of a specific package using logging trait #4844

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
25e0808
feat: set package log level with logging trait
Oct 23, 2023
d089fea
Merge branch 'apache:main' into feat/logging_trait_pkg_log_level
realMartinez Oct 23, 2023
d462365
feat: set package log level with logging trait
Oct 23, 2023
3cd573f
doc: generated documentation
Oct 23, 2023
cdf5dcd
doc: generated documentation
Oct 23, 2023
64e59a3
chore: changelog automatic update
github-actions[bot] Oct 17, 2023
147ca6b
chore: nightly SBOM update
github-actions[bot] Oct 17, 2023
5d52be4
chore(deps): bump knative.dev/serving from 0.38.1 to 0.38.2
dependabot[bot] Oct 18, 2023
0aed1ee
fix: removed unnecessary defaul value
Oct 17, 2023
33d1900
feat(api): Kamelet as static resource
squakez Oct 6, 2023
44a7264
chore(api): leave .status.phase=Ready as default
squakez Oct 17, 2023
c90765a
chore(deps): bump knative.dev/eventing from 0.38.4 to 0.38.5
dependabot[bot] Oct 18, 2023
d5135db
chore: changelog automatic update
github-actions[bot] Oct 18, 2023
bcf1135
chore: autogenerated project resource update
github-actions[bot] Oct 18, 2023
8fbac22
chore: nightly SBOM update
github-actions[bot] Oct 18, 2023
c23f801
chore(deps): bump github.com/prometheus/common from 0.44.0 to 0.45.0
dependabot[bot] Oct 18, 2023
49619fb
chore: changelog automatic update
github-actions[bot] Oct 19, 2023
5196b75
chore: nightly SBOM update
github-actions[bot] Oct 19, 2023
662708c
feat(build): add root image info
squakez Oct 19, 2023
3350183
fix(ci): latest azure/setup-kubectl action not working
squakez Oct 20, 2023
0cc0db8
chore: improved e2e tests documentation.
valdar Oct 20, 2023
c37f660
chore: changelog automatic update
github-actions[bot] Oct 20, 2023
96852d5
chore: autogenerated project resource update
github-actions[bot] Oct 20, 2023
ab89d9b
chore: nightly SBOM update
github-actions[bot] Oct 20, 2023
b4d3890
chore: nightly SBOM update
github-actions[bot] Oct 21, 2023
f49dc0d
chore: changelog automatic update
github-actions[bot] Oct 22, 2023
6fef0f5
chore: nightly SBOM update
github-actions[bot] Oct 22, 2023
0bc1a44
feat: set package log level with logging trait
Oct 23, 2023
4e9063c
doc: generated documentation
Oct 23, 2023
cf2aa74
doc: generated documentation
Oct 23, 2023
6847f9c
Merge remote-tracking branch 'refs/remotes/origin/feat/logging_trait_…
Nov 13, 2023
75fb9a5
Merge branch 'main' into feat/logging_trait_pkg_log_level
realMartinez Nov 13, 2023
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
2 changes: 2 additions & 0 deletions pkg/apis/camel/v1/trait/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package trait
// +camel-k:trait=logging.
type LoggingTrait struct {
Trait `property:",squash" json:",inline"`
// Log Level category of the trait
Category map[string]string `property:"category" json:"category,omitempty"`
// Colorize the log output
Color *bool `property:"color" json:"color,omitempty"`
// Logs message format
Expand Down
15 changes: 14 additions & 1 deletion pkg/trait/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package trait

import (
"strings"

"k8s.io/utils/pointer"

traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
Expand All @@ -28,6 +30,7 @@ const (
envVarQuarkusConsoleColor = "QUARKUS_CONSOLE_COLOR"
envVarQuarkusLogLevel = "QUARKUS_LOG_LEVEL"
envVarQuarkusLogConsoleFormat = "QUARKUS_LOG_CONSOLE_FORMAT"
envVarQuarkusLogCategory = "QUARKUS_LOG_CATEGORY"
// nolint: gosec // no sensitive credentials
envVarQuarkusLogConsoleJSON = "QUARKUS_LOG_CONSOLE_JSON"
envVarQuarkusLogConsoleJSONPrettyPrint = "QUARKUS_LOG_CONSOLE_JSON_PRETTY_PRINT"
Expand All @@ -43,7 +46,8 @@ func newLoggingTraitTrait() Trait {
return &loggingTrait{
BaseTrait: NewBaseTrait("logging", 800),
LoggingTrait: traitv1.LoggingTrait{
Level: defaultLogLevel,
Level: defaultLogLevel,
Category: map[string]string{},
},
}
}
Expand All @@ -63,6 +67,15 @@ func (l loggingTrait) Apply(e *Environment) error {
envvar.SetVal(&e.EnvVars, envVarQuarkusLogConsoleFormat, l.Format)
}

if len(l.Category) > 0 {
for k, v := range l.Category {
envVarQuarkusPackage := strings.ReplaceAll(strings.ToUpper(k), ".", "_")
envVarQuarkusLogCategoryPackageFormat := envVarQuarkusLogCategory + "_" + envVarQuarkusPackage + "_LEVEL"
envVarQuarkusLogCatagoryValue := strings.ToUpper(v)
envvar.SetVal(&e.EnvVars, envVarQuarkusLogCategoryPackageFormat, envVarQuarkusLogCatagoryValue)
}
}

if pointer.BoolDeref(l.JSON, false) {
envvar.SetVal(&e.EnvVars, envVarQuarkusLogConsoleJSON, True)
if pointer.BoolDeref(l.JSONPrettyPrint, false) {
Expand Down
35 changes: 32 additions & 3 deletions pkg/trait/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/apache/camel-k/v2/pkg/util/test"
)

func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint bool, logLevel string, logFormat string) *Environment {
func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint bool, logLevel string, logFormat string, logCategory map[string]string) *Environment {
t.Helper()

client, _ := test.NewFakeClient()
Expand Down Expand Up @@ -65,6 +65,7 @@ func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint b
JSON: pointer.Bool(json),
JSONPrettyPrint: pointer.Bool(jsonPrettyPrint),
Level: logLevel,
Category: logCategory,
},
},
},
Expand Down Expand Up @@ -101,7 +102,7 @@ func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint b
func createDefaultLoggingTestEnv(t *testing.T) *Environment {
t.Helper()

return createLoggingTestEnv(t, true, false, false, defaultLogLevel, "")
return createLoggingTestEnv(t, true, false, false, defaultLogLevel, "", map[string]string{})
}

func NewLoggingTestCatalog() *Catalog {
Expand Down Expand Up @@ -161,7 +162,7 @@ func TestEmptyLoggingTrait(t *testing.T) {

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, false, "TRACE", "%d{HH:mm:ss} %-5p (%t) %s%e%n", map[string]string{})
err := NewLoggingTestCatalog().apply(env)

assert.Nil(t, err)
Expand Down Expand Up @@ -212,3 +213,31 @@ func TestJsonLoggingTrait(t *testing.T) {
assert.True(t, logFormatIsNotDefault)
assert.NotEmpty(t, env.ExecutedTraits)
}

func TestLoggingCategory(t *testing.T) {
squakez marked this conversation as resolved.
Show resolved Hide resolved
env := createLoggingTestEnv(t, true, true, false, "TRACE", "%d{HH:mm:ss} %-5p (%t) %s%e%n", map[string]string{})
env.Integration.Spec.Traits = v1.Traits{
Logging: &traitv1.LoggingTrait{
Category: map[string]string{"org.test": "debug"},
},
}

envVarQuarkusLogCategoryTestPackageName := "QUARKUS_LOG_CATEGORY_ORG_TEST_LEVEL"
envVarQuarkusLogCategoryTestPackageValue := "DEBUG"
quarkusOrgPackage := false

err := NewLoggingTestCatalog().apply(env)

assert.Nil(t, err)

for _, e := range env.EnvVars {
t.Log("Key:" + e.Name)
if e.Name == envVarQuarkusLogCategoryTestPackageName {
t.Log("Value: " + e.Value)
if e.Value == envVarQuarkusLogCategoryTestPackageValue {
quarkusOrgPackage = true
}
}
}
assert.True(t, quarkusOrgPackage)
squakez marked this conversation as resolved.
Show resolved Hide resolved
}
Loading