Custom AppenderBase not being called

2019-07-27 00:01发布

问题:

I am writing a custom AppenderBase for my test and configuring it in logback-test.xml The append() function however is not being called.

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);
    }
}

This is my configuration file : 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>

This is my code under test

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

Inside a function I call :

LOGGER.error("Unhandled type");

Inside my Test I check :

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

This is my stack trace :

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

回答1:

I was using LoggingEvent instead of ILoggingEvent. Changing it solved my problem.



回答2:

Logback status messages indicate that the <root> element was placed inside an appender <element> in your configuration file. This won't work. However, the configuration file above does not have this problem, that is, it does not have a <root> element was placed inside an appender <element>.

Do the configuration file and status messages listed in your question match?

Also, you are declaring the <root> element twice. Instead, place two <appender-ref> elements in one <root>.