log4j 自定义打印行号

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.varia.LevelMatchFilter;
public class LevelFilter extends LevelMatchFilter{
private static Layout layout;//用来存储原始的layout,以便对不是相应级别的日志回复layout
private static Logger logger;
private static List<Appender> appenderList=new ArrayList<Appender>();//记录当前修改过的appender,以便回复layout

private String appenderName;//这一行可以定制化,在配置文件里配置,可以设置为如果为空的话,对所有appender都试用
private String conversionPatternString ;//自定义的layout

public String getConversionPatternString() {
  return conversionPatternString;
}
public void setConversionPatternString(String conversionPatternString) {
  this.conversionPatternString = conversionPatternString;
}
public String getAppenderName() {
  return appenderName;
}
public void setAppenderName(String appenderName) {
  this.appenderName = appenderName;
}
@Override
public  int decide(LoggingEvent event) {
  int status=super.decide(event);
  //如果当前级别符合配置中所配置的级别,那么按照相应参数修改其layout
  if(Filter.ACCEPT==status){
   //获取当前logger
      logger=Logger.getLogger(event.getLoggerName());
   PatternLayout patternLayout=new PatternLayout();
   patternLayout.setConversionPattern(conversionPatternString);
   Logger parentLogger=logger;
   while (true) {
//    获取指定appender
    Appender appender=parentLogger.getAppender(appenderName);
    if(appender!=null){
     appenderList.add(appender);
     layout=appender.getLayout();
     appender.setLayout(patternLayout);
     break;
    }
    //如果没有取到,则调用其父logger
    parentLogger=(Logger) parentLogger.getParent();
    //如果循环到其根logger都没有出现这个appender,那么跳出并且打印错误信息,很抱歉,这个方法是重载的好像不能抛出异常
    if(parentLogger==null){
     logger.error("no appender named  \""+appenderName+"\" found for logger:"+event.getLoggerName());
     break;
    }
   }
  }
  //如果级别不符,那么对已经修改了的appender的layout进行恢复
  else{
   if(null!=appenderList&&appenderList.size()>0&&null!=layout){
    for (int i = 0; i < appenderList.size(); i++) {
     appenderList.get(i).setLayout(layout);
    
    }
   }
  }
 
  return status;
}
}




原有的配置:
   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%p [%t] %c{1}.%M | %m%n"/>
        </layout>
   </appender>
修改为:
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%p [%t] %c{1}.%M | %m%n"/>
        </layout>
        <filter class="com.taobao.top.LevelFilter">
    <param name="LevelToMatch" value="debug" />
    <param name="AcceptOnMatch" value="true" />
    <param name="AppenderName" value="CONSOLE"/>
    <param name="ConversionPatternString" value="%L %p [%t] %c{1}.%M | %m%n "/>
        </filter>
    </appender>

测试类:
public class LevelFilterTest {
    private  final Log log = LogFactory.getLog(getClass());

    @Test
    public void test(){
log.debug("debug");
  log.warn("warn");
  log.info("info");
}
}

输出结果:
注意:以下结果只有debug的时候显示了行号,这是因为在配置中的设置。这就是针对debug级别的个性化输出

12 DEBUG [main] LevelFilterTest.test | debug
WARN [main] LevelFilterTest.test | warn
INFO [main] LevelFilterTest.test | info

你可能感兴趣的:(apache,log4j)