Skip to content

Commit

Permalink
Added support for the json log format via the logging trait
Browse files Browse the repository at this point in the history
- includes support for pretty printing the json
  • Loading branch information
orpiske committed Apr 30, 2021
1 parent 8683c29 commit c692a51
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 7 deletions.
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:"json.pretty-print,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)
}

0 comments on commit c692a51

Please sign in to comment.