Spring 配置log4j(转)
1、在WEB中加入对Spring对log4j的支持:
即在web.xml文件中加入如下代码:
Java代码
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webName.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/config/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>3000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webName.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/config/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>3000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
注意:这里webAppRootKey代表你的项目名 。
2、log4j.properties文件放在WEB-INF/config下
配置log4j.properties文件:
Java代码
log4j.rootCategory=INFO, stdout , R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=${webName.root}/WEB-INF/logs/log.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern ='.'yyyy-MM-dd
log4j.appender.R.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n
log4j.logger.org.springframework=DEBUG
log4j.logger.com.ibatis.db=WARN
log4j.logger.org.hibernate=DEBUG
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
log4j.logger.com.myproject=DEBUG
log4j.rootCategory=INFO, stdout , R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=${webName.root}/WEB-INF/logs/log.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern ='.'yyyy-MM-dd
log4j.appender.R.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n
log4j.logger.org.springframework=DEBUG
log4j.logger.com.ibatis.db=WARN
log4j.logger.org.hibernate=DEBUG
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
log4j.logger.com.myproject=DEBUG
注意:
log4j.appender.R.File=${webName.root}/WEB-INF/logs/log.log 指日志产生的路径 在 WEB-INF/logs/log.log
该配置是每天产生1个log文件
Java代码
log4j.logger.org.springframework=DEBUG
log4j.logger.com.ibatis.db=WARN
log4j.logger.org.hibernate=DEBUG
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
log4j.logger.org.springframework=DEBUG
log4j.logger.com.ibatis.db=WARN
log4j.logger.org.hibernate=DEBUG
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
这里可以更详细的用日志来显示spring hibernate/ibatis 运行情况!可以通过该日志看到每个查询的list中每个对象数据。