一.写这篇文章的目的
本人用的Tomcat版本一直为Tomcat5.0.28(JDK1.5.0_09)最近,改用了Tomcat5.5之后,发现了一个奇怪的问题,logs目录下不能产生追踪Web应用程序的日志。到Apche上察看才知道原因:原来是为了根据开发者的癖好而进行的改变,当你需要进行追踪时,要自己进行Log4j.properties配置。具体位置:http://tomcat.apache.org/tomcat-5.5-doc/logging.html
二.文件下载
1.加载 log4j-1.2.8.jar
Log4j.jar下载地址:http://www.apache.org/dist/jakarta/log4j/jakarta-log4j-1.2.8.zip
log4e.jar下载地址:http://log4e.jayefem.de/index.php/Download
说明:log4e为Eclipse的log4j插件,放入插件文件夹后重起Eclipse,程序里右键即可看到Log4E选项。
2.在根路径配置log4j.properties,参照下面的log4j配置代码
3.在需要记录日志的java文件、jsp或servlet中添加下面的记录日志代码
三.基本说明
1.配置根Logger,其语法为:
log4j.rootLogger = [ level ] , appenderName, appenderName, ...
1)level 是日志记录的优先级
log4j中有五级logger
FATAL 0
ERROR 3
WARN 4
INFO 6
DEBUG 7
如果log4j.rootLogger=WARN, 则意味着只有WARN,ERROR,FATAL,被输出,DEBUG,INFO将被屏蔽掉.
2)appenderName就是指定日志信息输出到哪个地方。您可以同时指定多个输出目的地。
设置输出地appenderName为ConsoleAppender(控制台)
log4j.appender.appenderName=org.apache.log4j.ConsoleAppender
2.配置日志信息输出目的地Appender:
1)基本语法:
#RollingFileAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
......
#ConsoleAppender
log4j.appender.B1=org.apache.log4j.ConsoleAppender
......
2)Log4j提供的appender有以下几种:
# 应用于控制台: org.apache.log4j.ConsoleAppender,
# 应用于文件: org.apache.log4j.FileAppender,
# 应用于每天产生一个日志文件: org.apache.log4j.DailyRollingFileAppender,
(# 应用于socket: org.apache.log4j.RollingFileAppender),
# 应用于文件回滚: org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件),
# 应用于日志流格式: org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方),
# 应用于Log Factor 5 Appender: org.apache.log4j.lf5.LF5Appender,
# 应用于发送日志给邮件: org.apache.log4j.net.SMTPAppender,
# 应用于数据库: org.apache.log4j.jdbc.JDBCAppender
3.配置日志信息的格式(布局):
1)基本语法:
#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(包含日志信息的级别和信息字符串),
org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
4.配置文件大小:
log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.MaxBackupIndex=1
5.常用输出格式
1) -X号:X信息输出时左对齐;
2) %p:日志信息级别
3) %d{}:日志信息产生时间
4) %c:日志信息所在地(类名)
5) %m:产生的日志具体信息
6) %n:输出日志信息换行
如: log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n
对应Log文件: 2007-11-02 10:38:14,735 [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/sshfile]]-[INFO] Initializing Spring root WebApplicationContext
四.举例说明:
1基本例子:
log4j.rootLogger=INFO,R //配置根Logger,其优先级为INFO,指定到单一输出目的R中.
#RollingFileAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender //应用于文件回滚
log4j.appender.Threshold=INFO //配置文件回滚优先级为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 //配置输出形式,包括{日志日期} [所在位置]-[级别] 产生具体信息并换行
#ConsoleAppender
log4j.appender.B1=org.apache.log4j.ConsoleAppender //应用于控制台
log4j.appender.Threshold=DEBUG /配置文件回滚优先级为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关于Log4j比较全面的配置
LOG4J的配置之简单使它遍及于越来越多的应用中了:Log4J配置文件实现了输出到控制台、文件、回滚文件、发送日志邮件、输出到数据库日志表、自定义标签等全套功能。择其一二使用就够用了,
log4j.rootLogger=DEBUG,CONSOLE,A1,im
log4j.addivity.org.apache=true
# 应用于控制台
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
#应用于文件
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
# 用于每天产生一个日志文件
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
# 应用于文件回滚
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
#应用于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
# 发送日志给邮件
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
# 用于数据库
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配置
本文是对log4j.properties配置的总结,常用log4j配置,一般可以采用两种方式:.properties和.xml,对于.xml的配置,参考如下例子:
log4j.xml
<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="com.david.all" class="org.apache.log4j.RollingFileAppender">
<!-- 设置通道ID:com.david.all和输出方式:org.apache.log4j.RollingFileAppender -->
<param name="File" value="F:/Program Files/Java/Tomcat 5.5/logs/all.log" /><!-- 设置File参数:日志输出文件名 -->
<param name="Append" value="false" /><!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 -->
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p (%c:%L)- %m%n" /><!-- 设置输出文件项目和格式 -->
</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" /> <!-- 设置文件大小 -->
<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"> <!-- 设置域名限制,即david.log域及以下的日志均输出到下面对应的通道中 -->
<level value="debug" /><!-- 设置级别 -->
<appender-ref ref="com.david.sun" /><!-- 与前面的通道id相对应 -->
</logger>
<root> <!-- 设置接收所有输出的通道 -->
<appender-ref ref="com.david.all" /><!-- 与前面的通道id相对应 -->
</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.
|