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.
Read More:
- [Solved] Spark Error: ERROR StatusLogger No log4j2 configuration file found
- log4j Error: Please initialize the log4j system properly [How to Solve]
- [Solved] Maven log jar package conflict error: Class path contains multiple SLF4J bindings
- log4j:WARN No appenders could be found for logger (freemarker.cache)
- [Solved] Junit.test use error: log4j:WARN No appenders could be found for logger
- Log jar package conflict error: Class path contains multiple SLF4J bindings
- [Solved] logback log format adjustment error: Failed to create converter for [%M] keyword
- [Solved] Arm Server kibana7.4.1 Error: Sending Logstash logs to /home/logstash-6.8.4/logs which is now configured via log4j2.properties
- How to Solve Excel Error Log 1: Data format error
- TypeError: unsupported format string passed to numpy.ndarray.__format__
- [Solved] SyntaxError: Invalid regular expression: invalid group specifier name
- IDEA reports an error. Error XXX reports an error. The class cannot be found
- [Solved] Neo4j error: Import-Module & neo4j Neo.ClientError.Security.Unauthorized
- [neo4j] error report when visiting neo4j in spring boot project
- [Solved] hello.s:15 Error: junk at end of line, first unrecognized character valued 0x8
- [Solved] pyinstaller: error: unrecognized arguments: sklearn
- [Solved] There are test failures. Please refer to D:\Java_study\springboot\springboot-sugon-3\target\surefire-reports for the individual test results.
- GCC error: unrecognized command line option ‘-no-pie’
- [Solved] Hive 2.3.9 Error: Error: Unrecognized column type: UNIONTYPE (state=,code=0)
- Maven compiles Scala and reports an error stackoverflowerror