How to get exception class name via pattern #2495
-
Hello, we are trying to pass the class name of an exception to a log4j2 message via layout patterns. The reason for this is that we want to replicate of the old and long deprecated log4j2-gelf library (see here). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
@der-eismann, Log4j 2 contains a GELF Layout. But all JSON-based layouts (i.e., JSON Layout and GELF Layout) are deprecated in favor of JSON Template Layout (JTL), which provides unmatched customizability and performance compared to other JSON-based layouts in both Log4j and other logging implementations. JTL already ships a GELF-compatible layout out of the box. You can use it as follows in your <JsonTemplateLayout eventTemplateUri="classpath:GelfLayout.json"/> You can extend the <JsonTemplateLayout eventTemplateUri="classpath:GelfLayout.json">
<EventTemplateAdditionalField key="exceptionClassName"
value='{"$resolver": "exception", "field": "className"}'
format="JSON"/>
</JsonTemplateLayout> |
Beta Was this translation helpful? Give feedback.
@der-eismann, Log4j 2 contains a GELF Layout. But all JSON-based layouts (i.e., JSON Layout and GELF Layout) are deprecated in favor of JSON Template Layout (JTL), which provides unmatched customizability and performance compared to other JSON-based layouts in both Log4j and other logging implementations.
JTL already ships a GELF-compatible layout out of the box. You can use it as follows in your
log4j2.xml
:You can extend the
GelfLayout.json
event template to include the exception class name as follows: