i am working on a project where i utilize log4j2 logging. while developing in intellij, all works fine and the logging is done as expected. the log4j2.xml is linked through java property passed to jvm on startup via intellij settings. but once i try to run a standalone gradle built fat-jar, i'm experiencing the following problems:
我正在开发一个使用log4j2日志记录的项目。在intellij的开发过程中,所有的工作都很好,日志记录是按照预期完成的。log4j2。xml通过java属性链接,在启动时通过intellij设置传递给jvm。但是,一旦我尝试运行一个独立的gradle构建的fatjar,我就会遇到以下问题:
java -Dlog4j.debug=true -Dlog4j.configurationFile=/home/aaa/log4j2.xml -jar /home/aaa/myjar-SNAPSHOT.jar
exceptions:
例外:
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 No log4j2 configuration file found. Using default configuration: logging only errors to the console.
i don't even understand where those [thread]s come from, since i get the same error even while using a basic simplest config in my log4j2:
我甚至不知道这些[线程]来自哪里,因为即使在log4j2中使用最简单的配置时,我也会得到相同的错误:
<?xml version="1.0" encoding="UTF-8" ?><Configuration status="WARN" monitorInterval="86400">
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout
pattern="%-5p %d{yyyy-MM-dd HH:mm:ss.SSS} ${hostName} %c{1} %msg %throwable{7}%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
any thoughts are welcome. thanks.
任何想法是受欢迎的。谢谢。
4 个解决方案
#1
3
in fatJar, dependencies can provide a log4j-provider.properties in the META-INF that cause this issue,
在fatJar中,依赖项可以提供log4j提供程序。导致这个问题的META-INF的属性,
remove it in the gradle task :
把它移到gradle任务中:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'project',
'Implementation-Version': project.version,
'Main-Class': 'com.sample.CLI'
}
baseName = project.name + '-all'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it).matching {
exclude 'META-INF/**.RSA'
exclude 'META-INF/MANIFEST.MF'
exclude 'META-INF/log4j-provider.properties'
} } }
with jar
}
#2
1
The problem is described here: https://issues.apache.org/jira/browse/LOG4J2-673
问题描述如下:https://issues es.apache.org/jirae./browse/log4j2-673
Unfortunately at the moment there only seems to be a solution for the maven-shade-plugin: https://github.com/edwgiz/maven-shaded-log4j-transformer
不幸的是,目前似乎只有maven-shade-plugin的解决方案:https://github.com/edwgiz/maven-shad-log4j transformer
<plugins>
<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>
<finalName>${project.artifactId}${appSuffix}</finalName>
<transformers>
...
<transformer
implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
</transformer>
</transformers>
...
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.edwgiz</groupId>
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
#3
1
The LoggerContextFactory
binds the Log4j API to its implementation. The Log4j LogManager locates a LoggerContextFactory by locating all instances of META-INF/log4j-provider.properties
, a standard java.util.Properties
file, and then inspecting each to verify that it specifies a value for the Log4jAPIVersion property that conforms to the version required by the LogManager.
LoggerContextFactory将Log4j API绑定到其实现。Log4j管理器通过定位META-INF/ Log4j提供程序的所有实例来定位LoggerContextFactory。属性,一个标准的java.util。属性文件,然后检查每个属性,以验证它是否为Log4jAPIVersion属性指定了一个值,该值符合LogManager所需的版本。
Incase of fat jar, you can also explicitly specify log4j2 to use LoggerContextFactory
in your application by:
在fat jar的Incase中,您还可以通过以下方式显式地指定log4j2在应用程序中使用LoggerContextFactory:
System.setProperty("log4j2.loggerContextFactory", "org.apache.logging.log4j.core.impl.Log4jContextFactory")
or as specified in the log4j-provider.properties
file included in your log4j-core
jar.
或如log4j-provider中指定的那样。包含在log4j-core jar中的属性文件。
#4
0
When you run your application from an IDE, jar runs itself without embedding the dependencies and you don't have conflict of log settings. But when you convert the application into a fat jar then all the dependencies will be injected into your project's jar file and your log4j settings that come from external jar files (dependencies) may be conflicted while fatJar process merge them into a single artifact.
当您从一个IDE运行您的应用程序时,jar在不嵌入依赖项的情况下运行自己,并且您没有日志设置的冲突。但是,当您将应用程序转换为一个fat jar时,所有依赖项都将被注入到项目的jar文件中,来自外部jar文件(依赖项)的log4j设置可能会发生冲突,而fatJar进程将它们合并到单个工件中。
In this case i think your "Log4j2Plugins.dat" files may be conflicted. To be sure, you can open your fatJar file with a zip editor (ex: 7Zip), navigate to path in fatJar as below and delete one of the conflicted files (you can choose smallest one by size) from your fatJar. Run the fatJar and check the logging is working properly.
在这种情况下,我认为您的“Log4j2Plugins”。文件可能会冲突。当然,您可以使用zip编辑器打开fatJar文件(例如:7Zip),导航到fatJar中的路径,并从fatJar中删除一个冲突的文件(您可以按大小选择最小的文件)。运行fatJar并检查日志记录是否正常工作。
\META-INF\org\apache\logging\log4j\core\config\plugins\Log4j2Plugins.dat
\ meta - inf \ org \ apache \日志\ log4j \ \ config \ plugins \ Log4j2Plugins.dat核心
Now we can check the dependencies (artifacts) and find which of them contain the "Log4j2Plugins.dat" files. So you can exclude the modules that have the file from your build tool and then your fatJar creation process will exclude the conflicted files and your new fatJar can start logging as expected.
现在,我们可以检查依赖项(构件),并找到其中包含“Log4j2Plugins”的部分。dat文件。因此,您可以从构建工具中排除具有该文件的模块,然后您的fatJar创建过程将排除冲突的文件,您的新fatJar可以按预期开始日志记录。
In my case, my fatJar module imports some other modules from Spring Boot and when i exclude the conflicted logging libraries, my fatJar starts logging without any error.
在我的例子中,我的fatJar模块从Spring Boot中导入了一些其他模块,当我排除冲突的日志库时,我的fatJar开始无错误地进行日志记录。
configurations { all*.exclude module: 'spring-boot' all*.exclude module: 'spring-boot-starter-logging' all*.exclude module: 'logback-classic' all*.exclude module: 'commons-logging' }
配置{ *。排除模块:“spring-boot”*。排除模块:“spring-boot-starter-logging”*。排除模块:“logback-classic”*。排除模块:“通用日志”}
#1
3
in fatJar, dependencies can provide a log4j-provider.properties in the META-INF that cause this issue,
在fatJar中,依赖项可以提供log4j提供程序。导致这个问题的META-INF的属性,
remove it in the gradle task :
把它移到gradle任务中:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'project',
'Implementation-Version': project.version,
'Main-Class': 'com.sample.CLI'
}
baseName = project.name + '-all'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it).matching {
exclude 'META-INF/**.RSA'
exclude 'META-INF/MANIFEST.MF'
exclude 'META-INF/log4j-provider.properties'
} } }
with jar
}
#2
1
The problem is described here: https://issues.apache.org/jira/browse/LOG4J2-673
问题描述如下:https://issues es.apache.org/jirae./browse/log4j2-673
Unfortunately at the moment there only seems to be a solution for the maven-shade-plugin: https://github.com/edwgiz/maven-shaded-log4j-transformer
不幸的是,目前似乎只有maven-shade-plugin的解决方案:https://github.com/edwgiz/maven-shad-log4j transformer
<plugins>
<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>
<finalName>${project.artifactId}${appSuffix}</finalName>
<transformers>
...
<transformer
implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
</transformer>
</transformers>
...
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.edwgiz</groupId>
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
#3
1
The LoggerContextFactory
binds the Log4j API to its implementation. The Log4j LogManager locates a LoggerContextFactory by locating all instances of META-INF/log4j-provider.properties
, a standard java.util.Properties
file, and then inspecting each to verify that it specifies a value for the Log4jAPIVersion property that conforms to the version required by the LogManager.
LoggerContextFactory将Log4j API绑定到其实现。Log4j管理器通过定位META-INF/ Log4j提供程序的所有实例来定位LoggerContextFactory。属性,一个标准的java.util。属性文件,然后检查每个属性,以验证它是否为Log4jAPIVersion属性指定了一个值,该值符合LogManager所需的版本。
Incase of fat jar, you can also explicitly specify log4j2 to use LoggerContextFactory
in your application by:
在fat jar的Incase中,您还可以通过以下方式显式地指定log4j2在应用程序中使用LoggerContextFactory:
System.setProperty("log4j2.loggerContextFactory", "org.apache.logging.log4j.core.impl.Log4jContextFactory")
or as specified in the log4j-provider.properties
file included in your log4j-core
jar.
或如log4j-provider中指定的那样。包含在log4j-core jar中的属性文件。
#4
0
When you run your application from an IDE, jar runs itself without embedding the dependencies and you don't have conflict of log settings. But when you convert the application into a fat jar then all the dependencies will be injected into your project's jar file and your log4j settings that come from external jar files (dependencies) may be conflicted while fatJar process merge them into a single artifact.
当您从一个IDE运行您的应用程序时,jar在不嵌入依赖项的情况下运行自己,并且您没有日志设置的冲突。但是,当您将应用程序转换为一个fat jar时,所有依赖项都将被注入到项目的jar文件中,来自外部jar文件(依赖项)的log4j设置可能会发生冲突,而fatJar进程将它们合并到单个工件中。
In this case i think your "Log4j2Plugins.dat" files may be conflicted. To be sure, you can open your fatJar file with a zip editor (ex: 7Zip), navigate to path in fatJar as below and delete one of the conflicted files (you can choose smallest one by size) from your fatJar. Run the fatJar and check the logging is working properly.
在这种情况下,我认为您的“Log4j2Plugins”。文件可能会冲突。当然,您可以使用zip编辑器打开fatJar文件(例如:7Zip),导航到fatJar中的路径,并从fatJar中删除一个冲突的文件(您可以按大小选择最小的文件)。运行fatJar并检查日志记录是否正常工作。
\META-INF\org\apache\logging\log4j\core\config\plugins\Log4j2Plugins.dat
\ meta - inf \ org \ apache \日志\ log4j \ \ config \ plugins \ Log4j2Plugins.dat核心
Now we can check the dependencies (artifacts) and find which of them contain the "Log4j2Plugins.dat" files. So you can exclude the modules that have the file from your build tool and then your fatJar creation process will exclude the conflicted files and your new fatJar can start logging as expected.
现在,我们可以检查依赖项(构件),并找到其中包含“Log4j2Plugins”的部分。dat文件。因此,您可以从构建工具中排除具有该文件的模块,然后您的fatJar创建过程将排除冲突的文件,您的新fatJar可以按预期开始日志记录。
In my case, my fatJar module imports some other modules from Spring Boot and when i exclude the conflicted logging libraries, my fatJar starts logging without any error.
在我的例子中,我的fatJar模块从Spring Boot中导入了一些其他模块,当我排除冲突的日志库时,我的fatJar开始无错误地进行日志记录。
configurations { all*.exclude module: 'spring-boot' all*.exclude module: 'spring-boot-starter-logging' all*.exclude module: 'logback-classic' all*.exclude module: 'commons-logging' }
配置{ *。排除模块:“spring-boot”*。排除模块:“spring-boot-starter-logging”*。排除模块:“logback-classic”*。排除模块:“通用日志”}