log4j 使用

1 在项目中添加如下配置:

  

### set log levels ###
log4j.rootLogger = INFO,stdout,D,E

### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n

### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E:/logs/intelStore_log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 保存异常信息到单独文件 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = E:/logs/intelStore_error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %l:%c:%t:%r ] - [ %p ]  %m%n

    针对log4j的配置项的意义,网上很多也很详细,在此不再叙述

2 在web.xml中配置log4j配置文件的位置(默认情况下,系统会到classpath下寻找log4j.properties文件):

  

<!-- log4j -->
    <context-param>  
	    <param-name>log4jConfigLocation</param-name>  
	    	<param-value>/WEB-INF/config/log4j.properties</param-value>  
	    </context-param>  
    <listener>  
    	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
    </listener>

3 在项目中引入log4j的jar包,在类中做如下使用:

  

final Logger log = Logger.getLogger(CommonCostController.class);

相关推荐