tomcat改用log4j

來源:互聯網
上載者:User
tomcat日誌改用log4j

昨天同事那邊出了點故障, 一台tomcat伺服器訪問出錯,tomcat是6.0版本的, 然後想查看tomcat日誌卻怎麼也找不到最近的日誌,logs下面 只有08年的幾個記錄檔。同事感覺很暈, 我也覺著奇怪, 照理說, tomcat預設情況下也是會組建記錄檔檔案的, 這回怎麼就平白無故的消失了呢?

到網上找了下相關的資訊, 網上大部分都是說的如何讓tomcat使用log4j。我想tomcat預設用的是jdklogger,如果能換用log4j也不錯, 只要日誌能產生出檔案來。

於是在我的本機環境的tomcat5.5和tomcat6下分別作了實驗。

結果,發現tomcat5.5和6在配置log4j的時候還存在差異。

1)tomcat5.5

把 commons-logging和log4j的jar包分別copy到common/lib下面, 再copy一份log4j.properties和commons-logging.properties到common/classes之下, 

log4j.properties,配置可以如下:

 寫道

log4j.rootLogger=INFO,stdout,file

## direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %l - %m%n

## direct messages to file SystemOut.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=../logs/SystemOut.log
log4j.appender.file.Append=false
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %l - %m%n

## block packages that you don't care about ###
log4j.logger.org.apache.commons.digester=ERROR
log4j.logger.org.apache.commons.beanutils=ERROR
log4j.logger.org.apache.commons.modeler=ERROR

 commons-logging.properties,配置可以如下:

 

 寫道org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

#Please refer to the Log implementations in package org.apache.commons.logging.impl#
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog

 

重啟tomcat之後,就能在logs/下面找到記錄檔了。

 

 

2)tomcat6

同樣的, 也要複製commons-logging和log4j的jar包到tomcat, 不過目標目錄不同,位於:${tomcat_home}/lib/;

commons-logging.properties和log4j.properties需要複製到${tomcat_home}/lib/目錄下;

 

額外的,還需要到http://www.apache.org/dist/tomcat/tomcat-6/v6.0.18/bin/extras/ 

下載兩個包:tomcat-juli.jar,tomcat-juli-adapters.jar 

tomcat-juli.jar複製到bin下面,替換原有的同名jar包;tomcat-juli-adapters.jar 放到${tomcat_home}/lib/

 

完成以上步驟之後, 重啟tomcat,就能發現已經有了log4j的記錄檔產生。

 

 

參考資料:

http://tomcat.apache.org/tomcat-5.5-doc/logging.html

http://tomcat.apache.org/tomcat-6.0-doc/logging.html

http://www.blogjava.net/Unmi/archive/2008/07/07/213001.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.