log4j在tomcat 和 工程中的配置

按照网上很多配置步骤走都配不通。现在回头看看大部分是版本不一致导致的。

另外log4j做的很灵活除了可以对整个容器进行日志管理以外,还可以对一个工程进行日志管理。如果只有容器里面的日志,那junit中的日志怎么办呢,所以工程的日志管理也很必要。

这里给出一个保证能够使用的配置文件。

og4j1.3在tomcat5中的配置:
  1. 关闭tomcat
  2. 下载 Commons Logging 包。
  3. commons-logging.jar拷贝到Tomcat common/lib目录下。
  4. 下载Log4j 1.3的包。
  5. 拷贝log4j-1.3alpha-8.jar到Tomcat common/lib 目录下。
  6. 在Tomcat common/classes目录下建一个log4j.properties文件。
  7. 重启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

在工程中配置log4j1.3:

在工程的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的,这样做能收集到所有的日志。


参考:

http://minaret.biz/tips/tomcatLogging

apache-log4j-1.2.15.jar, apache-log4j-extras-1.0.jar, apache-log4j-extras-1.1.jar, apache-log4j.jar, log4j-1.2-api-2.0.2-javadoc.jar, log4j-1.2-api-2.0.2-sources.jar, log4j-1.2-api-2.0.2.jar, log4j-1.2.11.jar, log4j-1.2.12.jar, log4j-1.2.13-sources.jar, log4j-1.2.13.jar, log4j-1.2.13.src.jar, log4j-1.2.14-sources.jar, log4j-1.2.14.jar, log4j-1.2.15-sources.jar, log4j-1.2.15.jar, log4j-1.2.16-sources.jar, log4j-1.2.16.jar, log4j-1.2.5.jar, log4j-1.2.6.jar, log4j-1.2.7.jar, log4j-1.2.8.jar, log4j-1.2.9.jar, log4j-1.2.91.jar, log4j-1.2.jar, log4j-1.2rc1.jar, log4j-1.3alpha-7.jar, log4j-api-2.0.2-javadoc.jar, log4j-api-2.0.2-sources.jar, log4j-api-2.0.2.jar, log4j-boot.jar, log4j-bridge-0.9-sources.jar, log4j-bridge-0.9-tests.jar, log4j-bridge-0.9.4-sources.jar, log4j-bridge-0.9.4-tests.jar, log4j-bridge-0.9.4.jar, log4j-bridge-0.9.6-sources.jar, log4j-bridge-0.9.6-tests.jar, log4j-bridge-0.9.6.jar, log4j-bridge-0.9.jar, log4j-config.jar, log4j-core-2.0.2-javadoc.jar, log4j-core-2.0.2-sources.jar, log4j-core-2.0.2-tests.jar, log4j-core-2.0.2.jar, log4j-core.jar, log4j-ext.jar, log4j-flume-ng-2.0.2-javadoc.jar, log4j-flume-ng-2.0.2-sources.jar, log4j-flume-ng-2.0.2.jar, log4j-java1.1.jar, log4j-jcl-2.0.2-javadoc.jar, log4j-jcl-2.0.2-sources.jar, log4j-jcl-2.0.2.jar, log4j-jmx-gui-2.0.2-javadoc.jar, log4j-jmx-gui-2.0.2-sources.jar, log4j-jmx-gui-2.0.2.jar, log4j-nosql-2.0.2-javadoc.jar, log4j-nosql-2.0.2-sources.jar, log4j-nosql-2.0.2.jar, log4j-over-slf4j-1.5.0.jar, log4j-over-slf4j-1.5.10.jar, log4j-over-slf4j-1.5.3.jar, log4j-over-slf4j-1.5.5.jar, log4j-over-slf4j-1.5.6-sources.jar, log4j-over-slf4j-1.5.6.jar, log4j-over-slf4j-1.5.8-sources.jar, log4j-over-slf4j-1.5.8.jar, log4j-over-slf4j-1.6.1.jar, log4j-slf4j-impl-2.0.2-javadoc.jar, log4j-slf4j-impl-2.0.2-sources.jar, log4j-slf4j-impl-2.0.2.jar, log4j-snmp-appender.jar, log4j-taglib-2.0.2-javadoc.jar, log4j-taglib-2.0.2-sources.jar, log4j-taglib-2.0.2.jar, log4j-to-slf4j-2.0.2-javadoc.jar, log4j-to-slf4j-2.0.2-sources.jar, log4j-to-slf4j-2.0.2.jar, log4j-web-2.0.2-javadoc.jar, log4j-web-2.0.2-sources.jar, log4j-web-2.0.2.jar, log4j-wjw.jar, log4j-xml-1.3alpha-7.jar, log4j.jar, org.apache.log4j_1.2.15.v201005080500.jar, org.apache.log4j_1.2.15.v201012070815.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值