Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a unit test for a custom logger in log4j2

I've created a couple of custom loggers with some levels that override the custom ones in Log4J2. I've followed the guide at http://logging.apache.org/log4j/2.x/manual/customloglevels.html.

I need to create some unit test to verify that the events are being registered on their correct custom levels and configuration.

I appreciate any hint on how to start. Thanks a lot.

like image 595
EdwinF Avatar asked Dec 01 '22 18:12

EdwinF


1 Answers

Here you have what I've done in one of my JUnit Test.

1- Create a custom appender holding a list of messages in memory.

package com.example.appender;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import lombok.Getter;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;

/**
 * @author carrad
 * 
 */
@Plugin(name = "TestAppender", category = "Core", elementType = "appender", printObject = true)
public class TestAppender extends AbstractAppender {

    @Getter
    private final List<LogEvent> messages = new ArrayList<>();

    protected TestAppender(String name, Filter filter, Layout<? extends Serializable> layout) {
        super(name, filter, layout, true, Property.EMPTY_ARRAY);
    }

    @Override
    public void append(LogEvent event) {
        messages.add(event);
    }

    @PluginFactory
    public static TestAppender createAppender(
            @PluginAttribute("name") String name,
            @PluginElement("Layout") Layout<? extends Serializable> layout,
            @PluginElement("Filter") final Filter filter,
            @PluginAttribute("otherAttribute") String otherAttribute
    ) {
        if (name == null) {
            LOGGER.error("No name provided for TestAppender");
            return null;
        }
        if (layout == null) {
            layout = PatternLayout.createDefaultLayout();
        }
        return new TestAppender(name, filter, layout);
    }
}

2- Add the appender to the log4j2-test.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" packages="com.example.appender">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <TestAppender name="TestAppender" >
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </TestAppender> 
    </Appenders>
    <Loggers>
        <Logger name="com.example" level="All" />
        <Root>
            <AppenderRef ref="Console" level="All" />
            <AppenderRef ref="TestAppender" level="All" /> 
        </Root>
    </Loggers>
</Configuration>

3- Get a reference to the appender in the Junit test.

public class LoggingInterceptorTest {

    @Autowired  // Whatever component you want to test
    private InterceptedComponent helperComponent;

    private TestAppender appender;

    @Before
    public void setUp() {
        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        final Configuration config = ctx.getConfiguration();
        appender = (TestAppender) config.getAppenders().get("TestAppender");
    }

    @Test
    public void test_wrapping() {
        helperComponent.doStuff("437");
        Assert.assertEquals(appender.getMessages().size(), 2);
    }
}

In your test case you can check for the number of messages written or the list containing the messages you want, including meta-information such as level and so on.

like image 124
David Lopez Carrasco Avatar answered Dec 05 '22 11:12

David Lopez Carrasco