• Log4j2报错ERROR StatusLogger Unrecognized format specifier


    问题

    使用maven-shade-plugin或者maven-assembly-plugin插件把项目打成一个可执行JAR包时,如果你引入了log4j2会出现如下问题:

    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.

    解决方法

    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>

    原因分析

    log4j2 是采用的插件式编程,当log4j2包编译时,或者含有log4j2插件的包编译时,会将需要加载的插件信息放在META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat这个文件中(包括官方logj42的原生插件),然后项目启动的时候,log4j2会在各个jar包的META-INF目录下扫描这个插件信息文件,然后去加载插件。

    但是当项目被打成一个jar包时,如果两个不同的jar包中都有Log4j2Plugins.dat 这个文件,就会出现问题,其中一个文件会被另一个覆盖,导致项目启动的时候有一个文件中的插件不能被正常加载,导致报错。

    解决这个问题就是当所有jar包被打成一个jar包时,需要对各个jar包中的Log4j2Plugins.dat进行合并,这就是maven-shade-plugin.log4j2-cachefile-transformer这个包所做的事情了。

  • 相关阅读:
    uni-app上拉加载
    uni-app 下拉刷新
    uni-app 的生命周期
    怎么看这个方法谁调用了?
    强推
    idea中spring热部署工具加载过慢问题彻底解决
    不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny"),或者是通过包含 overrideMode="Deny" 或旧有的 allowOverride="false" 的位置标记明确设置的。
    Springboot多数据源JdbcTemplate使用(非XA)
    关于Mysql报错:java.sql.SQLException: The statement (1) has no open cursor
    杂感记录
  • 原文地址:https://www.cnblogs.com/yeyang/p/10485790.html
Copyright © 2020-2023  润新知