2015-10-20 50 views
0

我正在將EAR應用程序從Log4J 1.2.17遷移到Log4J2 2.4。請在下面找到EAR結構。Log4j2自定義插件無法在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) 

所有瓶子中的自定義插件類都在同一個包中 - 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我的問題張貼在計算器上是相同的。

Custom plugin not getting detected in EAR with log4j2 API

我使用Wildfly 8.2.0決賽AS和Maven構建EAR。

只要添加一個註釋,我總是在包含自定義插件的Jars中找到Log4JPlugins.dat文件,而不管我嘗試關於檢測插件的選項。

您的回覆對我來說非常重要,謝謝。

回答

0

我不相信log4j類能夠看到戰爭和應用程序罐的類別加載程序。

0

編譯自定義插件時,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添加到測試JAR中,因爲它很可能是類路徑中的第一個JAR。

這意味着您的自定義插件緩存缺失。

該解決方案使用收縮包裝是創建一個新的Log4j2Plugins.dat合併所需的任何自定義插件緩存文件,芯子,然後補充說,到JAR。

下面的函數實現了...

private static void mergeLog4J2Log4j2PluginsFile(JavaArchive ja, Class... uniqueJARClasses) { 
    // @Author: Johnathan Ingram <[email protected]> 
    // 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); 
相關問題