http://blog.csdn.net/l0979365428/archive/2008/10/16/3085152.aspx
一.寫這篇文章的目的本人用的Tomcat版本一直為Tomcat5.0.28(JDK1.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.jarLog4j.jar下載地址:http://www./dist/jakarta/log4j/jakarta-log4j-1.2.8.zip log4e.jar下載地址:http://log4e./index.php/Download 說(shuō)明:log4e為Eclipse的log4j插件,放入插件文件夾后重起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è)置輸出地appenderName為ConsoleAppender(控制臺(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
六.tomcat5.5配制日志
在Tomcat 5.5和Tomcat 6.0中的配置基本一樣,只是相關(guān)文件放的目錄有所不同.
轉(zhuǎn)自: http://www./honeybee/articles/157736.html http://leaderbird./diary,19026491.shtml 發(fā)表于 @ 2008年10月16日 12:33:00
(#) |
|