目錄
Tomcat 日志資訊分為兩類:
運作中的日志,它主要記錄運作的一些資訊,尤其是一些異常錯誤日志資訊。
通路日志資訊,它記錄的通路的時間,IP,通路的資料等相關資訊。
啟用通路日志
預設 tomcat 不記錄通路日志,如下方法可以使 tomcat 記錄通路日志
編輯${catalina}/conf/server.xml 檔案(注:{catalina}是tomcat的安裝目錄),把以下的注釋 (<!-- -->) 去掉即可。
<code><!--</code>
<code> </code><code><Valve className="org.apache.catalina.valves.AccessLogValve"</code>
<code> </code><code>directory="logs" prefix="localhost_access_log." suffix=".txt"</code>
<code> </code><code>pattern="common" resolveHosts="false"/></code>
<code> </code><code>--></code>
配置tomcat寫出更詳細的日志
通過對 2.1 示例中 pattern 項的修改,可以改變日志輸出的内容。
該項值可以為: common 與 combined ,這兩個預先設定好的格式對應的日志輸出内容如下:
common 的值: <code>%h %l %u %t %r %s %b</code>
combined 的值: <code>%h %l %u %t %r %s %b %{Referer}i %{User-Agent}i</code>
pattern 也可以根據需要自由組合 , 例如 <code>pattern="%h %l"</code>
對 于各 fields 字段的含義請參照:
%a - Remote IP address
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if zero
%B - Bytes sent, excluding HTTP headers
%h - Remote host name (or IP address if resolveHosts is false)
%H - Request protocol
%l - Remote logical username from identd (always returns '-')
%m - Request method (GET, POST, etc.)
%p - Local port on which this request was received
%q - Query string (prepended with a '?' if it exists)
%r - First line of the request (method and request URI)
%s - HTTP status code of the response
%S - User session ID
%t - Date and time, in Common Log Format
%u - Remote user that was authenticated (if any), else '-'
%U - Requested URL path
%v - Local server name
%D - Time taken to process the request, in millis
%T - Time taken to process the request, in seconds
%I - current request thread name (can compare later with stacktraces)
%{xxx}i for incoming headers
%{xxx}o for outgoing response headers
%{xxx}c for a specific cookie
%{xxx}r xxx is an attribute in the ServletRequest
%{xxx}s xxx is an attribute in the HttpSession
Tomcat日志分為5類:<code>catalina 、localhost 、manager 、admin 、host-manager</code>
日志的級别分為7種:<code>SEVERE (highest value) > WARNING > INFO > CONFIG > FINE > FINER > FINEST (lowest value)</code>
修改 conf/logging.properties 中的内容,設定某類日志的級别
示例:
設定 catalina 日志的級别為: FINE
禁用 catalina 日志的輸出:
輸出 catalina 所有的日志消息均輸出:
使用Log4j輸出詳細系統日志資訊,快速診斷啟動故障
此例可彌補tomcat啟動異常時輸出的錯誤資訊不足的問題,使用commons-logging和log4j搭配輸出詳盡的日志資訊。
以window環境下tomcat5.5.27為例:
tomcat解壓目錄為:E: /tomcat5.5
設定環境變量:CATALINA_HOME=E: /tomcat5.5
下載下傳 log4j 與 commons-logging
本例将 commons-logging-1.1.1.jar 與 log4j-1.2.15.jar 放在 %TOMCAT_HOME%/bin 目錄下(可根據需要放置在其位置)
在 %TOMCAT_HOME%/bin 目錄下建立兩個檔案 commons-logging.properties 、log4j.properties
commons-logging.properties 檔案内容如下:
<code>org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger</code>
log4j.properties 檔案内容如下:
<code>log4j.rootLogger=WARN,stdout,</code><code>file</code>
<code>## 日志直接輸出到控制台 ###</code>
<code>log4j.appender.stdout=org.apache.log4j.ConsoleAppender</code>
<code>log4j.appender.stdout.Target=System.out</code>
<code>log4j.appender.stdout.layout=org.apache.log4j.PatternLayout</code>
<code>log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %l - %m%n</code>
<code>## 日志輸出到檔案 SystemOut.log ###</code>
<code>log4j.appender.</code><code>file</code><code>=org.apache.log4j.FileAppender</code>
<code>log4j.appender.</code><code>file</code><code>.File=E:</code><code>/tomcat5</code><code>.5/ logs</code><code>/SystemOut</code><code>.log</code>
<code>log4j.appender.</code><code>file</code><code>.Append=</code><code>false</code>
<code>log4j.appender.</code><code>file</code><code>.layout=org.apache.log4j.PatternLayout</code>
<code>log4j.appender.</code><code>file</code><code>.layout.ConversionPattern=%d{ABSOLUTE} %l - %m%n</code>
修改 catalina.bat 檔案
通過startup.bat啟動就會用log4j來輸出啟動日志了。
在E:/tomcat5.5/logs/SystemOut.log檔案中檢視輸出的日志
建立Java工程。
添加log4j.jar到工程的編譯路徑下。
建立名稱為log4j.properties的檔案,寫入如下内容:
<code>### direct log messages to stdout ###</code>
<code>log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n</code>
<code>log4j.rootLogger=debug, stdout</code>
建立類并添加如下内容:
<code>import</code> <code>org.apache.log4j.Logger;</code>
<code>public class LogClass {</code>
<code> </code><code>private static org.apache.log4j.Logger log = Logger</code>
<code> </code><code>.getLogger (LogClass. class );</code>
<code> </code><code>public static void main(String[] args) {</code>
<code> </code><code>log .trace(</code><code>"Trace"</code> <code>);</code>
<code> </code><code>log .debug(</code><code>"Debug"</code> <code>);</code>
<code> </code><code>log .info(</code><code>"Info"</code> <code>);</code>
<code> </code><code>log .warn(</code><code>"Warn"</code> <code>);</code>
<code> </code><code>log .error(</code><code>"Error"</code> <code>);</code>
<code> </code><code>log .fatal(</code><code>"Fatal"</code> <code>);</code>
<code> </code><code>}</code>
<code>}</code>
編譯運作,可在控制台中看到如下内容:
根據級别控制日志輸出内容:
log4j.rootCategory=INFO, stdout , R
此句為将等級為INFO的日志資訊輸出到stdout和R這兩個目的地。
等級可分為OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL,如果配置OFF則不打出任何資訊,如果配置為INFO這樣隻顯示INFO,WARN,ERROR的log資訊,而DEBUG資訊不會被顯示。
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
此句為定義名為stdout的輸出端是哪種類型,可以是:
org.apache.log4j.FileAppender
檔案
org.apache.log4j.DailyRollingFileAppender
每天産生一個日志檔案
org.apache.log4j.RollingFileAppender
檔案大小到達指定尺寸的時候産生一個新的檔案
org.apache.log4j.WriterAppender
将日志資訊以流格式發送到任意指定的地方
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
此句為定義名為stdout的輸出端的layout是哪種類型
org.apache.log4j.HTMLLayout
以HTML表格形式布局
org.apache.log4j.PatternLayout
可以靈活地指定布局模式
org.apache.log4j.SimpleLayout
包含日志資訊的級别和資訊字元串
org.apache.log4j.TTCCLayout
包含日志産生的時間、線程、類别等等資訊
log4j.appender.stdout.layout.ConversionPattern= [QC] %p [%t] %C.%M(%L) | %m%n
如果使用 pattern 布局就要指定的列印資訊的具體格式 ConversionPattern ,列印參數如下:
%m
輸出代碼中指定的消息
%p
輸出優先級,即DEBUG,INFO,WARN,ERROR,FATAL
%r
輸出自應用啟動到輸出該log資訊耗費的毫秒數
%c
輸出所屬的類目,通常就是所在類的全名
%t
輸出産生該日志事件的線程名
%n
輸出一個回車換行符,Windows平台為“rn”,Unix平台為“n”
%d
輸出日志時間點的日期或時間,預設格式為ISO8601,也可以指定格式,如:<code>%d{yyyymmddHH:mm:ss,SSS}</code>,輸出:2002101822:10:28,921
%l
輸出日志事件的發生位置,包括類目名、發生的線程,以及在代碼中的行數。
[QC]
是log資訊的開頭,可以為任意字元,一般為項目簡稱。
本文轉自 妙曼 51CTO部落格,原文連結:http://blog.51cto.com/yanruohan/1608748,如需轉載請自行聯系原作者