本文是小编为大家收集整理的关于Log4j2自定义插件在EAR中不工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我将EAR应用程序从Log4J 1.2.17迁移到Log4J2 2.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)
所有罐子中的自定义插件类都在同一软件包中 - com.test.it.logging.
pfb初始化代码.
-
添加自定义插件包.
pluginmanager.addpackage(" com.test,it.logging");
-
使用log4j2.xml初始化记录配置.
字符串路径=" path/log4j2.xml";
system.setproperty(" log4j.configurationfile",path);
均未检测到定义的自定义插件,我尝试了所有可用的组合来初始化log4j2.xml和插件初始化,但没有任何作用.
当我尝试所有排列和组合时,自定义插件根本没有在耳朵中工作.这是log4j2中的错误(版本:2.4)吗?如果否,请引导我如何定义包含ear的自定义插件的记录配置,其中包含散布在耳朵内许多罐子中的自定义插件?
任何人都可以让我知道如何配置
另外,PFB我的问题在同一stackoverflow中发布.
我正在使用Wildfly 8.2.0 final AS和Maven进行构建耳朵.
只是添加一个注释,我总是在包含自定义插件的罐子内找到Log4JPlugins.dat文件,无论我尝试检测插件的选项如何.
您的回应对我非常重要,谢谢.
推荐答案
我不认为log4j类对战争和应用程序罐的类别可见性.
其他推荐答案
编译自定义插件时,log4j pom.xml定义了一个插件,该插件会在文件meta-inf/org/apache/loging/log4j/log4j/core config/config/log4j2plugins.date中自动生成缓存数据 您可以在Maven项目中在目标/类下看到此.
log4j-core-2.x.x.jar还包含一个log4j2plugins.dat定义其缓存数据.
问题是在使用shninkwrap测试耳朵时创建一个jar,通常是log4j-core-2.x.x.x.jar log4j2plugins.dat添加到测试罐中,因为它很可能是在类路径中首次.<<<<<<<<<<<<<<<<<<<<<<<<
这意味着您的自定义插件缓存丢失.
使用缩小关节的解决方案是创建一个新的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);
问题描述
I am migrating an EAR application from Log4J 1.2.17 to Log4J2 2.4. Please find below the EAR structure.
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)
Custom plugin classes in all the jars are in the same package - com.test.it.logging.
PFB the initialization code.
Adding the custom plugins package.
PluginManager.addPackage("com.test,it.logging");
Initializing the logging configuration using log4j2.xml.
String path = "path/log4j2.xml";
System.setProperty("log4j.configurationFile", path);
None of the defined custom plugins are getting detected and I tried all the combinations available to initialize log4j2.xml and plugins initialization but nothing worked.
It gives me a feel that custom plugins is not at all working in EAR as I tried all the permutations and combinations. is this a BUG in log4j2 (version: 2.4) ? If no, then please guide me about how to define logging configuration containing custom plugins in an EAR containing custom plugins that are scattered across many jars within an EAR ?
Can anyone please let me know about how to configure
Also, PFB my question posted in stackoverflow on the same.
Custom plugin not getting detected in EAR with log4j2 API
I am using Wildfly 8.2.0-Final AS and maven for building EAR.
Just adding a note that I am always finding Log4JPlugins.dat file inside Jars containing custom plugins irrespective of the options I try regarding detecting plugins.
Your response is highly important to me and thanks.
推荐答案
I don't believe the log4j classes have visibility into the classloaers for the war and application jars.
其他推荐答案
When compiling a custom Plugin, the Log4J pom.xml defines a plugin that automatically generates cache data in the file META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat You can see this under your target/classes in a Maven project.
The log4j-core-2.x.x.jar also contains a Log4j2Plugins.dat defining its cache data.
The problem is a single JAR is created when testing an EAR using ShrinkWrap and normally the log4j-core-2.x.x.jar Log4j2Plugins.dat is added to the test JAR as it would most likely be first in the class path.
This means your custom plugin cache is missing.
The solution using ShrinkWrap is to create a new Log4j2Plugins.dat merging any required custom plugin cache files with the cores and then adding that to the JAR.
The following function achieves that...
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); } }
To run:
JavaArchive ja = ShrinkWrap.create(JavaArchive.class, "my-test.jar"); ... mergeLog4J2Log4j2PluginsFile(ja, org.apache.logging.log4j.core.config.plugins.processor.PluginCache.class, MyCustomPlugin.class);