1. web.xml
<!-- 加载Log4J 配置文件 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/conf/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>
log4jConfigLocation 配置log4j.properties 文件的位置。也可以这样配置 classpath:log4j.properties
2. 日志文件的位置
1)绝对路径
log4j.appender.R.File=D:/log/xxx.log
2)相对路径:
log4j.appender.R.File=../logs/app.log,将日志记录到tomcat下的logs文件夹;
log4j.appender.R.File=logs/app.log,将日志记录到tomcat的bin目录下的logs文件夹;
(3)使用环境变量相对路径法:程序会优先找jvm环境变量,然后再找系统环境变量,来查找配置文件中的变量。
log4j.appender.logfile.File=${user.dir}/logs/app.log,使用tomcat容器时${user.dir}对应tomcat的bin目录;
log4j.appender.logfile.File=${user.home}/logs/app.log,${user.home}对应操作系统当前用户目录;
log4j.appender.logfile.File=${webApp.root}/logs/app.log,${webApp.root}对应当前应用根目录;
3. 不同框架的日志分开存储:
#all logger output level is 'ERROR' and output position is stdout
#so only write our project's DEBUG log and ERROR log of the others
log4j.rootLogger=ERROR, stdout, R
#log4j.appender is set output postion
#log4j.logger is set some package(some package your want output logger specially)=[output level],[...output position]
log4j.logger.org.springframework=OFF
log4j.logger.org.apache.commons=OFF
log4j.logger.org.apache.struts=OFF
log4j.logger.org.hibernate=OFF
#log4j.logger.org.logicalcobwebs=OFF
#only write our project's log(DEBUG)
log4j.logger.com.mycompany=DEBUG
log4j.logger.org.apache.commons.dbcp=DEBUG, dbcp
#log4j.logger.org.logicalcobwebs.proxool=DEBUG, proxool
#log4j.logger.org.springframework=DEBUG, spring
log4j.logger.org.apache.catalina.core=DEBUG, catalina
#in the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#config stdout position output contain INFO level and up
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %l:%t - %m %n
#in the daily files
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.Threshold=DEBUG
log4j.appender.R.File=D:/EMR/log/EMR.log
log4j.appender.R.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%-5p] %l:%t - %m %n
log4j.logger.com.statestr=DEBUG
log4j.appender.dbcp=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dbcp.Threshold=DEBUG
log4j.appender.dbcp.File=D:/EMR/log/dbcp.log
log4j.appender.dbcp.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.dbcp.layout=org.apache.log4j.PatternLayout
log4j.appender.dbcp.layout.ConversionPattern=%d [%-5p] %l:%t - %m %n
log4j.appender.proxool=org.apache.log4j.DailyRollingFileAppender
log4j.appender.proxool.Threshold=DEBUG
log4j.appender.proxool.File=D:/EMR/log/proxool.log
log4j.appender.proxool.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.proxool.layout=org.apache.log4j.PatternLayout
log4j.appender.proxool.layout.ConversionPattern=%d [%-5p] %l:%t - %m %n
log4j.appender.spring=org.apache.log4j.DailyRollingFileAppender
log4j.appender.spring.Threshold=DEBUG
log4j.appender.spring.File=D:/EMR/log/spring.log
log4j.appender.spring.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.spring.layout=org.apache.log4j.PatternLayout
log4j.appender.spring.layout.ConversionPattern=%d [%-5p] %l:%t - %m %n
log4j.appender.catalina=org.apache.log4j.DailyRollingFileAppender
log4j.appender.catalina.Threshold=DEBUG
log4j.appender.catalina.File=D:/EMR/log/catalina.log
log4j.appender.catalina.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.catalina.layout=org.apache.log4j.PatternLayout
log4j.appender.catalina.layout.ConversionPattern=%d [%-5p] %l:%t - %m %n
4. 关闭日志
log4j.logger.org.springframework=OFF
log4j.logger.org.apache.commons=OFF
log4j.logger.org.apache.struts=OFF
log4j.logger.org.hibernate=OFF或者
log4j.logger.org.springframework=WARN
log4j.logger.org.apache.commons=WARN
log4j.logger.org.apache.struts=WARN
log4j.logger.org.hibernate=WARN