log4j2未检测到自定义appender插件

时间:2015-03-11 13:30:55

标签: java log4j2 appender

我正在尝试为log4j 2.0创建自定义appender,但是在让我的log4j配置识别appender时遇到问题。我知道log4j 2.0不支持配置属性中的包。所以我按照建议here尝试使用普通的javac运行代码,但即便如此,它也会出现此错误: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

这是我的自定义附加程序:

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

}

和我的配置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>

提前感谢任何有用的输入

2 个答案:

答案 0 :(得分:10)

我在appender之前在log4j2.xml的配置中添加了包含Custom Appender的软件包,它选择了没有错误的自定义appender。

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

我引用了这个帖子 - How to Create a Custom Appender in log4j2?

答案 1 :(得分:1)

查看文档here,您可能在maven编译器插件中缺少注释处理器配置。

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