1 在web.xml中,配置如下
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- 定义LOG4J监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
2 创建 log4j.properties,内容类似下面
log4j.rootLogger=DEBUG,DAILY_FILE
log4j.logger.simple=DEBUG,SIMPLE_CONSOLE
log4j.logger.MyApp=INFO,SIMPLE_CONSOLE,DAILY_FILE
log4j.appender.SIMPLE_CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.SIMPLE_CONSOLE.Target=System.out
log4j.appender.SIMPLE_CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.SIMPLE_CONSOLE.layout.ConversionPattern=[LOG4J-%-3p] %d{HH:mm:ss.SSS} [%t] %m%n
log4j.appender.SIMPLE_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.SIMPLE_FILE.file=MyApp.log
log4j.appender.SIMPLE_FILE.MaxFileSize= 1000KB
log4j.appender.SIMPLE_FILE.Append=true
log4j.appender.SIMPLE_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.SIMPLE_FILE.layout.ConversionPattern=[LOG4J-%-3p] %d{HH:mm:ss.SSS} [%t] %m%n
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.appender.DAILY_FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DAILY_FILE.file=MyApp
log4j.appender.DAILY_FILE.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.DAILY_FILE.Append=true
log4j.appender.DAILY_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.DAILY_FILE.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%-5p] %c{1} %-10r [%t] %m%n
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.file=MyApp.log
log4j.appender.FILE.MaxFileSize= 1000KB
log4j.appender.FILE.Append=true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%-5p] %c{1} %-10r [%t] %m%n
3 dao中
org.springframework.dao.support.DaoSupport.logger.debug("StudentDao:find错误!", e);