日志配置log4j 打印线程号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Set root logger level to WARN and append to stdout
log4j.rootLogger=INFO,stdout,R,R1
#日志文件输出目标,控制台/文件
#日志输出方式:控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=com.youcompan.project.log4j.Log4jExPatternLayout
log4j.appender.stdout.layout.ConversionPattern=[xxxx] %d %5p %T (%c.%M:%L) - %m%n
#应用服务器日志目录
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/ydbudget.log
log4j.appender.R.DatePattern = '.'yyyy-MM-dd'.log'
#log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.layout=com.youcompan.project.log4j.Log4jExPatternLayout
log4j.appender.R.layout.ConversionPattern=[xxxx]%d%5p %T (%C.%M:%L) - %m%n
log4j.appender.R.Append=true
#应用根目录
log4j.appender.R1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R1.File=${webApp.root}logs/ydbudget.log
log4j.appender.R1.DatePattern = '.'yyyy-MM-dd'.log'
#log4j.appender.R1.MaxFileSize=500KB
log4j.appender.R1.layout=com.youcompan.project.log4j.Log4jExPatternLayout
log4j.appender.R1.layout.ConversionPattern=[xxxx]%d%5p %T (%C.%M:%L) - %m%n
log4j.appender.R1.Append=true

输出三个地方 配置基本一致, 当然相对路径不同,按日滚动吧

其中应用服务器 中${catalina.home}是tomcat系的自带系统变量 ,无需其他配置

而应用根目录中的${webApp.root} 是在web.xml中通过spring定义的

1
2
3
4
5
6
7
8
9
10
11
<context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>webApp.root</param-value>
</context-param>
<context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>  
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
</listener>

现在来看布局类的定义和pattern的配置,

Log4jExPatternLayout 这个类是继承自 org.apache.log4j.PatternLayout ,其主要功能就是为了让 pattern 中的 %T 显示 线程号,上代码,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Log4jExPatternLayout extends PatternLayout {
    public Log4jExPatternLayout(String pattern){
        super(pattern);
    }
      
    public Log4jExPatternLayout(){
        super();
    }
     /**
      * 重写createPatternParser方法,返回PatternParser的子类
      */
     @Override
     protected PatternParser createPatternParser(String pattern) {
      return new Log4jExPatternParser(pattern);
     }
  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Log4jExPatternParser extends PatternParser {
  
    public Log4jExPatternParser(String pattern) {
        super(pattern);
    }
     /**
      * 重写finalizeConverter,对特定的占位符进行处理,T表示线程ID占位符
      */
     @Override
     protected void finalizeConverter(char c) {
      if (c == 'T') {
       this.addConverter(new ExPatternConverter(this.formattingInfo));
      } else {
       super.finalizeConverter(c);
      }
     }
  
     private static class ExPatternConverter extends PatternConverter {
  
      public ExPatternConverter(FormattingInfo fi) {
       super(fi);
      }
  
      /**
       * 当需要显示线程ID的时候,返回当前调用线程的ID
       */
      @Override
      protected String convert(LoggingEvent event) {
       return String.valueOf(Thread.currentThread().getId());
      }
  
     }
  
  
}

参考文档忘记了,这篇文章是后来整理的,感谢他们吧

 





posted @ 2013-08-23 15:41  九州浪子  阅读(17179)  评论(0编辑  收藏  举报