log4j:WARN创建自定义Appender时没有这样的属性[append]

时间:2015-05-04 14:33:21

标签: java log4j

任何人都可以建议如何摆脱这个错误。

我在How to create a own Appender in log4j?

的帮助下制作了自定义Appender

我的追随者是:

package com.abc.log4jlearn;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

public class MyAppender extends AppenderSkeleton{
    private LogManager logManager;

    public MyAppender (){
        this.logManager = LogManager.getInstance();
    }

    @Override
    protected void append(LoggingEvent loggingEvent) {
        String message = loggingEvent.getMessage().toString();
        logManager.addthese(message);
    }

    @Override
    public void close() {
        if(this.closed) {
            return;
        }
        this.closed = true;
    }
    @Override
    public boolean requiresLayout() {
        return false;
    }

    boolean checkEntryConditions() {
        return true;
    }
}

LogManager类是:

public class LogManager {
    public LogManager() {}

    public static LogManager getInstance() {
        return new LogManager();

    }
    public void addthese(String message) {
        System.out.println("Hey...");
        System.out.println(message);

    }
}

Log4j.xml配置为:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="MyAppender" class="package com.abc.log4jlearn.MyAppender">
        <param name="Append" value="false"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d{ISO8601} %c{1} - %m%n"/>
        </layout>
    </appender>

    <root>
        <priority value="debug"/>
        <appender-ref ref="MyAppender"/>
    </root>

</log4j:configuration>

测试类是:

public class Log4jLearnkDemo {
private static Logger logger = Logger.getLogger(Log4jLearnkDemo.class);
public static void main(String[] args){
    logger.debug("Hello this is a debug message");
    logger.info("Hello this is an info message");

}
}

当我运行Log4jLearnDemo类时,我得到输出

Hey...
Hello this is a debug message
Hey..
Hello this is an info message
log4j:WARN No such property [append] in com.abc.log4jlearn.MyAppender. 

有人可以说明我收到此错误的原因吗?

谢谢,

1 个答案:

答案 0 :(得分:3)

在Log4j.xml中,为一个名为&#34; Append&#34;的param定义以下行:有价值&#34;假&#34;:

 <param name="Append" value="false"/>

如果您取消注释(可能是您所采用的示例)

<!-- param name="Append" value="false"/ -->

消息将消失。

否则,如果你想给MyAppender&#34;追加&#34; -Logic,那么该类可能需要类似

的内容
private boolean append;
// get/set for the property