spring mvc 异常处理Log4j配置

1.肯定是要导入相应的log4j包:log4j.x.jar包;

2.在web.xml写入上下文:

  
        log4jConfigLocation  
       	classpath:log4j.properties  
  

3.配置log4j.properties属性文件:

# FATAL, ERROR, WARN, INFO, DEBUG  
log4j.rootLogger=WARN,stdout,D
  
#stdout
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
  
#logFile 
### save error to another file ###  
log4j.appender.D=org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File=../logs/sw_error.log
log4j.appender.D.Append=true
#error only in this file
log4j.appender.D.Threshold =WARN
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
4.全局异常处理配置,在spring-mvc.xml中配置:

       
                
                    
                 errors/error       
                 errors/err       
                    
                
                
                    
                 500       
                 404       
                    
                
                
                
                
               
                
                
5、现在可以测试了:

private final Log logger = LogFactory.getLog(SimplerController.class);  
  
logger.warn(" logger");  
logger.error("~~info~~");


想了解log4j.properties配置详情:http://blog.csdn.net/snakeqi/article/details/5183070

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