Log4j2:如何使其相信log4j-core.jar中的类确实存在?

huangapple go评论90阅读模式
英文:

Log4j2: How to convince it that log4j-core.jar's classes are indeed present?

问题

我正在将一个Java应用程序构建为单个可执行的.jar文件,以便我可以解压缩所有应用程序所依赖的必需jar文件,并将它们的所有内容与实际应用程序的类文件一起包含在一个单独的.jar文件中(这是经典ant jar任务的一个非常方便的特性,在这种情况下,就不需要处理或分发多个jar文件,也不需要处理嵌套的.jar文件之类的情况)。

这种方法到目前为止都运行得很好,但是现在我从log4j(v1)升级到log4j2(实际上我正在使用slf4j-API,但是底层的log4j版本发生了变化)。

自从升级后,我的方法似乎不再起作用,但在运行时我不断收到一个错误消息:
ERROR StatusLogger Log4j2无法找到日志记录实现。请将log4j-core添加到类路径中。使用SimpleLogger记录到控制台...

然而,log4j-core-version.jar的所有类文件确实都包含在我的.jar文件中!
显然,某些代码片段尝试验证原始.jar文件的存在(并且没有意识到这些包含的类实际上存在于类路径中),然后发出警告。

我正在使用log4j-slf4j-impl-2.13.1.jar、slf4j-api-1.7.25.jar、log4j-api-2.13.1.jar和log4j-core-2.13.1.jar(或者更确切地说,只是它们的内容,而不是实际的.jar文件)。

是否有办法避免这种情况,并告诉日志记录代码不要关心那个.jar文件的存在?我不想因为这个问题而不得不处理并且总是不得不分发两个.jar文件,这让我很头疼。

英文:

I am building a Java application into a single executable .jar file such that I unpack all required jar files that the application depends on and include all their contents together with the actual application's class files into a single .jar file (that's an IMHO very handy feature of the classic ant jar task. One then doesn't need to handle or distribute multiple jars nor nested .jars or such).

This approach worked all fine so far, but now I upgraded from log4j(v1) to log4j2 (I am actually using the slf4j-API but the log4j version underneath changed).

Since that upgrade my approach does not seem to work anymore but at runtime I keep getting an error message:
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console....

However, all the class files of the log4j-core-version.jar are included in my .jar file!
Apparently some piece of code tries to verify the presence of the original .jar file (and doesn't realize that the contained classes are indeed present and on the classpath) and then yells out.

I am using log4j-slf4j-impl-2.13.1.jar, slf4j-api-1.7.25.jar, log4j-api-2.13.1.jar and log4j-core-2.13.1.jar (or rather - as described - just their contents, not the actual .jars).

Is there a way to avoid that and tell the logging code to not bother re. the presence of that .jar? I would hate to have to deal with and always have to distribute two .jar files just because of this.

答案1

得分: 0

Jar文件不仅包含类,还可以包含资源。Log4j 2插件系统使用一个文件来识别jar中的所有插件,而java.util.ServiceLoader则使用一个文件来识别某项服务的所有实现。没有这些文件,会导致一些功能无法正常运行。

假设您正在使用Maven Shade插件,您需要使用log4j shade transformer来识别插件,并且还需要使用ServicesResourceTransformer

英文:

Jar files contain more than just classes. They can also contain resources. The Log4j 2 Plugin system uses a file to identify all the plugins in the jar and java.util.ServiceLoader uses a file to identify all the implementations of a service. Without these files stuff breaks.

Assuming you are using the Maven shade plugin you need to use the log4j shade transformer for plugins to be recognized and the ServicesResourceTransformer.

答案2

得分: 0

在发送完我的问题后,我突然有了一个怀疑:也许检查log4j-core存在性的代码是在jar的MANIFEST文件夹中检查的!?最初,我总是省略将所包含的.jar文件的整个META-INF文件夹复制到生成的新的all-in-one.jar文件中。现在,我只排除了这些包含的.jar文件的实际MANIFEST.MF文件,惊喜吧,log4j的代码现在可以工作,不再抱怨了。我没有精确测试是哪个文件产生了差异,但显然将这些额外的文件保留下来就是这个神奇的咒语(现在有一个名为“services”的目录和另一个名为“versions”的目录,在组合的.jar文件中分别有多个文件,以及一堆pom文件和一个Log4j2Plugins.dat文件)。

英文:

Right after sending off my question I suddenly had a suspicion: maybe the code that checks for the presence of log4j-core checks for something in the jar's MANIFEST-folder!?!
Originally I had always omitted the entire META-INF folder of the included .jars from being copied over into the generated new all-in-one.jar file. Now I only exclude the actual MANIFEST.MF file of those included .jars and - surprise, surprise - now the log4j code works and doesn't complain anymore. I didn't test exactly what file made the difference but apparently leaving those additional files in was the magic incantation (there is now a directory called "services" and another one called "versions" with several files each in the combined .jar as well as a bunch of pom files and a Log4j2Plugins.dat).

huangapple
  • 本文由 发表于 2020年4月9日 03:21:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61108395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定