maven項目中因為引入的有log4j2 在打成jar包 通過java -cp 命令運行時,引起下面這段錯誤,后果就是log日志無法打印。
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. ERROR StatusLogger Reconfiguration failed: No configuration found for '5c647e05' at 'null' in 'null'
先分析原因:
log4j2 是采用的插件式編程,當log4j2包編譯時,或者含有log4j2插件的包編譯時,會將需要加載的插件信息放在META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat這個文件中(包括官方logj42的原生插件),然后項目啟動的時候,log4j2會在各個jar包的META-INF目錄下掃描這個插件信息文件,然后去加載插件。但是當項目被打成一個jar包時,如果兩個不同的jar包中都有Log4j2Plugins.dat 這個文件,就會出現問題,其中一個文件會被另一個覆蓋,導致項目啟動的時候有一個文件中的插件不能被正常加載,導致報錯。
所以解決辦法有兩種,第一種是在項目編譯打包時排除這個文件,第二種是合並這些文件。
我在嘗試第二種時該問題還是沒能解決,但是用第一種就解決了。
下面分別說下兩種解決辦法:
一:
在打包插件中maven-shade-plugin 排除相關這個Log4j2Plugins.dat即可
**/Log4j2Plugins.dat
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> <!-- 排除log4j2插件文件 解決在打成jar包運行時日志無法打印的問題 https://stackoverflow.com/questions/34945438/log4j2-configuration-not-found-when-running-standalone-application-built-by-shad--> <exclude>**/Log4j2Plugins.dat</exclude> </excludes> </filter> </filters> <artifactSet> <includes> <include>*:*</include> </includes> </artifactSet> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin>
二:添加個transformer合並Log4j2Plugins.dat文件即可,需要一個第三方的轉換器maven-shade-plugin.log4j2-cachefile-transformer 。本人在嘗試該方法時還是未能解決以上問題,估計是哪里沒配置好,該方法不行的話,建議使用第一種,簡單方便。
<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.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>