log4j Java配置问题

时间:2016-03-04 10:42:05

标签: java log4j config log4j2

我在使用Java配置log4j2时遇到了问题。

我有一个有效的XML配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="DEBUG">
<appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n" />
    </Console>
    <File name="DM_PREPUB" fileName="logs/41_2015/DM_PREPUB.log">
        <MarkerFilter marker="DM_PREPUB" onMatch="ACCEPT" onMismatch="DENY" />
        <PatternLayout>
            <pattern>%d %p %C{10} [%t] %m%n</pattern>
        </PatternLayout>
    </File>
    <File name="BACKUP" fileName="logs/41_2015/BACKUP.log">
        <MarkerFilter marker="BACKUP" onMatch="ACCEPT" onMismatch="DENY" />
        <PatternLayout>
            <pattern>%d %p %C{10} [%t] %m%n</pattern>
        </PatternLayout>
    </File>
</appenders>
<loggers>
    <logger name="foo.bar" level="DEBUG">
        <appender-ref ref="DM_PREPUB" />
        <appender-ref ref="BACKUP" />
    </logger>
    <root level="ALL">
        <appender-ref ref="Console" />
    </root>
</loggers>
</configuration>

如您所见,此配置会创建两个带有相应标记的FILE appender。相当简单。

我在Java中尝试并实现了相同的配置,但没有运气:

    ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    builder.setStatusLevel(org.apache.logging.log4j.Level.ALL);
    builder.setConfigurationName("DMWorkflow");
    builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("level", org.apache.logging.log4j.Level.DEBUG));
    // console appender
    AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
    appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n"));
    builder.add(appenderBuilder);
    // main log appender
    appenderBuilder = builder.newAppender("DM_PREPUB" /*appender name*/, "FILE" /*appender type*/)
            .addAttribute("fileName", "logs/41_2015/DM_PREPUB.log");
    appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d %p %C{10} [%t] %m%n"));
    appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.DENY).addAttribute("marker", "DM_PREPUB"));
    builder.add(appenderBuilder);
    // step appenders
    appenderBuilder = builder.newAppender("BACKUP" /*appender name*/, "FILE" /*appender type*/)
            .addAttribute("fileName", "logs/41_2015/BACKUP.log");
    appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d %p %C{10} [%t] %m%n"));
    appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.DENY).addAttribute("marker", "BACKUP"));
    builder.add(appenderBuilder);
    // main logger
    LoggerComponentBuilder componentBuilder = builder.newLogger("foo.bar", org.apache.logging.log4j.Level.DEBUG).add(builder.newAppenderRef("DM_PREPUB"));
    // step loggers
    componentBuilder.add(builder.newAppenderRef("BACKUP"));
    builder.add(componentBuilder);
    builder.add(builder.newRootLogger(org.apache.logging.log4j.Level.ALL).add(builder.newAppenderRef("Stdout")));
    Configurator.initialize(builder.build());

在这两种情况下,我都以这种方式实例化记录器:

    logger = LogManager.getLogger("foo.bar");
    globalMarker = MarkerManager.getMarker("DM_PREPUB");

Java配置代码似乎并不复杂,这就是为什么我不能设法看到我做错了什么。

如果你能得到任何帮助,那可能会很棒。

谢谢!

艾万

1 个答案:

答案 0 :(得分:3)

当我遇到类似问题时,对我来说有用的是使用以下方法初始化记录器:

((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)).start(builder.build());

然而,我没有尝试过你的方法。我的问题是记录器没有写任何东西,并且在调试时我发现我使用builder提供的配置从未实际使用过,因为Log4j2以某种方式决定它已经使用相同的配置而不需要重新配置

如果上述方法无效,请尝试调试以查看builder.build()Configurator.initialize()中的情况。据我所知,我的麻烦是由AbstractLifeCycle.state旗引起的,需要处于州INITIALIZED但过早成为STARTED