另外log4j做的很灵活除了可以对整个容器进行日志管理以外,还可以对一个工程进行日志管理。如果只有容器里面的日志,那junit中的日志怎么办呢,所以工程的日志管理也很必要。
这里给出一个保证能够使用的配置文件。
og4j1.3在tomcat5中的配置:
- 关闭tomcat
- 下载 Commons Logging 包。
- 把
commons-logging.jar
拷贝到Tomcatcommon/lib
目录下。 - 下载Log4j 1.3的包。
- 拷贝log4j-1.3alpha-8.jar到Tomcat
common/lib
目录下。 - 在Tomcat
common/classes
目录下建一个log4j.properties
文件。 - 重启Tomcat.
Log4j Properties File
#
# Configures Log4j as the Tomcat system logger
#
#
# Configure the logger to output info level messages into a rolling log file.
#
log4j.rootLogger=INFO, R, CONSOLE
#
# To continue using the "catalina.out" file (which grows forever),
# comment out the above line and uncomment the next.
#
#log4j.rootLogger=ERROR, A1
#
# Configuration for standard output ("catalina.out").
#
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#
# Print the date in ISO 8601 format
#
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#
# Configuration for a rolling log file ("tomcat.log").
#
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern='.'yyyy-MM-dd
#
# Edit the next line to point to your logs directory.
# The last part of the name is the log file name.
#
log4j.appender.R.File=D:/tomcat/logs/tomcat.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
#
# Print the date in ISO 8601 format
#
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
####################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
#
# Edit the next line to point to your logs directory.
# The last part of the name is the log file name.
#
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#
# Print the date in ISO 8601 format
#
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#
# Application logging options
#
#log4j.logger.org.apache=DEBUG
#log4j.logger.org.apache=INFO
#log4j.logger.org.apache.struts=DEBUG
#log4j.logger.org.apache.struts=INFO
在工程的src目录下新建
log4j.properties
文件如下:
#
# Configures Log4j as the Tomcat system logger
#
#
# Configure the logger to output info level messages into a rolling log file.
#
log4j.rootLogger=info, CONSOLE
#
# To continue using the "catalina.out" file (which grows forever),
# comment out the above line and uncomment the next.
#
#log4j.rootLogger=ERROR, A1
#
# Configuration for standard output ("catalina.out").
#
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#
# Print the date in ISO 8601 format
#
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
####################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
#
# Edit the next line to point to your logs directory.
# The last part of the name is the log file name.
#
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#
# Print the date in ISO 8601 format
#
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#
# Application logging options
#
#log4j.logger.org.apache=DEBUG
#log4j.logger.org.apache=INFO
#log4j.logger.org.apache.struts=DEBUG
#log4j.logger.org.apache.struts=INFO
然后就OK了。我的工程是struts spring hibernate的,这样做能收集到所有的日志。
参考: