Java Log4j2自定义插件在EAR中不工作
我正在将EAR应用程序从Java Log4j2自定义插件在EAR中不工作,java,logging,log4j,log4j2,Java,Logging,Log4j,Log4j2,我正在将EAR应用程序从log4j1.2.17迁移到log4j22.4。请找到下面的耳朵结构 EAR -- APPLICATION JAR 1 (contains custom plugin) -- APPLICATION JAR 2 -- APPLICATION JAR 3 (contains custom plugin) -- APPLICATION JAR 4 -- APPLICATION WAR 1 -- APPLICATION WAR 2 -- APPLICATION WAR 3 --
log4j1.2.17
迁移到log4j22.4
。请找到下面的耳朵结构
EAR
-- APPLICATION JAR 1 (contains custom plugin)
-- APPLICATION JAR 2
-- APPLICATION JAR 3 (contains custom plugin)
-- APPLICATION JAR 4
-- APPLICATION WAR 1
-- APPLICATION WAR 2
-- APPLICATION WAR 3
-- OTHER THIRD PARTY APIs
-- lib/log4j-api-2.4.jar
-- lib/log4j-core-2.4.jar
-- lib/log4j-jcl-2.4.jar
-- lib/log4j-web-2.4.1.jar
-- META-INF/log4j2.xml
-- META-INF/MANIFEST.MF (contains all jars in class-path entry)
所有JAR中的自定义插件类都在同一个包中-com.test.it.logging
PFB初始化代码
System.setProperty(“log4j.configurationFile”,路径)强> 没有检测到任何已定义的自定义插件,我尝试了所有可用于初始化
log4j2.xml
和插件初始化的组合,但没有任何效果
它让我觉得定制插件在EAR中根本不起作用,因为我尝试了所有的排列和组合。这是log4j2(版本:2.4)中的错误吗?如果没有,那么请指导我如何定义在EAR中包含自定义插件的日志配置,该EAR中包含分散在多个JAR中的自定义插件
有人能告诉我如何配置吗
另外,PFB将我的问题张贴在同一网站的stackoverflow上
我使用Wildfly 8.2.0-Final AS和maven构建EAR
只需添加一个注释,我总是在包含自定义插件的jar中找到Log4JPlugins.dat
文件,而不管我尝试了哪些关于检测插件的选项
您的回答对我来说非常重要,谢谢。我不相信log4j类可以看到war和应用程序JAR的类浏览器。在编译自定义插件时,Log4J pom.xml定义了一个插件,该插件自动生成META-INF/org/apache/logging/Log4J/core/config/plugins/Log4j2Plugins.dat文件中的缓存数据 您可以在Maven项目的目标/类下看到这一点 log4j-core-2.x.x.jar还包含定义其缓存数据的Log4j2Plugins.dat 问题是,在使用包覆膜收缩器测试EAR时会创建一个JAR,通常会将log4j-core-2.x.x.JAR Log4j2Plugins.dat添加到测试JAR中,因为它很可能是类路径中的第一个 这意味着您的自定义插件缓存丢失 使用ShrinkWrap的解决方案是创建一个新的Log4j2Plugins.dat,将任何需要的自定义插件缓存文件与核心合并,然后将其添加到JAR中 下面的函数实现
private static void mergeLog4J2Log4j2PluginsFile(JavaArchive ja, Class... uniqueJARClasses) {
// @Author: Johnathan Ingram <jingram@rogueware.org>
// Log4J2 uses /META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat within a JAR to define custom plugins
// This is automatically generated by the plugin defined in the log4j-core-2.x.x pom.xml when compiling your custom plugin
// The problem with shrinkwrap is that the JAR is not preserved and only a single
// /META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat
// file can exist as JAR files cannot be added to a JAR file as a library.
// This is normally the default contained in log4j-core-2.x.x.jar which does not expose any custom plugins
// To rectify, both the core and the custom plugin JAR file Log4j2Plugins.dat need to be merged into a single Log4j2Plugins.dat
try {
// List of a unique class in each JAR containing a Log4j2Plugins.dat requiring merging
Vector<URL> datUrls = new Vector<URL>();
for (Class klass : uniqueJARClasses) {
// Find the JAR the class belongs to
URL classLoc = klass.getProtectionDomain().getCodeSource().getLocation();
URL resourceURL = classLoc.toString().endsWith(".jar")
? new URL("jar:" + URLDecoder.decode(classLoc.toString(), "UTF-8") + "!/META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat")
: new URL(URLDecoder.decode(classLoc.toString(), "UTF-8") + "/META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat");
datUrls.add(resourceURL);
}
// Use the Log4J2 PluginCache to build a merged Log4j2Plugins.dat
File mergedDatFile = new File("target/Log4j2Plugins.dat");
try (FileOutputStream fo = new FileOutputStream(mergedDatFile)) {
org.apache.logging.log4j.core.config.plugins.processor.PluginCache pc = new org.apache.logging.log4j.core.config.plugins.processor.PluginCache();
pc.loadCacheFiles(datUrls.elements());
pc.writeCache(fo);
}
// Replace the default Log4j2Plugins.dat if present
ja.delete("/META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat");
ja.addAsManifestResource(mergedDatFile, "org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat");
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}
JavaArchive ja = ShrinkWrap.create(JavaArchive.class, "my-test.jar");
...
mergeLog4J2Log4j2PluginsFile(ja, org.apache.logging.log4j.core.config.plugins.processor.PluginCache.class, MyCustomPlugin.class);