2017-04-12 135 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

相关问题