log4j深入研究(2)--自定义appender

log4j.logger.com.bjsxt.service.impl=info,test3log

log4j.appender.test3log=com.bjsxt.util.LogUnit
log4j.appender.test3log.logMsg=logmsg
log4j.appender.test3log.logInfo=loginfo
log4j.appender.test3log.File=e\:/log/test3log.log
log4j.appender.test3log.layout=org.apache.log4j.PatternLayout 
log4j.appender.test3log.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%c-%L]-[%p] %m %n

 

 

import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

public class LogUnit extends FileAppender{
 
 private String logMsg;
 private String logInfo;
 
 public LogUnit() {
 }
 
 public LogUnit(String logMsg, String logInfo) {
  super();
  this.logMsg = logMsg;
  this.logInfo = logInfo;
 }

 

 /* (non-Javadoc)
  * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
  * 打印日志核心方法
  */
 @Override
 public void append(LoggingEvent event) {
//  super.append(event);
  
  this.qw.write(this.layout.format(event));

     if(layout.ignoresThrowable()) {
       String[] s = event.getThrowableStrRep();
       if (s != null) {
  int len = s.length;
  for(int i = 0; i < len; i++) {
    this.qw.write(s[i]);
    this.qw.write(Layout.LINE_SEP);
  }
       }
     }

     if(this.immediateFlush) {
       this.qw.flush();
     }
  
  LogLog.warn("Hello, " + logMsg + " : "+ event.getMessage()+" ,logInfo:"+logInfo);
  System.out.println("Hello, " + logMsg + " : "+ event.getMessage()+" ,logInfo:"+logInfo);
 }

 /* (non-Javadoc)
  * @see org.apache.log4j.AppenderSkeleton#close()
  * 释放资源
  */
 @Override
 public void close() {
  
 }

 /**
  * 是否需要按格式输出文本
  */
 @Override
 public boolean requiresLayout() {
  return true;
 }

 /* (non-Javadoc)
  * @see org.apache.log4j.AppenderSkeleton#activateOptions()
  * 初始化加载资源
  */
 @Override
 public void activateOptions() {
  super.activateOptions();
  logInfo+=" dd";
 }

 public String getLogMsg() {
  return logMsg;
 }

 public void setLogMsg(String logMsg) {
  this.logMsg = logMsg;
 }

 public String getLogInfo() {
  return logInfo;
 }

 public void setLogInfo(String logInfo) {
  this.logInfo = logInfo;
 }

 
}

log4j深入研究(2)--自定义appender

上一篇:移动开发策略的选择


下一篇:Android用户体验之意见反馈的重要