Log4jEx

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
/**
 * 
log4j.appender.myAppender=com.aspire.nm.component.demo.log4j_ex.MyAppender
log4j.appender.myAppender.params=12
 *
 */
public class MyAppender extends AppenderSkeleton {
	
	private String params;
	
	public String getParams() {
		return params;
	}

	//框架自动将属性读入
	public void setParams(String params) {
		System.out.println("set.... ");
		this.params = params;
	}

	//在所有属性都已设置好之后,框架就会调用这个方法
	public void activateOptions(){
		System.out.println("activateOptions.... ");
	}

	//处理日志记录请求
	public void append(LoggingEvent event){
		System.out.println("params: "+params);
		System.out.println("getLevel: "+event.getLevel());
		System.out.println("getLoggerName: "+event.getLoggerName());
		System.out.println("getMessage: "+event.getMessage());
		System.out.println("getThreadName: "+event.getThreadName());
		System.out.println("getLocationInformation: "+event.getLocationInformation());
		System.out.println("getThrowableInformation: "+event.getThrowableInformation());

	}
	
	
	//清理资源:  logger.shutdown时。
	public synchronized void close(){
		System.out.println("close.... ");
	}
	//是否需要Layout
	public boolean requiresLayout(){
		return true;
	}
}



你可能感兴趣的:(Log4jEx)