log4J2 EventLogger使用Level.OFF

时间:2014-12-22 07:36:36

标签: log4j2

我读了log4j2的EventLogger源代码,我无法理解 为什么EventLogger使用Level.OFF可以显示登录控制台

感谢您的回复。

EventLogger.java

package org.apache.logging.log4j;

public final class EventLogger {


/**
 * Log events with a level of ALL.
 * @param msg The event StructuredDataMessage.
 */
public static void logEvent(final StructuredDataMessage msg) {
    LOGGER.logIfEnabled(FQCN, Level.OFF, EVENT_MARKER, msg, null);
}

TestEventLogger.java

public class TestEventLogger {
private static final String CONFIG = "log4j2-eventLogger.xml";
private static Logger logger;

@Before
public void setup() {
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,CONFIG);
    logger = LogManager.getLogger();
}

@Test
public void structuredData() {
    ThreadContext.put("loginId", "JohnDoe");
    ThreadContext.put("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "123457");
    msg.put("Amount", "200.00");
    EventLogger.logEvent(msg);
    logger.debug(msg);
    ThreadContext.clearMap();
  }

log4j2-eventLogger.xml

<configuration status="error">
<appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="[%d{yyyy-mm-dd HH:mm:ss.SSS}] %-5level %c{1} - %msg %n%n" />
    </Console>
</appenders>

<loggers>
    <root level="ERROR">
        <appender-ref ref="Console" />
    </root>
</loggers>

控制台

[2014-41-22 14:41:58.568] OFF   EventLogger - Audit [Transfer@18060 Amount="200.00" FromAccount="123457" ToAccount="123456"] Transfer Complete 

1 个答案:

答案 0 :(得分:1)

/**
 * Log events with a level of ALL.
 * @param msg The event StructuredDataMessage.
 */
public static void logEvent(final StructuredDataMessage msg) {
    LOGGER.logIfEnabled(FQCN, Level.OFF, EVENT_MARKER, msg, null);
}

以上代码表示:如果记录器配置为允许Level.OFF或更高的消息,请记录此事件。由于Level.OFF是可能的最低级别,因此所有消息都将通过。