cuiyi's blog(崔毅 crazycy)

          記錄點滴 鑒往事之得失 以資于發展
          數據加載中……

          讓你的Tomcat5.5.*以上的版本如同Tomcat5.0.27版本一樣,記錄應用程序的日志

          用Spring、Hibernate、Struts之類的開發框架時,往往應用在Web Application應用中。在應用中我們常常并不使用、起碼在最開始開發時很少使用非常合理的異常也不用catch(Exception) catch(Throwable)之類的,因為稍有些矯枉過正之嫌。

          那么界面(web頁面)往往提示你產生了某些異常之流,或許已經足夠供你解決問題;或許你還想知道的更詳細;更甚是提示的錯誤讓你丈二和尚摸不著頭腦,怎么辦呢?別忘記:Tomcat在logs目錄中給你準備了十分充足的信息記錄,有些時候真的讓你欣喜不已。在我blog文章中已經詳述并證實了這個事實。(http://www.aygfsteel.com/crazycy/archive/2006/07/07/57214.html)

          既然Tomcat的日志功能這么強大,我突然棄用了Tomcat5.0.*系列(5.0.*系列的好處就是:既可以支持JDK1.4也可以支持JDK5.0。但是對5.0的支持需要配置,參考我blog文章http://www.aygfsteel.com/crazycy/archive/2006/05/31/49225.html? 和http://www.aygfsteel.com/crazycy/archive/2006/06/03/50150.aspx)。

          改用了Tomcat5.5之后,奇怪了,日志空空。在我的項目中我用的是改裝的Tomcat5.0.28(JDK1.5.0_07),我的一個朋友用的是Tomcat5.5.12。在他遇到問題:Servlet action is not available? (http://www.aygfsteel.com/crazycy/archive/2006/07/07/57214.html?第1,2點)時,我陳述了我的解決方法,他似乎不很能接受,因而我讓他把Tomcat的最新類似localhost.2006-07-16.log的文件發給我,結果我也傻眼了,空空如也,失去十足的論據,雖然確實解決了問題。

          說了這么多無非想闡述2點:
          1 Tomcat的log很有用,建議出了界面提示的不足以解決問題的錯誤時常察看;
          2 Tomcat5.5版本以上的沒有原先的log功能

          那如何解決呢:apache給出了明確的方案:原來是為了給開發者更多的個人癖好的選擇;比較人性化;但是委屈了我們這些新手;從這個事實也說明察看源網站聲明的重要性。具體位置:http://tomcat.apache.org/tomcat-5.5-doc/logging.html
          在這里引用一下:

          Logging in Tomcat

          Printer Friendly Version
          print-friendly
          version
          Introduction

          Tomcat 5.5 uses Commons Logging throughout its internal code allowing the developer to choose a logging configuration that suits their needs, e.g java.util.logging or Log4J. Commons Logging provides Tomcat the ability to log hierarchially across various log levels without needing to rely on a particular logging implementation.

          An important consequence for Tomcat 5.5 is that the <Logger> element found in previous versions to create a localhost_log is no longer a valid nested element of <Context>. Instead, the default Tomcat configuration will use java.util.logging. If the developer wishes to collect detailed internal Tomcat logging (i.e what is happening within the Tomcat engine), then they should configure a logging system such as java.util.logging or log4j as detailed next.

          log4j

          Tomcat 5.5 has done away with localhost_log which you may be familiar with as the runtime exception/stack trace log. These types of error are usually thrown by uncaught exceptions, but are still valuable to the developer. They can now be found in the stdout log.

          If you need to setup cross-context detailed logging from within Tomcat's code, then you can use a simple log4j configuration. Note that this logging van be very verbose depending on the log level you chose to use. Note also that a log4j logging configuration is not going to produce stack trace type logging: those stack traces are output to stdout as discussed above.

          Follow the following steps to setup a file named tomcat.log that has internal Tomcat logging output to it:

          1. Create a file called log4j.properties with the following content and save it into common/classes.
                        log4j.rootLogger=debug, R 
                        log4j.appender.R=org.apache.log4j.RollingFileAppender 
                        log4j.appender.R.File=${catalina.home}/logs/tomcat.log 
                        log4j.appender.R.MaxFileSize=10MB 
                        log4j.appender.R.MaxBackupIndex=10 
                        log4j.appender.R.layout=org.apache.log4j.PatternLayout 
                        log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 
                        log4j.logger.org.apache.catalina=DEBUG, R
                      
          2. Download Log4J (v1.2 or later) and place the log4j jar in $CATALINA_HOME/common/lib.
          3. Download Commons Logging and place the commons-logging.jar (not commons-logging-api.jar) in $CATALINA_HOME/common/lib with the log4j jar.
          4. Start Tomcat

          This log4j configuration sets up a file called tomcat.log in your Tomcat logs folder with a maximum file size of 10MB and up to 10 backups. DEBUG level is specified which will result in the most verbose output from Tomcat.

          You can (and should) be more picky about which packages to include in the logging. Tomcat 5.5 uses defines loggers by Engine and Host names. For example, for a default Catalina localhost log, add this to the end of the log4j.properties above. Note that there are known issues with using this naming convention (with square brackets) in log4j XML based configuration files, so we recommend you use a properties file as described until a future version of log4j allows this convention.

          • log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
          • log4j.logger.org.apache.catalina.core=DEBUG, R
          • log4j.logger.org.apache.catalina.session=DEBUG, R
          Be warned a level of DEBUG will produce megabytes of logging and slow startup of Tomcat. This level should be used sparingly when debugging of internal Tomcat operations is required.

          Your web applications should certainly use their own log4j configuration. This is valid with the above configuration. You would place a similar log4j.properties file in your web application's WEB-INF/classes folder, and log4j1.2.8.jar into WEB-INF/lib. Then specify your package level logging. This is a basic setup of log4j which does *not* require Commons-Logging, and you should consult the log4j documentation for more options. This page is intended only as a bootstrapping guide.


          我用的依然是Log 4J,幾點建議就是:
          1
          log4j.rootLogger=debug, R
          log4j.logger.org.apache.catalina=DEBUG, R
          中的debug, DEBUG根據你的記錄級別調整,我喜歡用error ERROR
          2
          log4j.appender.R.File=${catalina.home}/logs/tomcat.log
          這個地方不需要人為修改
          3
          建議在系統環境變量增加:CATALINA_HOME一項配置以下Tomcat的安裝目錄


          最后就是希望遇到這個問題的朋友,能及時的看到這個文章或者從Tomcat網站上得到幫助,盡量減少這些問題上消耗過多時間和精力。把苦悶留給最開始遇到問題的人吧,然后大家充分share之。

          posted on 2006-07-22 15:45 crazycy 閱讀(2279) 評論(1)  編輯  收藏 所屬分類: JavaEE技術

          評論

          # re: 讓你的Tomcat5.5.*以上的版本如同Tomcat5.0.27版本一樣,記錄應用程序的日志  回復  更多評論   

          這樣配置得來的是一些內部處理細節的日志。如何記錄console上顯示的內容?
          2007-06-13 15:35 | fbysss
          主站蜘蛛池模板: 信丰县| 皋兰县| 交城县| 衡阳县| 青州市| 鄂托克旗| 吉木萨尔县| 收藏| 邵武市| 砀山县| 甘孜县| 温宿县| 手游| 囊谦县| 伊春市| 锦州市| 黄浦区| 东阿县| 荥阳市| 姜堰市| 绥中县| 天水市| 甘孜县| 齐河县| 临安市| 扎囊县| 荣成市| 九台市| 望谟县| 海安县| 富宁县| 九江县| 时尚| 淮阳县| 乌鲁木齐市| 广饶县| 增城市| 丁青县| 赞皇县| 涞源县| 建平县|