Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hapag-lloyd/log4j2-filtered-stacktrace-plugin

Plugin for Log4j2 to reduce noise in stacktraces.
https://github.com/hapag-lloyd/log4j2-filtered-stacktrace-plugin

filter java log4j2 logger logging plugin stacktrace

Last synced: about 7 hours ago
JSON representation

Plugin for Log4j2 to reduce noise in stacktraces.

Awesome Lists containing this project

README

        

[![Actions](https://github.com/Hapag-Lloyd/log4j2-filtered-stacktrace-plugin/workflows/Release/badge.svg)](https://github.com/Hapag-Lloyd/log4j2-filtered-stacktrace-plugin/actions)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.hlag.logging/log4j2-stacktrace-filter/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.hlag.logging/log4j2-stacktrace-filter-plugin)

# Filter Stacktrace Plugin for Log4j2

This project contains a stacktrace filter for the Log4j2 logging system. It can be included in a [`JsonTemplateLayout`](https://logging.apache.org/log4j/2.x/manual/json-template-layout.html).

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](https://stackoverflow.com/questions/70614495/is-there-a-way-to-override-the-exceptionresolver-of-jsontemplatelayout-of-log4j2/77143208#77143208).
We replaced the main logic completely.

# Output

Adding the filter to the logging configuration with ``
produces the following Json object in the log stream:

```json
"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.

# Usage

## Maven Dependency

Include the following artifact to your project which contains the `log4j2.xml` configuration file. Make sure to use the newest version.

```xml

com.hlag.logging
log4j2-stacktrace-filter

1.3.0

```

## Log4j2 Configuration

```xml











```

### additionalPackagesToIgnore

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.

### whitelistPackages

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.