Class CaseConverterResolver
- java.lang.Object
-
- org.apache.logging.log4j.layout.template.json.resolver.CaseConverterResolver
-
- All Implemented Interfaces:
EventResolver
,TemplateResolver<LogEvent>
public final class CaseConverterResolver extends Object implements EventResolver
Converts the case of string values.Configuration
config = case , input , [ locale ] , [ errorHandlingStrategy ] input = JSON case = "case" -> ( "upper" | "lower" ) locale = "locale" -> ( language | ( language , "_" , country ) | ( language , "_" , country , "_" , variant ) ) errorHandlingStrategy = "errorHandlingStrategy" -> ( "fail" | "pass" | "replace" ) replacement = "replacement" -> JSON
input
can be any available template value; e.g., a JSON literal, a lookup string, an object pointing to another resolver.Unless provided,
locale
points to the one returned byJsonTemplateLayoutDefaults.getLocale()
, which is configured bylog4j.layout.jsonTemplate.locale
system property and by default set to the default system locale.errorHandlingStrategy
determines the behavior when either theinput
doesn't resolve to a string value or case conversion throws an exception:fail
propagates the failurepass
causes the resolved value to be passed as isreplace
suppresses the failure and replaces it with thereplacement
, which is set tonull
by default
errorHandlingStrategy
is set toreplace
by default.Most of the time JSON logs are persisted to a storage solution (e.g., Elasticsearch) that keeps a statically-typed index on fields. Hence, if a field is always expected to be of type string, using non-string
replacement
s orpass
inerrorHandlingStrategy
might result in type incompatibility issues at the storage level.Unless the
input
value ispass
ed intact orreplace
d, case conversion is not garbage-free.Examples
Convert the resolved log level strings to upper-case:{ "$resolver": "caseConverter", "case": "upper", "input": { "$resolver": "level", "field": "name" } }
Convert the resolvedUSER
environment variable to lower-case usingnl_NL
locale:{ "$resolver": "caseConverter", "case": "lower", "locale": "nl_NL", "input": "${env:USER}" }
Convert the resolvedsessionId
thread context data (MDC) to lower-case:{ "$resolver": "caseConverter", "case": "lower", "input": { "$resolver": "mdc", "key": "sessionId" } }
Above, ifsessionId
MDC resolves to a, say, number, case conversion will fail. SinceerrorHandlingStrategy
is set toreplace
andreplacement
is set tonull
by default, the resolved value will benull
. One can suppress this behavior and let the resolvedsessionId
number be left as is:{ "$resolver": "caseConverter", "case": "lower", "input": { "$resolver": "mdc", "key": "sessionId" }, "errorHandlingStrategy": "pass" }
or replace it with a custom string:{ "$resolver": "caseConverter", "case": "lower", "input": { "$resolver": "mdc", "key": "sessionId" }, "errorHandlingStrategy": "replace" "replacement": "unknown" }
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isFlattening()
Indicates if the resolution should be appended to the parent JSON object.boolean
isResolvable()
Indicates if the resolver if applicable at all.boolean
isResolvable(LogEvent logEvent)
Indicates if the resolver if applicable for the givenvalue
.void
resolve(LogEvent logEvent, JsonWriter jsonWriter)
Resolves the givenvalue
using the providedJsonWriter
.void
resolve(LogEvent logEvent, JsonWriter jsonWriter, boolean succeedingEntry)
Resolves the givenvalue
using the providedJsonWriter
.
-
-
-
Method Detail
-
isFlattening
public boolean isFlattening()
Description copied from interface:TemplateResolver
Indicates if the resolution should be appended to the parent JSON object.For instance,
ThreadContextDataResolver
, i.e., MDC resolver, uses this flag to indicate whether the contents should be appended to the parent JSON object or not.- Specified by:
isFlattening
in interfaceTemplateResolver<LogEvent>
-
isResolvable
public boolean isResolvable()
Description copied from interface:TemplateResolver
Indicates if the resolver if applicable at all.For instance, the source line resolver can be short-circuited using this check if the location information is disabled in the layout configuration.
- Specified by:
isResolvable
in interfaceTemplateResolver<LogEvent>
-
isResolvable
public boolean isResolvable(LogEvent logEvent)
Description copied from interface:TemplateResolver
Indicates if the resolver if applicable for the givenvalue
.For instance, the stack trace resolver can be short-circuited using this check if the stack traces are disabled in the layout configuration.
- Specified by:
isResolvable
in interfaceTemplateResolver<LogEvent>
-
resolve
public void resolve(LogEvent logEvent, JsonWriter jsonWriter)
Description copied from interface:TemplateResolver
Resolves the givenvalue
using the providedJsonWriter
.- Specified by:
resolve
in interfaceTemplateResolver<LogEvent>
-
resolve
public void resolve(LogEvent logEvent, JsonWriter jsonWriter, boolean succeedingEntry)
Description copied from interface:TemplateResolver
Resolves the givenvalue
using the providedJsonWriter
.- Specified by:
resolve
in interfaceTemplateResolver<LogEvent>
succeedingEntry
- false, if this is the first element in a collection; true, otherwise
-
-