custom appender plugin not detected by log4j2

10,135

Solution 1

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?

Solution 2

Looking into docs here, you are probably missing annotation processor configuration in your maven compiler plugin.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.1</version>
  <executions>
    <execution>
      <id>log4j-plugin-processor</id>
      <goals>
        <goal>compile</goal>
      </goals>
      <phase>process-classes</phase>
      <configuration>
        <proc>only</proc>
        <annotationProcessors>
          <annotationProcessor>org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor</annotationProcessor>
        </annotationProcessors>
      </configuration>
    </execution>
  </executions>
</plugin>
Share:
10,135

Related videos on Youtube

halbs
Author by

halbs

Updated on June 04, 2022

Comments

  • halbs
    halbs almost 2 years

    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

  • halbs
    halbs about 9 years
    But the packages attribute is not supported in log4j2.0
  • Sumalatha Abhishek
    Sumalatha Abhishek about 9 years
    are you using the beta version or release ? I used the log4j-2.2.api and core
  • Robin Coe
    Robin Coe almost 9 years
    This answer is correct. The packages attribute is supported by log4j2, I use it to perform a triggered rollover strategy.