linux下tomcat日志总结
一、前言
Tomcat的日志默认使用JDK的Log,java.util.logging,单独提供了自定义的logging.properties文件,在这个文件里指定了不同的应用的日志级别等。同时提供了Console和File两种格式的日志输出。
二、linux下tomcat默认有下列六种日志
1、catalna.out
控制台输出的日志,Linux下默认重定向到catalina.out,tomcat官方解释为
When running Tomcat on unixes, the console output is usually redirected to the file
named catalina.out. The name is configurable using an environment variable. (See
the startup scripts). Whatever is written to System.err/out will be caught into that file.
That may include:
● Uncaught exceptions printed by java.lang.ThreadGroup.uncaughtException(..)
● Thread dumps, if you requested them via a system signal
2、catalina.日期.log
Cataline引擎的日志文件,文件名catalina.日期.log
3、localhost.日期.log
Tomcat下内部代码丢出的日志,文件名localhost.日期.log(jsp页面内部错误的异常,org.apache.jasper.runtime.HttpJspBase.service类丢出的,日志信息就在该文件!)
4、localhost_access_log.日期.txt
Access访问日志,tomcat官方解释为
Access logging is a related but different feature, which is implemented as a Valve. It
uses self-contained logic to write its log files.These logs can later be analyzed by
standard log analysis tools to track page hit counts, user session activity, and so
on.This Valve uses self-contained logic to write its log files, which can be
automaticallyrolled over at midnight each day.
5、manager.日期.log
Tomcat下默认manager应用日志
6、host-manager.日期.log
Web应用程序host-manager的日志
三、六种日志的配置方法如下
1、catalina.out
conf/loggin.properties如下两行用来配置catalina.out
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
如果不要生成catalina.out,需要修改bin/catalina.sh文件
在catalina.sh文件中查找字符串catalina.out
修改
>> “$CATALINA_BASE”/logs/catalina.out 2>&1 &
为
>> /dev/null 2>&1 &
当然,也可以修改日志文件catalilna.out为其它的文件名,方法是修改catalina.sh文件中的catalina.out为其它的字符串即可。
2、catalina.日期.log
conf/loggin.properties如下三行用来配置catalina.日期.log
1catalina.org.apache.juli.FileHandler.level = ALL #OFF只能控制不打印日志,但是文件依然生成
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina. #前缀
handlers和.handlers用来配置是否生成该日志
3、localhost.日期.log
conf/logging.properties如下五行用来配置localhost.日期.log
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers =
2localhost.org.apache.juli.FileHandler
handlers和.handlers用来配置是否生成该日志
4、localhost_access_log.日期.txt
可以在/conf/server.xml中配置配置localhost_access_log.日期.txt
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t"%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
注释后tomcat将不再生成localhost_access_log.日期.txt文件
5、manager.日期.log
conf/logging.properties如下五行用来配置manager.日期.log
3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.FileHandler.prefix = manager.
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers =
3manager.org.apache.juli.FileHandler
handlers和.handlers用来配置是否生成该日志
6、host-manager.日期.log
conf/logging.properties如下五行用来配置host-manager.日期.log
4host-manager.org.apache.juli.FileHandler.level = FINE
4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.FileHandler.prefix = host-manager.
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers =
4host-manager.org.apache.juli.FileHandler
handlers和.handlers用来配置是否生成该日志
四、除了tomcat默认内置的六种日志,还可以使用第三方日志框架,比如log4j日志
1、log4j可以实现应用程序之间的日志分离,tomcat官方解释为
The default implementation of java.util.logging provided in the JDK is too limited to beuseful. A limitation of JDK Logging appears to be the inability to have per-webapplication logging, as the configuration is per-VM.
2、可以配置两种类型的log4j日志
第一种是在应用程序里面配置,把log4j.jar 和 log4j.properties 分别放在 WEB-INF/lib 和 WEB-INF/classes即可
These steps are not needed if you just want to use log4j in your own web application. — In that case, just put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application.
第二种在tomcat实例上配置,需要下载tomcat-juli.jar、tomcat-juli-adapters.jar和log4j.jar,
1)在$CATALINA_BASE/lib或$CATALINA_HOME/lib下创建文件log4j.properties,文件内容如下:
log4j.rootLogger = INFO, CATALINA
# Define all the appenders
log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina
log4j.appender.CATALINA.Append = true
log4j.appender.CATALINA.Encoding = UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File = ${catalina.base}/logs/localhost
log4j.appender.LOCALHOST.Append = true
log4j.appender.LOCALHOST.Encoding = UTF-8
log4j.appender.LOCALHOST.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File = ${catalina.base}/logs/manager
log4j.appender.MANAGER.Append = true
log4j.appender.MANAGER.Encoding = UTF-8
log4j.appender.MANAGER.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File = ${catalina.base}/logs/host-manager
log4j.appender.HOST-MANAGER.Append = true
log4j.appender.HOST-MANAGER.Encoding = UTF-8
log4j.appender.HOST-MANAGER.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding = UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] =\
INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] =\
INFO, HOST-MANAGER
2)下载log4j.jar、 tomcat-juli.jar 和tomcat-juli-adapters.jar
3)把log4j.jar 和 tomcat-juli-adapters.jar 放在$CATALINA_HOME/lib目录,把用下载的tomcat-juli.jar替换$CATALINA_HOME/bin/tomcat-juli.jar ;如果是用CATALINA_BASE运行的tomcat,将$CATALNA_HOME替换成$CATALINA_BASE即可
4)删除$CATALINA_BASE/conf/logging.properties
5)启动tomcat
五、应用
1、按日期拆分catalina.out
2、删除catalina.日期.log、manager.日期.log、host-manager.日期.log、localhost.日期.log
3、保留localhost_access_log.日期.txt
备注:
1、tomcat的六种日志中,除了local_access_日期.log是独立的一套日志逻辑,其它五种日志都依赖于Apache Commons Logging,java.util.logging framework
2、tomcat日志文件级别如下:
OFF > SEVERE > WARNING > INFO > CONFIG > FINE > FINER > FINEST = ALL
3、log4j日志级别如下
DEBUG < INFO < WARN < ERROR < FATAL
参考资料
http://tomcat.apache.org/tomcat-7.0-doc/logging.html
http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Logging
http://www.cnblogs.com/iliuyuet/p/4481910.html
http://blog.csdn.net/buster2014/article/details/50844548
http://blog.csdn.net/death10001/article/details/8486355(log4j配置详解)