java log4j2 错误 StatusLogger 无法识别的转换说明符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48033792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j2 ERROR StatusLogger Unrecognized conversion specifier
提问by corlydream
I have log4j2 in my project when I run main method in intellij Idea ,it correct to print log.
当我在 intellij Idea 中运行 main 方法时,我的项目中有 log4j2,打印日志是正确的。
when i use maven-shade-plugin package project to jar file, and run jar as standalone application it shows error:
当我使用 maven-shade-plugin 包项目到 jar 文件,并将 jar 作为独立应用程序运行时,它显示错误:
java -cp package.jar com.xxx.TestMain
java -cp package.jar com.xxx.TestMain
the console output
控制台输出
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 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.xml config . log4j2.version = 2.10.0, spring boot version is 1.5.9.RELEASE
pom.xml 配置。log4j2.version = 2.10.0,spring boot 版本为 1.5.9.RELEASE
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<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>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<sourceDirectory>src/main/java</sourceDirectory>
</build>
log4j2.xml config
log4j2.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" monitorInterval="30" shutdownHook="disable">
<properties>
<property name="LOG_HOME">/data1/logs</property>
<property name="JOB_NAME">noname</property>
</properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss} [%t] %level [%logger{36}][%file:%line] %msg%n%throwable"/>
</Console>
<RollingRandomAccessFile name="DetailFile"
fileName="${sys:LOG_HOME}/${sys:JOB_NAME}/detail.log" bufferedIO="false"
filePattern="${sys:LOG_HOME}/${sys:JOB_NAME}/detail.%d{yyyy-MM-dd}-%i.log">
<PatternLayout
pattern="%date{yyyy-MM-dd HH:mm:ss} [%t] %level [%file:%line] %msg%n%throwable"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="error"
fileName="${sys:LOG_HOME}/${sys:JOB_NAME}/error.log" bufferedIO="true"
filePattern="${sys:LOG_HOME}/${sys:JOB_NAME}/error.%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="%date{yyyy-MM-dd HH:mm:ss} [%t] %level [%logger{36}:%line] %msg%n%throwable"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="DetailFile"/>
<AppenderRef level="error" ref="error"/>
</Root>
</Loggers>
</Configuration>
回答by Shashank Kapoor
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
如果依赖项包含 log4j2 插件,则 jar 中包含 Log4j2Plugins.dat 缓存文件。当使用 Maven shade 插件将多个 jar 与 Log4j2Plugins.dat 文件合并时,只有一个会存活。如果没有插件定义,启动时会显示错误。Log4j2 问题
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:
对此的一种解决方案是从阴影 jar 中排除 Log4j2Plugins.dat 缓存文件,以便 Log4j 在启动时扫描插件。为此,maven-shade-plugin
在 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 pluginwhich is log4j version specific.
另一种解决方案是使用特定于 log4j 版本的转换插件合并缓存文件。
回答by Remko Popma
I've seen this error before when people had multiple versions of Log4j2 on the classpath.
当人们在类路径上有多个版本的 Log4j2 之前,我见过这个错误。
回答by philuX
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:
不幸的是,我无法回复 Shashank Kapoor 的回答,因为他正确地指出排除Log4j2Plugins.dat
文件可以解决问题。在使用 gradle 时,这对我有用:
shadowJar {
exclude "**/Log4j2Plugins.dat"
}
回答by Chris Xue
If you are using Gradle and ShadowJar 4+:
如果您使用的是 Gradle 和 ShadowJar 4+:
shadowJar{
transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer)
}
Gradle Kotlin DSL:
Gradle Kotlin DSL:
shadowJar {
transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer::class.java)
}