Chinaunix首页 | 论坛 | 博客
  • 博客访问: 156196
  • 博文数量: 23
  • 博客积分: 390
  • 博客等级: 一等列兵
  • 技术积分: 157
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-13 16:25
文章分类

全部博文(23)

文章存档

2013年(4)

2012年(19)

我的朋友

分类: 系统运维

2012-08-13 15:07:28

.写这篇文章的目的

       本人用的Tomcat版本一直为Tomcat5.0.28JDK1.5.0_09)最近,改用了Tomcat5.5之后,发现了一个奇怪的问题,logs目录下不能产生追踪Web应用程序的日志。到Apche上察看才知道原因:原来是为了根据开发者的癖好而进行的改变,当你需要进行追踪时,要自己进行Log4j.properties配置。具体位置:

.文件下载1.加载 log4j-1.2.8.jar  

Log4j.jar下载地址:

       log4e.jar下载地址:

       说明:log4eEclipselog4j插件,放入插件文件夹后重起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就是指定日志信息输出到哪个地方。您可以同时指定多个输出目的地。

       设置输出地appenderNameConsoleAppender(控制台)

       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] 7c %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:输出日志信息换行
博主新加的:
%-5p:-表示左对齐,占5个字符,不足用空格代替。

: 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] 7c %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@

       log4j.appender.MAIL.SMTPHost=

       log4j.appender.MAIL.Subject=Log4J Message

       log4j.appender.MAIL.To=xxx@

       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] 7c %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

      

      

      

            

        

               

               

               

               

                      

               

         

         

         

               

               

                

               

               

                      

               

         

         

         

               

               

         

         

         

               

         

      

2 Logging in Tomcat

Tomcat 5.5 uses  throughout its internal code allowing the developer to choose a logging configuration that suits their needs, e.g java.util.logging or . 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 element found in previous versions to create a localhost_log is no longer a valid nested element of . 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.

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.      (v1.2 or later) and place the log4j jar in $CATALINA_HOME/common/lib.

3.      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中的配置基本一样,只是相关文件放的目录有所不同. 

首先需要准备的文件为: 

1.log4j.jar , 下载地址 :
2.Tomcat 5.5所需的 commons-logging.jar,下载地址:

3.log4j配置文件: 
log4j.properties 
配置内容为: 

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 

#日志级别不能太低,如果配置为debug的话,输出的日志信息太多,导致tomcat启动非常的慢. 

4.Tomcat 6.0所需的juli替换文件: 



在Tomcat6.0中, 

将tomcat-juli-adapters.jar,log4j-1.2.15.jar,log4j.properties复制到D:/Java/ApacheTomcat6.0.14/lib下面. 

将tomcat-juli.jar复制到D:/Java/apache-tomcat-6.0.14/bin/下面. 

然后启动tomcat,就可以在D:/Java/apache-tomcat-6.0.14/logs下看到lizongbo.log了. 

在 Tomcat5.5中, 

将commons-logging-1.1.jar,log4j-1.2.15.jar,复制到D:/Java/apache-tomcat-5.5.25/common/lib下面. 
将log4j.properties复制到D:/Java/apache-tomcat-5.5.25/common/classes下面. 

然后启动tomcat,就可以在D:/Java/apache-tomcat-5.5.25/logs下看到lizongbo.log了.

阅读(1115) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~