Tag Archives: Log4j2 error

Log4j2 reports ERROR StatusLogger Unrecognized format specifier

problem

When using maven-shade-plugin or maven-assembly-plugin to mark the project into an executable JAR package, if you import log4j2, the following problems will occur:

ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.

 

Solution

Add the following configuration to pom.

< plugin > 
   < groupId > org.apache.maven.plugins </ groupId > 
   < artifactId > maven-shade-plugin </ artifactId > 
   < version > 2.4.3 </ version > 
   < executions > 
       < execution > 
           < phase > package < / phase > 
           < goals > 
               < goal > shade </ goal > 
           </ goals > 
           <configuration > 
               < filters > 
                   < filter > 
                       < artifact > *:* </ artifact > 
                       < excludes > 
                           < exclude > META-INF/*.SF </ exclude > 
                           < exclude > META-INF/*.DSA </ exclude > 
                           < exclude > META-INF/*.RSA </ exclude > 
                       </ excludes > 
                   </ filter > 
               </ filters > 
               <finalName> ${artifactId}-${env}-${version} </ finalName > 
               < transformers > 
                   < transformer
                            implementation ="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > 
                       < mainClass > xxx.yyyy.zzz .Main </ mainClass > 
                   </ transformer > 
                   < transformer implementation ="org.apache.maven.plugins.shade.resource.AppendingTransformer" > 
                       < resource > META-INF/spring.handlers </ resource >
                   </transformer > 
                   < transformer implementation ="org.apache.maven.plugins.shade.resource.AppendingTransformer" > 
                       < resource > META-INF/spring.schemas </ resource > 
                   </ transformer > 
                   < transformer implementation ="org.apache.maven .plugins.shade.resource.AppendingTransformer" > 
                       < resource > META-INF/spring.tooling </ resource > 
                   </ transformer > 
                   < transformer
                            implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer" /> 
                   < transformer
                            implementation ="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> 
                   < transformer implementation ="com.github.edwgiz. mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer"  /> 
               </ transformers > 
           </ configuration > 
       </ execution > 
   </ executions > 
   < dependencies > 
       < dependency > 
           <groupId >com.github.edwgiz </ groupId > 
           < artifactId > maven-shade-plugin.log4j2-cachefile-transformer </ artifactId > 
           < version > 2.6.1 </ version > 
       </ dependency > 
   </ dependencies > 
</ plugin >

 

Cause Analysis

log4j2 is plug-in programming. When the log4j2 package is compiled, or the package containing the log4j2 plug-in is compiled, the plug-in information that needs to be loaded will be placed in META-INF/org/apache/logging/log4j/core/config/plugins/ Log4j2Plugins.dat (including the official logj42 native plug-in), and then when the project starts, log4j2 will scan the plug-in information file in the META-INF directory of each jar package, and then load the plug-in.

But when the project is marked as a jar package, if there are Log4j2Plugins.dat files in two different jar packages, there will be a problem, one of the files will be overwritten by the other, resulting in a file when the project starts The plug-in cannot be loaded normally, resulting in an error.

To solve this problem, when all jar packages are labeled as one jar package, the Log4j2Plugins.dat in each jar package needs to be merged. This is what the maven-shade-plugin.log4j2-cachefile-transformer package does.