java - log4j2 ERROR StatusLogger Unrecognized conversion specifier

ID : 131379

viewed : 10

Tags : javalog4j2maven-shade-pluginjava

Top 5 Answer for java - log4j2 ERROR StatusLogger Unrecognized conversion specifier

vote vote

95

If a dependency contains log4j2 plugin, a Log4j2Plugins.dat cache file is included in the jar. When the Maven shade plugin is used to merge multiple jars with a Log4j2Plugins.dat file, only one will survive. Without the plugin definitions, errors are shown on startup. Log4j2 issue

One solution for this is excluding Log4j2Plugins.dat cache file from shaded jar so that Log4j scans for plugins on startup. For this, add a filter in maven-shade-plugin configuration in your pom:

    <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-shade-plugin</artifactId>         <version>3.1.0</version>         <configuration>             <createDependencyReducedPom>false</createDependencyReducedPom>             <filters>                 <filter>                     <artifact>*:*</artifact>                     <excludes>                         <exclude>**/Log4j2Plugins.dat</exclude>                     </excludes>                 </filter>             </filters>         </configuration>         ...     </plugin> 

Another solution is to merge the cache files using a transformation plugin which is log4j version specific.

vote vote

88

If you are using Gradle and ShadowJar 4+:

shadowJar{   transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer) } 

Gradle Kotlin DSL:

shadowJar {     transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer::class.java) } 
vote vote

70

Unfortunately, I can't reply to Shashank Kapoor answer, as he correctly pointed out excluding the Log4j2Plugins.dat file solves the issue. This did the trick for me while using gradle:

shadowJar {     exclude "**/Log4j2Plugins.dat" } 
vote vote

70

I’ve seen this error before when people had multiple versions of Log4j2 on the classpath.

vote vote

57

For those of us who are removing the older Logj42 from our classpaths to remediate the Log4Shell (CVE-2021-44228) vulnerability, you may find you have this issue when using your uber jars after you upgrade.

If you are building your uber jars from Maven or Gradle, use one of the plugins respectively:

if you are building them by some other means, see this answer: https://stackoverflow.com/a/70497498/1174024. But basically you have to properly handle Log4j2 plugin cache files as this class does:

https://github.com/edwgiz/maven-shaded-log4j-transformer/blob/master/src/main/java/io/github/edwgiz/log4j/maven/plugins/shade/transformer/Log4j2PluginCacheFileTransformer.java

to properly handle the Log4j2Plugins.dat file when building the new uber jar.

Top 3 video Explaining java - log4j2 ERROR StatusLogger Unrecognized conversion specifier

Related QUESTION?