一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Log4j配置備忘及tomcat5.5日志配制方法

 9loong 2010-06-21
http://blog.csdn.net/l0979365428/archive/2008/10/16/3085152.aspx
 

.寫這篇文章的目的

       本人用的Tomcat版本一直為Tomcat5.0.28JDK1.5.0_09)最近,改用了Tomcat5.5之后,發(fā)現(xiàn)了一個(gè)奇怪的問(wèn)題,logs目錄下不能產(chǎn)生追蹤Web應(yīng)用程序的日志。到Apche上察看才知道原因:原來(lái)是為了根據(jù)開(kāi)發(fā)者的癖好而進(jìn)行的改變,當(dāng)你需要進(jìn)行追蹤時(shí),要自己進(jìn)行Log4j.properties配置。具體位置:http://tomcat./tomcat-5.5-doc/logging.html

.文件下載

1.加載 log4j-1.2.8.jar  

Log4j.jar下載地址:http://www./dist/jakarta/log4j/jakarta-log4j-1.2.8.zip

       log4e.jar下載地址:http://log4e./index.php/Download

       說(shuō)明:log4eEclipselog4j插件,放入插件文件夾后重起Eclipse,程序里右鍵即可看到Log4E選項(xiàng)。

2.在根路徑配置log4j.properties,參照下面的log4j配置代碼

3.在需要記錄日志的java文件、jsp或servlet中添加下面的記錄日志代碼

.基本說(shuō)明

1.配置根Logger,其語(yǔ)法為:

       log4j.rootLogger = [ level ] , appenderName, appenderName, ...

      1)level 是日志記錄的優(yōu)先級(jí)

       log4j中有五級(jí)logger

              FATAL 0

              ERROR 3

              WARN 4

              INFO 6

              DEBUG 7

       如果log4j.rootLogger=WARN, 則意味著只有WARN,ERROR,FATAL,被輸出,DEBUG,INFO將被屏蔽掉.

   2)appenderName就是指定日志信息輸出到哪個(gè)地方。您可以同時(shí)指定多個(gè)輸出目的地。

       設(shè)置輸出地appenderNameConsoleAppender(控制臺(tái))

       log4j.appender.appenderName=org.apache.log4j.ConsoleAppender

2.配置日志信息輸出目的地Appender:

   1)基本語(yǔ)法:

       #RollingFileAppender

       log4j.appender.R=org.apache.log4j.RollingFileAppender

              ......

       #ConsoleAppender

       log4j.appender.B1=org.apache.log4j.ConsoleAppender

              ......

      2)Log4j提供的appender有以下幾種:

       # 應(yīng)用于控制臺(tái): org.apache.log4j.ConsoleAppender,

       # 應(yīng)用于文件: org.apache.log4j.FileAppender,

       # 應(yīng)用于每天產(chǎn)生一個(gè)日志文件: org.apache.log4j.DailyRollingFileAppender,

       (# 應(yīng)用于socket: org.apache.log4j.RollingFileAppender),

       # 應(yīng)用于文件回滾: org.apache.log4j.RollingFileAppender(文件大小到達(dá)指定尺寸的時(shí)候產(chǎn)生一個(gè)新的文件),

       # 應(yīng)用于日志流格式: org.apache.log4j.WriterAppender將日志信息以流格式發(fā)送到任意指定的地方),

       # 應(yīng)用于Log Factor 5 Appender: org.apache.log4j.lf5.LF5Appender,

       # 應(yīng)用于發(fā)送日志給郵件: org.apache.log4j.net.SMTPAppender,

       # 應(yīng)用于數(shù)據(jù)庫(kù): org.apache.log4j.jdbc.JDBCAppender

3.配置日志信息的格式(布局):

1)基本語(yǔ)法:

       #RollingFileAppender

       log4j.appender.R.layout=org.apache.log4j.PatternLayout

       log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n

       ......

       #ConsoleAppender

       log4j.appender.B1.layout=org.apache.log4j.PatternLayout

       log4j.appender.B1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n

       ......

2)Log4j提供的layout有以下幾種:

       org.apache.log4j.HTMLLayout(以HTML表格形式布局),

       org.apache.log4j.PatternLayout(可以靈活地指定布局模式),

       org.apache.log4j.SimpleLayout(包含日志信息的級(jí)別和信息字符串),

       org.apache.log4j.TTCCLayout(包含日志產(chǎn)生的時(shí)間、線程、類別等等信息)

4.配置文件大小:

       log4j.appender.R.MaxFileSize=500KB

       log4j.appender.R.MaxBackupIndex=1

5.常用輸出格式

   1) -X號(hào):X信息輸出時(shí)左對(duì)齊;

       2) %p:日志信息級(jí)別

       3) %d{}:日志信息產(chǎn)生時(shí)間

       4) %c:日志信息所在地(類名)

       5) %m:產(chǎn)生的日志具體信息

       6) %n:輸出日志信息換行

: log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n

對(duì)應(yīng)Log文件: 2007-11-02 10:38:14,735 [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/sshfile]]-[INFO] Initializing Spring root WebApplicationContext

.舉例說(shuō)明:

1基本例子:

       log4j.rootLogger=INFO,R //配置根Logger,其優(yōu)先級(jí)為INFO,指定到單一輸出目的R.

       #RollingFileAppender          

       log4j.appender.R=org.apache.log4j.RollingFileAppender    //應(yīng)用于文件回滾

       log4j.appender.Threshold=INFO          //配置文件回滾優(yōu)先級(jí)為INFO

       log4j.appender.R.File=F:/Program Files/Java/Tomcat 5.5/logs/logs_tomcat.log    //日志輸出路徑

       log4j.appender.R.MaxFileSize=100KB     //配置文件大小

       log4j.appender.R.MaxBackupIndex=1

       log4j.appender.R.layout=org.apache.log4j.PatternLayout      //靈活地指定布局模式

       log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n //配置輸出形式,包括{日志日期} [所在位置]-[級(jí)別] 產(chǎn)生具體信息并換行

       #ConsoleAppender

       log4j.appender.B1=org.apache.log4j.ConsoleAppender       //應(yīng)用于控制臺(tái)

       log4j.appender.Threshold=DEBUG      /配置文件回滾優(yōu)先級(jí)為DEBUG

       log4j.appender.B1.layout=org.apache.log4j.PatternLayout   //靈活地指定布局模式

       log4j.appender.B1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n

       log4j.appender.B1.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

2關(guān)于Log4j比較全面的配置

       LOG4J的配置之簡(jiǎn)單使它遍及于越來(lái)越多的應(yīng)用中了:Log4J配置文件實(shí)現(xiàn)了輸出到控制臺(tái)、文件、回滾文件、發(fā)送日志郵件、輸出到數(shù)據(jù)庫(kù)日志表、自定義標(biāo)簽等全套功能。擇其一二使用就夠用了,

       log4j.rootLogger=DEBUG,CONSOLE,A1,im

       log4j.addivity.org.apache=true

       # 應(yīng)用于控制臺(tái)

       log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

       log4j.appender.Threshold=DEBUG

       log4j.appender.CONSOLE.Target=System.out

       log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

       log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

              #log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n

       #應(yīng)用于文件

       log4j.appender.FILE=org.apache.log4j.FileAppender

       log4j.appender.FILE.File=file.log

       log4j.appender.FILE.Append=false

       log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

       log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

       # 用于每天產(chǎn)生一個(gè)日志文件

       log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender

       log4j.appender.A1.File=SampleMessages.log4j

       log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j'

       log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout

       # Use this layout for LogFactor 5 analysis

       # 應(yīng)用于文件回滾

       log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender

       log4j.appender.ROLLING_FILE.Threshold=ERROR

       log4j.appender.ROLLING_FILE.File=rolling.log

       log4j.appender.ROLLING_FILE.Append=true

       log4j.appender.ROLLING_FILE.MaxFileSize=10KB

       log4j.appender.ROLLING_FILE.MaxBackupIndex=1

       log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout

       log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

       #應(yīng)用于socket

       log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender

       log4j.appender.SOCKET.RemoteHost=localhost

       log4j.appender.SOCKET.Port=5001

       log4j.appender.SOCKET.LocationInfo=true

       # Set up for Log Facter 5

       log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout

                     log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n

       # Log Factor 5 Appender

       log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender

       log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000

       # 發(fā)送日志給郵件

       log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender

       log4j.appender.MAIL.Threshold=FATAL

       log4j.appender.MAIL.BufferSize=10

       log4j.appender.MAIL.From=xxx@www.xxx.com

       log4j.appender.MAIL.SMTPHost=www.wusetu.com

       log4j.appender.MAIL.Subject=Log4J Message

       log4j.appender.MAIL.To=xxx@www.xxx.com

       log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout

       log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

       # 用于數(shù)據(jù)庫(kù)

       log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender

       log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test

       log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver

       log4j.appender.DATABASE.user=root

       log4j.appender.DATABASE.password=

       log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')

       log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout

       log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

       輸出到2000NT日志

Log4j壓縮包里的NTEventLogAppender.dll拷到WINNT"SYSTEM32目錄下

       log4j.logger.NTlog=FATAL, A8

       # APPENDER A8

       log4j.appender.A8=org.apache.log4j.nt.NTEventLogAppender

log4j.appender.A8.Source=JavaTest

       log4j.appender.A8.layout=org.apache.log4j.PatternLayout

log4j.appender.A8.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n

       #自定義Appender

       log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender

       log4j.appender.im.host = mail.cybercorlin.net

       log4j.appender.im.username = username

       log4j.appender.im.password = password

       log4j.appender.im.recipient = xxx@xxx.net

       log4j.appender.im.layout=org.apache.log4j.PatternLayout

       log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

.參考:

1 log4j.xml配置

       本文是對(duì)log4j.properties配置的總結(jié),常用log4j配置,一般可以采用兩種方式:.properties.xml,對(duì)于.xml的配置,參考如下例子:

       log4j.xml

       <?xml version="1.0" encoding="GB2312" ?>

       <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

       <log4j:configuration xmlns:log4j="http://jakarta./log4j/">

          <appender name="com.david.all" class="org.apache.log4j.RollingFileAppender">  

        <!-- 設(shè)置通道ID:com.david.all和輸出方式:org.apache.log4j.RollingFileAppender -->

               <param name="File" value="F:/Program Files/Java/Tomcat 5.5/logs/all.log" /><!-- 設(shè)置File參數(shù):日志輸出文件名 -->

               <param name="Append" value="false" /><!-- 設(shè)置是否在重新啟動(dòng)服務(wù)時(shí),在原有日志的基礎(chǔ)添加新日志 -->

               <param name="MaxBackupIndex" value="10" />

               <layout class="org.apache.log4j.PatternLayout">

                      <param name="ConversionPattern" value="%p (%c:%L)- %m%n" /><!-- 設(shè)置輸出文件項(xiàng)目和格式 -->

               </layout>

          </appender>

         

          <appender name="com.david.sun" class="org.apache.log4j.RollingFileAppender">

               <param name="File" value="F:/Program Files/Java/Tomcat 5.5/logs/david.log" />

               <param name="Append" value="true" />

                <param name="MaxFileSize" value="10240" /> <!-- 設(shè)置文件大小 -->

               <param name="MaxBackupIndex" value="10" />

               <layout class="org.apache.log4j.PatternLayout">

                      <param name="ConversionPattern" value="%p (%c:%L)- %m%n" />

               </layout>

          </appender>

         

          <logger name="david.log"> <!-- 設(shè)置域名限制,即david.log域及以下的日志均輸出到下面對(duì)應(yīng)的通道中 -->

               <level value="debug" /><!-- 設(shè)置級(jí)別 -->

               <appender-ref ref="com.david.sun" /><!-- 與前面的通道id相對(duì)應(yīng) -->

          </logger>

         

          <root> <!-- 設(shè)置接收所有輸出的通道 -->

               <appender-ref ref="com.david.all" /><!-- 與前面的通道id相對(duì)應(yīng) -->

          </root>

       </log4j:configuration>

2 Logging in Tomcat

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

         

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.

.tomcat5.5配制日志

 

在Tomcat 5.5和Tomcat 6.0中的配置基本一樣,只是相關(guān)文件放的目錄有所不同.

首先需要準(zhǔn)備的文件為:

1.log4j.jar , 下載地址 : http://www./dist/logging/log4j/1.2.15/apache-log4j-1.2.15.zip
2.Tomcat 5.5所需的 commons-logging.jar,下載地址: http://www./dist/commons/logging/binaries/commons-logging-1.1.zip

3.log4j配置文件:
log4j.properties
配置內(nèi)容為:

log4j.rootLogger=ERROR,R

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p]%t-%c-%m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/lizongbo.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=INFO,R,CONSOLE

#日志級(jí)別不能太低,如果配置為debug的話,輸出的日志信息太多,導(dǎo)致tomcat啟動(dòng)非常的慢.

4.Tomcat 6.0所需的juli替換文件:
http://www./dist/tomcat/tomcat-6/v6.0.14/bin/extras/tomcat-juli-adapters.jar
http://www./dist/tomcat/tomcat-6/v6.0.14/bin/extras/tomcat-juli.jar

在Tomcat6.0中,

將tomcat-juli-adapters.jar,log4j-1.2.15.jar,log4j.properties復(fù)制到D:\Java\ApacheTomcat6.0.14\lib下面.

將tomcat-juli.jar復(fù)制到D:\Java\apache-tomcat-6.0.14\bin\下面.

然后啟動(dòng)tomcat,就可以在D:\Java\apache-tomcat-6.0.14\logs下看到lizongbo.log了.

在 Tomcat5.5中,

將commons-logging-1.1.jar,log4j-1.2.15.jar,復(fù)制到D:\Java\apache-tomcat-5.5.25\common\lib下面.
將log4j.properties復(fù)制到D:\Java\apache-tomcat-5.5.25\common\classes下面.

然后啟動(dòng)tomcat,就可以在D:\Java\apache-tomcat-5.5.25\logs下看到lizongbo.log了.

 

轉(zhuǎn)自:

http://www./honeybee/articles/157736.html

http://leaderbird./diary,19026491.shtml

發(fā)表于 @ 2008年10月16日 12:33:00

 

(#)

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    丝袜破了有美女肉体免费观看 | 操白丝女孩在线观看免费高清| 国产三级不卡在线观看视频| 尤物久久91欧美人禽亚洲| 亚洲国产av在线观看一区| 国产成人一区二区三区久久| 伊人国产精选免费观看在线视频 | 亚洲国产精品肉丝袜久久| 日韩一区二区三区在线欧洲| 亚洲天堂一区在线播放| 久久精品国产亚洲熟女| 亚洲中文字幕剧情在线播放| 少妇淫真视频一区二区| 欧美人妻少妇精品久久性色| 国内外激情免费在线视频| 欧美激情视频一区二区三区| 国产情侣激情在线对白| 日本二区三区在线播放| 国产盗摄精品一区二区视频| 加勒比系列一区二区在线观看 | 91精品日本在线视频| 内射精品欧美一区二区三区久久久| 91人妻丝袜一区二区三区| 国产传媒一区二区三区| 久久本道综合色狠狠五月| 国产农村妇女成人精品| 日韩高清毛片免费观看| 精品国产日韩一区三区| 国内午夜精品视频在线观看| 亚洲精品一二三区不卡| 亚洲av专区在线观看| 日本人妻中出在线观看| 欧美三级大黄片免费看| 欧美大粗爽一区二区三区| 日韩欧美一区二区黄色| 精品人妻一区二区四区| 日本黄色美女日本黄色| 国产精品亚洲综合天堂夜夜| 肥白女人日韩中文视频| 污污黄黄的成年亚洲毛片| 久久本道综合色狠狠五月|