This project contains a stacktrace filter for the Log4j2 logging system. It can be included in a JsonTemplateLayout
.
The plugin reduces the stacktrace by filtering classes which are not of any interest. It has a built-in list of packages to exclude
from the stacktrace. Additional packages can be configured in the log4j2.xml
file. Find the full blown example below.
This plugin was built on top of the work of the Apache Log4j team and takanuva15, who described the plugin in a Stackoverflow article. We replaced the main logic completely.
Adding the filter to the logging configuration with <EventTemplateAdditionalField key="error" format="JSON" value='{"$resolver": "filteredStacktraceException"}'/>
produces the following Json object in the log stream:
"error": {
"stack": "java.lang.RuntimeException: java.lang.ArithmeticException: / by zero\r\n\tat com.hlag.logging.log4j2.LoggerIntegrationTest.wrapException(LoggerIntegrationTest.java:21)\r\n\tat com.hlag.logging.log4j2.LoggerIntegrationTest.logException(LoggerIntegrationTest.java:11)\r\n\t[suppressed 70 lines]\r\ncaused by java.lang.ArithmeticException\r\n\tat com.hlag.logging.log4j2.LoggerIntegrationTest.throwArithmeticException(LoggerIntegrationTest.java:25)\r\n\tat com.hlag.logging.log4j2.LoggerIntegrationTest.wrapException(LoggerIntegrationTest.java:19)\r\n\tat com.hlag.logging.log4j2.LoggerIntegrationTest.logException(LoggerIntegrationTest.java:11)\r\n\t[suppressed 70 lines]",
"name": "java.lang.RuntimeException",
"message": "java.lang.ArithmeticException: / by zero",
"totalFilteredElements": 140
}
140 elements have been removed from the stacktrace. Only those remain which are relevant for your application.
Include the following artifact to your project which contains the log4j2.xml
configuration file. Make sure to use the newest version.
<!-- pom.xml -->
<dependency>
<groupId>com.hlag.logging</groupId>
<artifactId>log4j2-stacktrace-filter</artifactId>
<!-- make sure to use the newest version -->
<version>1.3.0</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<!-- log4j2.xml -->
<Configuration status="INFO">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<JsonTemplateLayout eventTemplateUri="classpath:JsonLayout.json" maxStringLength="32768" stackTraceEnabled="false">
<EventTemplateAdditionalField key="error" format="JSON"
value='{"$resolver": "filteredStacktraceException"}, "additionalPackagesToIgnore": ["com.hlag.logging.log4j2."]}' />
</JsonTemplateLayout>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>
Use this parameter to add other packages to the built-in list. These packages are ignored too. Especially useful if you have to include company internal frameworks, but you don't want to see them in the stacktrace.
Makes no sense to specify whitelistedPackages
in addition to this parameter.
Use this parameter to define packages which should remain when filtering a stack trace. Every other package is removed from the
stacktrace. The built-in list of packages to filter as well as the additionalPackagesToIgnore
parameter have no effect when
whitelistPackages
exists.