【问题标题】:Log4j2 custom plugins not working in EARLog4j2 自定义插件在 EAR 中不起作用
【发布时间】:2015-10-20 13:21:43
【问题描述】:

我正在将 EAR 应用程序从 Log4J 1.2.17 迁移到 Log4J2 2.4。请在 EAR 结构下方找到。

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 初始化代码。

  1. 添加自定义插件包。

    PluginManager.addPackage("com.test,it.logging");

  2. 使用 log4j2.xml 初始化日志配置。

String path = "path/log4j2.xml";
System.setProperty("log4j.configurationFile", path);

未检测到任何已定义的自定义插件,我尝试了所有可用于初始化 log4j2.xml 和插件初始化的组合,但没有任何效果。

当我尝试了所有的排列和组合时,它让我感觉自定义插件根本无法在 EAR 中运行。这是 log4j2(版本:2.4)中的 BUG 吗?如果不是,那么请指导我如何在 EAR 中定义包含自定义插件的日志记录配置,其中包含分散在 EAR 中许多 jar 中的自定义插件?

谁能告诉我如何配置

另外,PFB 我的问题也发布在 stackoverflow 上。

Custom plugin not getting detected in EAR with log4j2 API

我正在使用 Wildfly 8.2.0-Final AS 和 maven 来构建 EAR。

只需添加一条注释,我总是在包含自定义插件的 Jars 中找到 Log4JPlugins.dat 文件,而不管我尝试检测插件的选项如何。

您的回复对我来说非常重要,谢谢。

【问题讨论】:

    标签: java logging log4j log4j2


    【解决方案1】:

    我不相信 log4j 类对战争和应用程序 jar 的类加载器具有可见性。

    【讨论】:

      【解决方案2】:

      在编译自定义插件时,Log4J pom.xml 定义了一个插件,该插件会在 META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat 文件中自动生成缓存数据 您可以在 Maven 项目中的目标/类下看到这一点。

      log4j-core-2.x.x.jar 还包含定义其缓存数据的 Log4j2Plugins.dat。

      问题是在使用 ShrinkWrap 测试 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);
      

      【讨论】:

        猜你喜欢
        • 2016-01-17
        • 2020-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-01
        • 1970-01-01
        相关资源
        最近更新 更多