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

Added support for the json log format via the logging trait #2252

Merged
merged 1 commit into from Apr 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions deploy/traits.yaml
Expand Up @@ -576,6 +576,12 @@ traits:
- name: color
type: bool
description: Colorize the log output
- name: json
type: bool
description: Output the log in json format
- name: json-pretty-print
type: bool
description: Enable "pretty printing" of the json log
- name: master
platform: false
profiles:
Expand Down
8 changes: 8 additions & 0 deletions docs/modules/traits/pages/logging.adoc
Expand Up @@ -29,6 +29,14 @@ The following configuration options are available:
| bool
| Colorize the log output

| logging.json
| bool
| Output the log in json format

| logging.json-pretty-print
| bool
| Enable "pretty printing" of the json log

|===

// End of autogenerated code - DO NOT EDIT! (configuration)
38 changes: 33 additions & 5 deletions pkg/trait/logging.go
Expand Up @@ -25,7 +25,10 @@ import (
)

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

// This trait is used to control logging options (such as color)
Expand All @@ -35,12 +38,18 @@ type loggingTrait struct {
BaseTrait `property:",squash"`
// Colorize the log output
Color *bool `property:"color" json:"color,omitempty"`
// Output the log in json format
Json *bool `property:"json" json:"json,omitempty"`
// Enable "pretty printing" of the json log
JsonPrettyPrint *bool `property:"json-pretty-print" json:"jsonPrettyPrint,omitempty"`
}

func newLoggingTraitTrait() Trait {
return &loggingTrait{
BaseTrait: NewBaseTrait("logging", 800),
Color: util.BoolP(true),
BaseTrait: NewBaseTrait("logging", 800),
Color: util.BoolP(true),
Json: util.BoolP(false),
JsonPrettyPrint: util.BoolP(false),
}
}

Expand All @@ -49,11 +58,30 @@ func (l loggingTrait) Configure(environment *Environment) (bool, error) {
return false, nil
}

return environment.IntegrationInPhase(v1.IntegrationPhaseDeploying, v1.IntegrationPhaseRunning), nil
return environment.IntegrationInPhase(v1.IntegrationPhaseInitialization, v1.IntegrationPhaseDeploying,
v1.IntegrationPhaseRunning), nil
}

func (l loggingTrait) Apply(environment *Environment) error {
envvar.SetVal(&environment.EnvVars, envVarQuarkusLogConsoleColor, strconv.FormatBool(*l.Color))

if environment.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
if *l.Json {
if environment.Integration.Status.Dependencies == nil {
environment.Integration.Status.Dependencies = make([]string, 0)
}

util.StringSliceUniqueAdd(&environment.Integration.Status.Dependencies, depQuarkusLoggingJson)
}

return nil
}

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

if !*l.Json {
envvar.SetVal(&environment.EnvVars, envVarQuarkusLogConsoleColor, strconv.FormatBool(*l.Color))
}

return nil
}
70 changes: 68 additions & 2 deletions pkg/trait/logging_test.go
Expand Up @@ -22,13 +22,14 @@ import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/test"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

func createLoggingTestEnv(t *testing.T) *Environment {
func createLoggingTestEnv(t *testing.T, color bool, json bool, jsonPrettyPrint bool) *Environment {
c, err := camel.DefaultCatalog()
if err != nil {
panic(err)
Expand All @@ -48,6 +49,13 @@ func createLoggingTestEnv(t *testing.T) *Environment {
},
Spec: v1.IntegrationSpec{
Profile: v1.TraitProfileOpenShift,
Traits: map[string]v1.TraitSpec{
"logging": test.TraitSpecFromMap(t, map[string]interface{}{
"color": color,
"json": json,
"json-pretty-print": jsonPrettyPrint,
}),
},
},
},
IntegrationKit: &v1.IntegrationKit{
Expand All @@ -71,26 +79,84 @@ func createLoggingTestEnv(t *testing.T) *Environment {
return res
}

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

func NewLoggingTestCatalog() *Catalog {
return NewCatalog(context.TODO(), nil)
}

func TestEmptyLoggingTrait(t *testing.T) {
env := createLoggingTestEnv(t)
env := createDefaultLoggingTestEnv(t)
err := NewLoggingTestCatalog().apply(env)

assert.Nil(t, err)
assert.NotEmpty(t, env.ExecutedTraits)

quarkusConsoleColor := false
jsonFormat := false
jsonPrettyPrint := false

for _, e := range env.EnvVars {
if e.Name == envVarQuarkusLogConsoleColor {
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
}
}
}

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

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

assert.Nil(t, err)
assert.NotEmpty(t, env.ExecutedTraits)

quarkusConsoleColor := false
jsonFormat := true
jsonPrettyPrint := false

for _, e := range env.EnvVars {
if e.Name == envVarQuarkusLogConsoleColor {
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
}
}
}

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