自定义AppenderBase未被调用

时间:2018-01-31 20:41:39

标签: java junit logback slf4j

我正在为我的测试编写自定义AppenderBase并在logback-test.xml中进行配置 然而,append()函数没有被调用。

import ch.qos.logback.core.AppenderBase;
import org.apache.log4j.spi.LoggingEvent;

import java.util.ArrayList;
import java.util.List;

public class TestAppender extends AppenderBase<LoggingEvent> {
    public static List<LoggingEvent> events = new ArrayList<>();

    @Override
    protected void append(LoggingEvent e) {
        events.add(e);
    }
}

这是我的配置文件:logback-test.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="false">
    <contextName>logging</contextName>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <root level="WARN">
        <appender-ref ref="CONSOLE" />
    </root>

    <logger name="com.egencia.library" level="INFO" />


        
    <appender name="map" class="com.egencia.service.contact.controller.TestAppender" />
        
    <root level="ERROR">
                
        <appender-ref ref="map"/>
            
    </root>


</configuration>

这是我的测试代码

private final static Logger LOGGER = LoggerFactory.getLogger(ServiceController.class);

在我调用的函数中:

LOGGER.error("Unhandled type");

在我的测试中,我检查:

Assert.assertEquals(1, TestAppender.events.size());

这是我的堆栈跟踪:

12:31:22,715 |-INFO in ch.qos.logback.classic.LoggerContext[logging] - Could NOT find resource [logback.groovy]
12:31:22,715 |-INFO in ch.qos.logback.classic.LoggerContext[logging] - Found resource [logback-test.xml] at [file:/Users/barora/stash/contact-service/contact-service/target/test-classes/logback-test.xml]
12:31:22,831 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
12:31:22,839 |-INFO in ch.qos.logback.classic.joran.action.ContextNameAction - Setting logger context name as [logging]
12:31:22,839 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
12:31:22,844 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [CONSOLE]
12:31:22,851 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
12:31:22,934 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to WARN
12:31:22,934 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[ROOT]
12:31:22,936 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.egencia.library] to INFO
12:31:22,936 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [com.egencia.service.contact.controller.TestAppender]
12:31:22,940 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [map]
12:31:22,941 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@23:22 - no applicable action for [root], current ElementPath  is [[configuration][appender][root]]
12:31:22,942 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@25:28 - no applicable action for [appender-ref], current ElementPath  is [[configuration][appender][root][appender-ref]]
12:31:22,942 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
12:31:22,943 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@27953a83 - Registering current configuration as safe fallback point

2 个答案:

答案 0 :(得分:1)

我使用的是LoggingEvent而不是ILoggingEvent。改变它解决了我的问题。

答案 1 :(得分:0)

回溯状态消息表明&lt; root&gt; element被放置在一个appender&lt; element&gt;里面。在您的配置文件中。这不行。但是,上面的配置文件会出现此问题,即的&lt; root&gt;元素放在一个appender&lt; element&gt;里面。

问题中列出的配置文件和状态消息是否匹配?

另外,您要声明&lt; root&gt;元素两次。相反,放置两个&lt; appender-ref&gt;一个&lt; root&gt;中的元素。