Class MessageResolver
java.lang.Object
org.apache.logging.log4j.layout.template.json.resolver.MessageResolver
- All Implemented Interfaces:
EventResolver
,TemplateResolver<LogEvent>
Message
resolver.
Configuration
config = [ stringified ] , [ fallbackKey ] stringified = "stringified" -> boolean fallbackKey = "fallbackKey" -> string
Examples
Resolve the message into a string:{ "$resolver": "message", "stringified": true }Resolve the message such that if it is a
ObjectMessage
or MultiformatMessage
with JSON support, its emitted JSON type (string, list,
object, etc.) will be retained:
{ "$resolver": "message" }Given the above configuration, a
SimpleMessage
will generate a
"sample log message", whereas a MapMessage
will generate a
{"action": "login", "sessionId": "87asd97a"}. Certain indexed log
storage systems (e.g., Elasticsearch) will not
allow both values to coexist due to type mismatch: one is a string
while the other is an object. Here one can use a
fallbackKey to work around the problem:
{ "$resolver": "message", "fallbackKey": "formattedMessage" }Using this configuration, a
SimpleMessage
will generate a
{"formattedMessage": "sample log message"} and a MapMessage
will generate a {"action": "login", "sessionId": "87asd97a"}. Note
that both emitted JSONs are of type object and have no
type-conflicting fields.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static EventResolver
private static EventResolver
createObjectResolver
(String fallbackKey) private static EventResolver
createStringResolver
(String fallbackKey) (package private) static String
getName()
void
resolve
(LogEvent logEvent, JsonWriter jsonWriter) Resolves the givenvalue
using the providedJsonWriter
.private static void
resolveString
(String fallbackKey, LogEvent logEvent, JsonWriter jsonWriter) private static void
resolveString
(String fallbackKey, Message message, JsonWriter jsonWriter) private static boolean
writeMultiformatMessage
(JsonWriter jsonWriter, Message message) private static boolean
writeObjectMessage
(JsonWriter jsonWriter, Message message) 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
isFlattening, isResolvable, isResolvable, resolve
-
Field Details
-
FORMATS
-
internalResolver
-
-
Constructor Details
-
MessageResolver
MessageResolver(TemplateResolverConfig config)
-
-
Method Details
-
getName
-
createInternalResolver
-
resolve
Description copied from interface:TemplateResolver
Resolves the givenvalue
using the providedJsonWriter
.- Specified by:
resolve
in interfaceTemplateResolver<LogEvent>
-
createStringResolver
-
resolveString
-
resolveString
-
createObjectResolver
-
writeMultiformatMessage
-
writeObjectMessage
-