Class ReadOnlyStringMapResolver
- All Implemented Interfaces:
EventResolver
,TemplateResolver<LogEvent>
- Direct Known Subclasses:
MapResolver
,ThreadContextDataResolver
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" -> stringNote 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static enum
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionReadOnlyStringMapResolver
(EventResolverContext context, TemplateResolverConfig config, Function<LogEvent, ReadOnlyStringMap> mapAccessor) -
Method Summary
Modifier and TypeMethodDescriptionprivate static EventResolver
createKeyResolver
(String key, boolean stringified, Function<LogEvent, ReadOnlyStringMap> mapAccessor) private static EventResolver
createResolver
(boolean flatten, Recycler<ReadOnlyStringMapResolver.LoopContext> loopContextRecycler, Function<LogEvent, ReadOnlyStringMap> mapAccessor) private static EventResolver
createResolver
(EventResolverContext context, TemplateResolverConfig config, Function<LogEvent, ReadOnlyStringMap> mapAccessor) private static EventResolver
createResolver
(RecyclerFactory recyclerFactory, boolean flatten, String prefix, String pattern, String replacement, boolean stringified, Function<LogEvent, ReadOnlyStringMap> mapAccessor) boolean
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 Details
-
internalResolver
-
-
Constructor Details
-
ReadOnlyStringMapResolver
ReadOnlyStringMapResolver(EventResolverContext context, TemplateResolverConfig config, Function<LogEvent, ReadOnlyStringMap> mapAccessor)
-
-
Method Details
-
createResolver
private static EventResolver createResolver(EventResolverContext context, TemplateResolverConfig config, Function<LogEvent, ReadOnlyStringMap> mapAccessor) -
createKeyResolver
private static EventResolver createKeyResolver(String key, boolean stringified, Function<LogEvent, ReadOnlyStringMap> mapAccessor) -
createResolver
private static EventResolver createResolver(RecyclerFactory recyclerFactory, boolean flatten, String prefix, String pattern, String replacement, boolean stringified, Function<LogEvent, ReadOnlyStringMap> mapAccessor) -
createResolver
private static EventResolver createResolver(boolean flatten, Recycler<ReadOnlyStringMapResolver.LoopContext> loopContextRecycler, 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
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
Description copied from interface:TemplateResolver
Resolves the givenvalue
using the providedJsonWriter
.- Specified by:
resolve
in interfaceTemplateResolver<LogEvent>
-
resolve
Description copied from interface:TemplateResolver
Resolves the givenvalue
using the providedJsonWriter
.- Specified by:
resolve
in interfaceTemplateResolver<LogEvent>
- Parameters:
succeedingEntry
- false, if this is the first element in a collection; true, otherwise
-