Class ReadOnlyStringMapResolver
- java.lang.Object
-
- org.apache.logging.log4j.layout.template.json.resolver.ReadOnlyStringMapResolver
-
- All Implemented Interfaces:
EventResolver
,TemplateResolver<LogEvent>
- Direct Known Subclasses:
MapResolver
,ThreadContextDataResolver
class ReadOnlyStringMapResolver extends java.lang.Object implements EventResolver
ReadOnlyStringMap
resolver.Configuration
config = singleAccess | multiAccess singleAccess = key , [ stringified ] key = "key" -> string stringified = "stringified" -> boolean multiAccess = [ pattern ] , [ replacement ] , [ flatten ] , [ stringified ] pattern = "pattern" -> string replacement = "replacement" -> string flatten = "flatten" -> ( boolean | flattenConfig ) flattenConfig = [ flattenPrefix ] flattenPrefix = "prefix" -> string
Note that singleAccess resolves a single field, whilst multiAccess resolves a multitude of fields. If flatten is provided, multiAccess merges the fields with the parent, otherwise creates a new JSON object containing the values.Enabling stringified flag converts each value to its string representation.
Regex provided in the pattern is used to match against the keys. If provided, replacement will be used to replace the matched keys. These two are effectively equivalent to Pattern.compile(pattern).matcher(key).matches() and Pattern.compile(pattern).matcher(key).replaceAll(replacement) calls.
Garbage Footprint
stringified allocates a new String for values that are not of type String.pattern and replacement incur pattern matcher allocation costs.
Writing certain non-primitive values (e.g., BigDecimal, Set, etc.) to JSON generates garbage, though most (e.g., int, long, String, List, boolean[], etc.) don't.
Examples
"$resolver" is left out in the following examples, since it is to be defined by the actual resolver, e.g.,MapResolver
,ThreadContextDataResolver
.Resolve the value of the field keyed with user:role:
{ "$resolver": "…", "key": "user:role" }
Resolve the string representation of the user:rank field value:{ "$resolver": "…", "key": "user:rank", "stringified": true }
Resolve all fields into an object:{ "$resolver": "…" }
Resolve all fields into an object such that values are converted to string:{ "$resolver": "…", "stringified": true }
Resolve all fields whose keys match with the user:(role|rank) regex into an object:{ "$resolver": "…", "pattern": "user:(role|rank)" }
Resolve all fields whose keys match with the user:(role|rank) regex into an object after removing the user: prefix in the key:{ "$resolver": "…", "pattern": "user:(role|rank)", "replacement": "$1" }
Merge all fields whose keys are matching with the user:(role|rank) regex into the parent:{ "$resolver": "…", "flatten": true, "pattern": "user:(role|rank)" }
After converting the corresponding field values to string, merge all fields to parent such that keys are prefixed with _:{ "$resolver": "…", "stringified": true, "flatten": { "prefix": "_" } }
- See Also:
MapResolver
,ThreadContextDataResolver
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ReadOnlyStringMapResolver.LoopContext
private static class
ReadOnlyStringMapResolver.LoopMethod
-
Field Summary
Fields Modifier and Type Field Description private EventResolver
internalResolver
-
Constructor Summary
Constructors Constructor Description ReadOnlyStringMapResolver(EventResolverContext context, TemplateResolverConfig config, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static EventResolver
createKeyResolver(java.lang.String key, boolean stringified, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
private static EventResolver
createResolver(boolean flatten, Recycler<ReadOnlyStringMapResolver.LoopContext> loopContextRecycler, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
private static EventResolver
createResolver(EventResolverContext context, TemplateResolverConfig config, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
private static EventResolver
createResolver(RecyclerFactory recyclerFactory, boolean flatten, java.lang.String prefix, java.lang.String pattern, java.lang.String replacement, boolean stringified, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
boolean
isFlattening()
Indicates if the resolution should be appended to the parent JSON object.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
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.logging.log4j.layout.template.json.resolver.TemplateResolver
isResolvable
-
-
-
-
Field Detail
-
internalResolver
private final EventResolver internalResolver
-
-
Constructor Detail
-
ReadOnlyStringMapResolver
ReadOnlyStringMapResolver(EventResolverContext context, TemplateResolverConfig config, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
-
-
Method Detail
-
createResolver
private static EventResolver createResolver(EventResolverContext context, TemplateResolverConfig config, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
-
createKeyResolver
private static EventResolver createKeyResolver(java.lang.String key, boolean stringified, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
-
createResolver
private static EventResolver createResolver(RecyclerFactory recyclerFactory, boolean flatten, java.lang.String prefix, java.lang.String pattern, java.lang.String replacement, boolean stringified, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
-
createResolver
private static EventResolver createResolver(boolean flatten, Recycler<ReadOnlyStringMapResolver.LoopContext> loopContextRecycler, java.util.function.Function<LogEvent,ReadOnlyStringMap> mapAccessor)
-
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(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
-
-