仅通过代码配置log4j2 - 如果配置丢失

时间:2017-04-12 07:27:00

标签: java log4j2

如果在初始化过程中没有找到配置文件,我想用代码 配置log4j2日志框架。

我读了Programmatic Configuration的可能性。至于现在,我能够检测到log4j是在没有任何配置(ConfigurationSource.NULL_SOURCE)的情况下初始化的,并以编程方式激活我的新默认配置。但在这种情况下,我仍然得到log4j2 Status Loggor关于缺失配置的错误消息,我必须停止当前配置并启动我的新配置。

我想知道是不是可以在我的用例中使用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 个答案:

答案 0 :(得分:2)

这种方式可能并不完美,但我认为它可以满足您的需求。

这是一个简短的例子:

<强> 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
  }
}

然后在src/main/resources中创建一个名为 log4j2.code 的空文件。

注意:

最重要的是确保自定义log4j2配置资源文件的suffixCustomConfigurationFactory.getSupportedTypes方法的返回值相对应。

<强>更新

如果log4j2无法找到CustomConfigurationFactory插件,请尝试启用注释处理。