log4j在tomcat 和 工程中的配置

按照網上很多配置步驟走都配不通。現在回頭看看大部分是版本不一致導致的。

另外log4j做的很靈活除了可以對整個容器進行日誌管理以外,還可以對一個工程進行日誌管理。如果只有容器裏面的日誌,那junit中的日誌怎麼辦呢,所以工程的日誌管理也很必要。

這裏給出一個保證能夠使用的配置文件。

在tomcat容器中配置:


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.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章