custom appender plugin not detected by log4j2

拟墨画扇 提交于 2019-12-05 03:48:40

问题


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


回答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?




回答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>


来源:https://stackoverflow.com/questions/28987951/custom-appender-plugin-not-detected-by-log4j2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!