custom appender plugin not detected by log4j2

halbs picture halbs · Mar 11, 2015 · Viewed 8.9k times · Source

I am trying to create a custom appender for log4j 2.0, but am having issues getting my log4j configuration to recognize the appender. I know log4j 2.0 doesnt support packages in configuration attributes. So I tried, as suggested here, running the code with plain javac but even then it gives this error:2015-03-11 18:47:35,281 ERROR Error processing element Test: CLASS_NOT_FOUND 2015-03-11 18:47:35,307 ERROR Unable to locate appender test1 for logger

Here is my custom appender:

@Plugin(name = "Test", category = "Core", elementType = "appender", printObject = true)
public class TestAppender extends AbstractAppender{
protected TestAppender(String name, Filter filter,
        Layout<? extends Serializable> layout, boolean ignoreExceptions) {
    super(name, filter, layout, ignoreExceptions);
    // TODO Auto-generated constructor stub
}

@PluginFactory
public static TestAppender createAppender(@PluginAttribute("name") String name,
        @PluginAttribute("ignoreExceptions") boolean ignoreExceptions,
        @PluginElement("Layout") Layout<? extends Serializable> layout,
        @PluginElement("Filters") Filter filter) {



    return new TestAppender(name, filter, layout, true);        
}

public void append(LogEvent event) {
    // TODO Auto-generated method stub
    System.out.println(event.getMessage());
}

}

and my config xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" >
<Appenders>
    <Test name="test1" >
        <PatternLayout pattern="%d %msg%n" />
        <ThresholdFilter level="DEBUG" />
    </Test>
</Appenders>
<Loggers>
    <Root level="DEBUG">
        <AppenderRef ref="test1" />
    </Root>
</Loggers>
</Configuration>

Thanks in advance for any useful input

Answer

Sumalatha Abhishek picture Sumalatha Abhishek · Mar 18, 2015

I added the package containing the Custom Appender in the Configuration of log4j2.xml before the appenders and it picked up the custom appender with no errors.

<Configuration packages="com.yourcompany.yourcustomappenderpackage">

I referred this thread - How to Create a Custom Appender in log4j2?