Skip to content

Commit

Permalink
Allow the log pattern be configurable via the Infinispan CR
Browse files Browse the repository at this point in the history
Closes #2162
  • Loading branch information
karesti authored Oct 2, 2024
1 parent 692368c commit 20d600c
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 3 deletions.
3 changes: 3 additions & 0 deletions api/v1/infinispan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ const (
)

type InfinispanLoggingSpec struct {
// A custom pattern to be applied to the Log4j STDOUT output
// +optional
Pattern string `json:"pattern,omitempty"`
Categories map[string]LoggingLevelType `json:"categories,omitempty"`
}

Expand Down
10 changes: 10 additions & 0 deletions api/v1/types_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const (
SiteServiceFQNTemplate = "%s.%s.svc.cluster.local"

GossipRouterDeploymentNameTemplate = "%s-router"

DefaultLoggingPattern = "%d{HH:mm:ss,SSS} %-5p (%t) [%c] %m%throwable%n"
)

type ExternalDependencyType string
Expand Down Expand Up @@ -531,6 +533,14 @@ func (ispn *Infinispan) GetLogCategoriesForConfig() map[string]string {
return copied
}

// GetLogPatternForConfig returns the user configured log pattern or the DefaultLoggingPattern.
func (ispn *Infinispan) GetLogPatternForConfig() string {
if ispn.Spec.Logging != nil && ispn.Spec.Logging.Pattern != "" {
return ispn.Spec.Logging.Pattern
}
return DefaultLoggingPattern
}

// IsWellFormed return true if cluster is well formed
func (ispn *Infinispan) IsWellFormed() bool {
return ispn.EnsureClusterStability() == nil
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/infinispan.org_infinispans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@ spec:
- error
type: string
type: object
pattern:
description: A custom pattern to be applied to the Log4j STDOUT
output
type: string
type: object
replicas:
description: The number of nodes in the Infinispan cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ include::{topics}/con_fips_mode_cr.adoc[leveloffset=+1]
endif::downstream[]

//Logging
include::{topics}/proc_configuring_logging.adoc[leveloffset=+1]
include::{topics}/proc_configuring_logging_pattern.adoc[leveloffset=+1]
include::{topics}/proc_configuring_logging_categories.adoc[leveloffset=+1]
include::{topics}/ref_logging.adoc[leveloffset=+2]

//Community only
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[id='configuring-logging_{context}']
= Adjusting log pattern

[role="_abstract"]
To customize the log display for {brandname} log traces, update the log pattern.
If no custom pattern is set, the default format is:
`%d{HH:mm:ss,SSS} %-5p (%t) [%c] %m%throwable%n`

.Procedure

. Configure {brandname} logging with the `spec.logging.pattern` field in your `Infinispan` CR.
+
[source,options="nowrap",subs=attributes+]
----
include::yaml/logging_pattern.yaml[]
----
+
. Apply the changes.
. Retrieve logs from {brandname} pods as required.
+
[source,options="nowrap",subs=attributes+]
----
{oc_logs} -f $POD_NAME
----
3 changes: 3 additions & 0 deletions documentation/asciidoc/topics/yaml/logging_pattern.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spec:
logging:
pattern: %X{address} %X{user} [%d{dd/MMM/yyyy:HH:mm:ss Z}]
1 change: 1 addition & 0 deletions pkg/infinispan/configuration/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

type Spec struct {
Pattern string
Categories map[string]string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func InfinispanServer(i *ispnv1.Infinispan, ctx pipeline.Context) {
func Logging(i *ispnv1.Infinispan, ctx pipeline.Context) {
loggingSpec := &logging.Spec{
Categories: i.GetLogCategoriesForConfig(),
Pattern: i.GetLogPatternForConfig(),
}
log4jXml, err := logging.Generate(ctx.Operand(), loggingSpec)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/templates/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Appenders>
<!-- Colored output on the console -->
<Console name="STDOUT">
<PatternLayout pattern="%d{HH:mm:ss,SSS} %-5p (%t) [%c] %m%throwable%n"/>
<PatternLayout pattern="{{ .Pattern }}"/>
</Console>
</Appenders>

Expand Down
23 changes: 22 additions & 1 deletion test/e2e/infinispan/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func TestBaseFunctionality(t *testing.T) {
defer testKube.CleanNamespaceAndLogOnPanic(t, tutils.Namespace)

// Infinispan cluster defintion with extra labels to be propagated to the service and pods
// Infinispan cluster definition with extra labels to be propagated to the service and pods
replicas := 2
spec := tutils.DefaultSpec(t, testKube, func(i *ispnv1.Infinispan) {
// Ensure that cluster creation using the limit:request format works on initial creation
Expand Down Expand Up @@ -66,6 +66,7 @@ func TestBaseFunctionality(t *testing.T) {
verifyLabelsAndAnnotations(assert, require, ispn)
verifyDefaultAuthention(require, ispn)
verifyScheduling(assert, require, ispn)
verifyLogPattern(assert, ispn, v1.DefaultLoggingPattern)

if tutils.IsVersionAtLeast("15.0.0") {
// Verify that the Redis endpoint is accessible
Expand All @@ -76,6 +77,26 @@ func TestBaseFunctionality(t *testing.T) {
}
}

func TestCustomLoggingPattern(t *testing.T) {
defer testKube.CleanNamespaceAndLogOnPanic(t, tutils.Namespace)
spec := tutils.DefaultSpec(t, testKube, func(i *ispnv1.Infinispan) {
i.Spec.Logging.Pattern = "custom_log_pattern_for_test"
})

testKube.CreateInfinispan(spec, tutils.Namespace)
testKube.WaitForInfinispanPods(1, tutils.SinglePodTimeout, spec.Name, tutils.Namespace)
ispn := testKube.WaitForInfinispanCondition(spec.Name, spec.Namespace, ispnv1.ConditionWellFormed)

assert := assert.New(t)

verifyLogPattern(assert, ispn, "custom_log_pattern_for_test")
}

func verifyLogPattern(assert *assert.Assertions, ispn *v1.Infinispan, pattern string) {
configMap := testKube.GetConfigMap(ispn.GetConfigName(), ispn.GetNamespace())
assert.Containsf(configMap.Data["log4j.xml"], "<PatternLayout pattern=\""+pattern+"\"/>", "logging pattern not found in the config map")
}

// Make sure no PVCs were created
func verifyNoPVCs(assert *assert.Assertions, ispn *v1.Infinispan) {
pvcs := testKube.GetPVCList(ispn.Namespace, ispn.PodSelectorLabels())
Expand Down
1 change: 1 addition & 0 deletions test/e2e/infinispan/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestOpenShiftIntegration(t *testing.T) {

// Smoke tests
t.Run("TestBaseFunctionality", TestBaseFunctionality)
t.Run("TestCustomLoggingPattern", TestCustomLoggingPattern)
t.Run("TestExplicitCredentials", TestExplicitCredentials)
t.Run("TestAuthorizationWithCustomRoles", TestAuthorizationWithCustomRoles)

Expand Down

0 comments on commit 20d600c

Please sign in to comment.