2017-04-12 132 views
0

我想通過代碼只配置log4j2日誌框架以防在初始化過程中未找到配置文件。按代碼配置log4j2 - 在配置丟失的情況下

我讀了可能性obout Programmatic Configuration。至於現在我能夠檢測到log4j在沒有任何配置的情況下被初始化(ConfigurationSource.NULL_SOURCE)並以編程方式激活我的新默認配置。但在這種情況下,我仍然收到有關缺少配置的log4j2狀態記錄器的錯誤消息,我必須停止當前配置並啓動新配置。

我不知道是否不可能在我的用例中使用ConfigurationFactory方法。但我不知道如何將它掛接到初始化過程中,以便只有在沒有其他配置方法成功時才能激活它。我想離開一般配置過程和支持的文件類型不變。

我目前的做法是這樣的

@Plugin(name="FooBarConfigurationFactory", category = ConfigurationFactory.CATEGORY) 
@Order(0) // should be very low priority 
public class FooBarConfigurationFactory extends ConfigurationFactory { 

    @Override 
    protected String[] getSupportedTypes() { 
     // what to return here if the factory should support 'missing' configuration file? 
     return new String[] {"*"}; 
    } 

    @Override 
    public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { 
     System.out.println("Get FooBar Configuration..."); // never saw this so far :-(
     return ...; 
    } 

感謝所有幫助

弗朗茨

回答

1

有一種方法可能不是完美的,但我認爲它能夠滿足您的需求。

這裏有一個簡單的例子:

Log4j2.java

package test; 

import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 
import org.apache.logging.log4j.core.config.plugins.util.PluginManager; 

public class Log4j2 { 

    private static Logger logger = LogManager.getLogger(); 

    public static void main(String[] args) { 
    logger.info("hello"); 
    } 
} 

CustomConfigurationFactory.java

package test; 

import java.net.URI; 
import org.apache.logging.log4j.Level; 
import org.apache.logging.log4j.core.LoggerContext; 
import org.apache.logging.log4j.core.appender.ConsoleAppender; 
import org.apache.logging.log4j.core.config.Configuration; 
import org.apache.logging.log4j.core.config.ConfigurationFactory; 
import org.apache.logging.log4j.core.config.ConfigurationSource; 
import org.apache.logging.log4j.core.config.Order; 
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; 
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; 
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder; 
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; 
import org.apache.logging.log4j.core.config.plugins.Plugin; 

@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY) 
@Order(0) 
public class CustomConfigurationFactory extends ConfigurationFactory { 

    private static Configuration createConfiguration(
     final String name, ConfigurationBuilder<BuiltConfiguration> builder) { 
    builder.setConfigurationName(name); 

    AppenderComponentBuilder appenderBuilder = 
     builder 
      .newAppender("Stdout", "CONSOLE") 
      .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); 
    appenderBuilder.add(
     builder.newLayout("PatternLayout").addAttribute("pattern", "%level: %msg%n")); 
    builder.add(appenderBuilder); 

    RootLoggerComponentBuilder rootLoggerBuilder = builder.newRootLogger(Level.DEBUG); 
    rootLoggerBuilder.add(builder.newAppenderRef("Stdout")); 

    builder.add(rootLoggerBuilder); 
    return builder.build(); 
    } 

    @Override 
    public Configuration getConfiguration(
     final LoggerContext loggerContext, final ConfigurationSource source) { 
    return getConfiguration(loggerContext, source.toString(), null); 
    } 

    @Override 
    public Configuration getConfiguration(
     final LoggerContext loggerContext, final String name, final URI configLocation) { 
    ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder(); 
    return createConfiguration(name, builder); 
    } 

    @Override 
    protected String[] getSupportedTypes() { 
    return new String[] {".code"}; // IMPORTANT 
    } 
} 

然後創建一個名爲log4j2.code在一個空文件10。

注:

最重要的是要確保您的自定義log4j2配置資源文件的suffix對應於CustomConfigurationFactory.getSupportedTypes方法的返回值。


UPDATE

如果log4j2水溼找到CustomConfigurationFactory插件,嘗試啓用註釋處理。

+0

+ q放置某種標記文件對我來說可以。不幸的是我注意到我無法確保第一個在我的上下文中調用Logger,因此ConfigurationFactory解決方案可能不會工作,因爲log4j2默認配置已經完成了,對嗎? – FrVaBe

+0

不需要調用'PluginManager.addPackage'方法,因爲log4j-core-2。*。jar中的PluginProcessor會自動掃描自定義插件。你可以試試。 – wangyuntao

+0

我已更新我的回答 – wangyuntao

相關問題