spring mvc log4j 配置

1、首页在WEB-INF目录下新建 log4j.properties 配置文件,内容如下:

log4j.rootCategory = INFO,dailyFile  

log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyFile.Threshold=DEBUG 
log4j.appender.dailyFile.ImmediateFlush=true
log4j.appender.dailyFile.Append=true
log4j.appender.dailyFile.File=c:/logs/log.log
log4j.appender.dailyFile.DatePattern=yyyy-MM-dd'.log'
log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyFile.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n

2、修改web.xml配置,将如下配置加入到一级节点下:

<context-param>

   <param-name>log4jConfigLocation</param-name>

   <param-value>/WEB-INF/log4j.properties</param-value>

</context-param>

<listener>

   <listener-class>

    org.springframework.web.util.Log4jConfigListener

   </listener-class>

</listener>

3、java文件中使用:

Logger log = Logger.getLogger(this.getClass().getName());
log.info(" logger");  
log.error("error");

//log.debug("debug");

这里debug不会写入到日志中。详见log4j.properties 详细配置说明。

 

posted on 2015-04-24 11:46  lvlv岁月流逝  阅读(418)  评论(0编辑  收藏  举报

导航