Class CaseConverterResolver
- All Implemented Interfaces:
EventResolver,TemplateResolver<LogEvent>
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 by
JsonTemplateLayoutDefaults.getLocale(), which is configured by
log4j.layout.jsonTemplate.locale system property and by default set
to the default system locale.
errorHandlingStrategy determines the behavior when either the
input doesn't resolve to a string value or case conversion throws an
exception:
failpropagates the failurepasscauses the resolved value to be passed as isreplacesuppresses the failure and replaces it with thereplacement, which is set tonullby default
errorHandlingStrategy is set to replace 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
replacements or pass in errorHandlingStrategy might
result in type incompatibility issues at the storage level.
Unless the input value is passed intact or replaced,
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 resolved USER environment variable to lower-case using
nl_NL locale:
{
"$resolver": "caseConverter",
"case": "lower",
"locale": "nl_NL",
"input": "${env:USER}"
}
Convert the resolved sessionId thread context data (MDC) to
lower-case:
{
"$resolver": "caseConverter",
"case": "lower",
"input": {
"$resolver": "mdc",
"key": "sessionId"
}
}
Above, if sessionId MDC resolves to a, say, number, case conversion
will fail. Since errorHandlingStrategy is set to replace and
replacement is set to null by default, the resolved value
will be null. One can suppress this behavior and let the resolved
sessionId 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"
}
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CaseConverterResolver.ErrorHandlingStrategyprivate final TemplateResolver<LogEvent> private final TemplateResolver<LogEvent> -
Constructor Summary
ConstructorsConstructorDescriptionCaseConverterResolver(EventResolverContext context, TemplateResolverConfig config) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidconvertCase(LogEvent logEvent, JsonWriter jsonWriter, int startIndex) private voidconvertCase(LogEvent logEvent, JsonWriter jsonWriter, int startIndex, String json) private static TemplateResolver<LogEvent> createDelegate(EventResolverContext context, TemplateResolverConfig config) private static TemplateResolver<LogEvent> createReplacement(EventResolverContext context, TemplateResolverConfig config) (package private) static StringgetName()booleanIndicates if the resolution should be appended to the parent JSON object.booleanIndicates if the resolver if applicable at all.booleanisResolvable(LogEvent logEvent) Indicates if the resolver if applicable for the givenvalue.private static CaseConverterResolver.ErrorHandlingStrategyvoidresolve(LogEvent logEvent, JsonWriter jsonWriter) Resolves the givenvalueusing the providedJsonWriter.voidresolve(LogEvent logEvent, JsonWriter jsonWriter, boolean succeedingEntry) Resolves the givenvalueusing the providedJsonWriter.
-
Field Details
-
inputResolver
-
converter
-
errorHandlingStrategy
-
replacementResolver
-
-
Constructor Details
-
CaseConverterResolver
CaseConverterResolver(EventResolverContext context, TemplateResolverConfig config)
-
-
Method Details
-
createDelegate
private static TemplateResolver<LogEvent> createDelegate(EventResolverContext context, TemplateResolverConfig config) -
createConverter
-
readErrorHandlingStrategy
private static CaseConverterResolver.ErrorHandlingStrategy readErrorHandlingStrategy(TemplateResolverConfig config) -
createReplacement
private static TemplateResolver<LogEvent> createReplacement(EventResolverContext context, TemplateResolverConfig config) -
getName
-
isFlattening
public boolean isFlattening()Description copied from interface:TemplateResolverIndicates 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:
isFlatteningin interfaceTemplateResolver<LogEvent>
-
isResolvable
public boolean isResolvable()Description copied from interface:TemplateResolverIndicates 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:
isResolvablein interfaceTemplateResolver<LogEvent>
-
isResolvable
Description copied from interface:TemplateResolverIndicates 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:
isResolvablein interfaceTemplateResolver<LogEvent>
-
resolve
Description copied from interface:TemplateResolverResolves the givenvalueusing the providedJsonWriter.- Specified by:
resolvein interfaceTemplateResolver<LogEvent>
-
resolve
Description copied from interface:TemplateResolverResolves the givenvalueusing the providedJsonWriter.- Specified by:
resolvein interfaceTemplateResolver<LogEvent>- Parameters:
succeedingEntry- false, if this is the first element in a collection; true, otherwise
-
convertCase
-
convertCase
-