以编程方式设置Log4j2记录器和附加器

时间:2019-01-21 16:44:21

标签: java log4j2 rollingfileappender

我无法以编程方式分配log4j2属性文件。

我试图设置2个不同的记录器及其各自的附加器。

但是即使在Configurator.initialize()之后,我也无法获得理想的结果。

这是我的代码

    ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();

    AppenderComponentBuilder rollingFileDrop = builder.newAppender("rollingDrop", "RollingFile");
    rollingFileDrop.addAttribute("fileName", directory + "/droppedMessages.log");
    rollingFileDrop.addAttribute("filePattern", directory + "/droppedMessages-%d{yyyy-MM-dd-hh-mm}.log");

    ComponentBuilder triggeringPolicies = builder.newComponent("Policies")
            .addComponent(builder.newComponent("TimeBasedTriggeringPolicy")
                    .addAttribute("interval", String.valueOf(timeInterval))
                    .addAttribute("modulate", "true"));

    rollingFileDrop.addComponent(triggeringPolicies);

    LayoutComponentBuilder patternLayout = builder.newLayout("PatternLayout");
    patternLayout.addAttribute("pattern", "%m%n");

    rollingFileDrop.addComponent(patternLayout);
    builder.add(rollingFileDrop);

    LoggerComponentBuilder droplog = builder.newLogger("droplog", Level.ERROR);
    droplog.add(builder.newAppenderRef("rollingDrop"));
    droplog.addAttribute("additivity", false);
    builder.add(droplog);

    AppenderComponentBuilder rollingFileError = builder.newAppender("rollingError", "RollingFile");
    rollingFileError.addAttribute("fileName", directory + "/kafkaFailedMsg.log");
    rollingFileError.addAttribute("filePattern", directory + "/kafkaFailedMsg-%d{yyyy-MM-dd-hh-mm}.log");

    rollingFileError.addComponent(triggeringPolicies);

    rollingFileError.addComponent(patternLayout);
    builder.add(rollingFileError);

    LoggerComponentBuilder errorLog = builder.newLogger("kafkaErrorLog", Level.ERROR);
    errorLog.add(builder.newAppenderRef("rollingError"));
    errorLog.addAttribute("additivity", false);
    builder.add(errorLog);

    LOG.info("Error Log setup successfully");


    BuiltConfiguration configuration = builder.build();
    System.out.println(configuration.getLoggers());
    System.out.println(configuration.getAppenders());

    LoggerContext loggerContext = (LoggerContext) LogManager.getContext();
    loggerContext.updateLoggers(configuration);


    System.out.println(loggerContext);
    Map<String, LoggerConfig> map = loggerContext.getConfiguration().getLoggers();
    Map<String, Appender> map2 = loggerContext.getConfiguration().getAppenders();

    System.out.println(map);
    System.out.println(map2);

    ERROR_LOG = loggerContext.getLogger("kafkaErrorLog");

因此,我得到以下结果: 正在创建文件“ kafkaFailedMsgs.log”和“ droppedMsgs.log”,但日志未写入其中,而仅写入控制台。

这是我收到的消息:

main ERROR No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging. 

System.out语句的输出为

{kafkaErrorLog=kafkaErrorLog, droplog=droplog}
{rollingDrop=rollingDrop, rollingError=rollingError, DefaultConsole-3=DefaultConsole-3}
org.apache.logging.log4j.core.LoggerContext@399c4be1
{}
{DefaultConsole-5=DefaultConsole-5}


<?xml version="1.0" ?>
<Configuration>
  <Appenders>
    <RollingFile name="rollingDrop" fileName="/home/lehar/test/logs/droppedMessages.log" filePattern="/home/lehar/test/logs/droppedMessages-%d{yyyy-MM-dd-hh-mm}.log">
      <Policies>
        <TimeBasedTriggeringPolicy interval="2" modulate="true"/>
      </Policies>
      <PatternLayout pattern="%m%n"/>
    </RollingFile>
    <RollingFile name="rollingError" fileName="/home/lehar/test/logs/kafkaFailedMsg.log" filePattern="/home/lehar/test/logs/kafkaFailedMsg-%d{yyyy-MM-dd-hh-mm}.log">
      <Policies>
        <TimeBasedTriggeringPolicy interval="2" modulate="true"/>
      </Policies>
      <PatternLayout pattern="%m%n"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Logger name="droplog" level="ERROR" additivity="false">
      <AppenderRef ref="rollingDrop"/>
    </Logger>
    <Logger name="kafkaErrorLog" level="ERROR" additivity="false">
      <AppenderRef ref="rollingError"/>
    </Logger>
  </Loggers>
</Configuration>

0 个答案:

没有答案