tomcat 日志详解
Tomcat 日志分为5类 :
catalina Cataline引擎的日志文件,文件名catalina.日期.log;
localhost Tomcat下内部代码丢出的日志,文件名localhost.日期.log(jsp页面内部错误的异常,org.apache.jasper.runtime.HttpJspBase.service类丢出的,日志信息就在该文件!);
manager Tomcat下默认manager应用日志,文件名manager.日期.log;
admin
host-manager
对应在logging.properties文件中
1catalina.org.apache.juli.AsyncFileHandler.level = FINE 1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina. 2localhost.org.apache.juli.AsyncFileHandler.level = FINE 2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost. 3manager.org.apache.juli.AsyncFileHandler.level = FINE 3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 3manager.org.apache.juli.AsyncFileHandler.prefix = manager. 4host-manager.org.apache.juli.AsyncFileHandler.level = FINE 4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager. # 控制台输出的日志,Linux下默认定向到catalina.out java.util.logging.ConsoleHandler.level = FINE java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter ############################################################ # Facility specific properties. # Provides extra control for each logger. ############################################################ org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler # web应用程序/manager的日志 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler # web应用程序/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.AsyncFileHandler
每类日志的级别分为7种 :
SEVERE (highest value) > WARNING > INFO > CONFIG > FINE > FINER > FINEST (lowest value)设定某类日志的级别
修改 conf/logging.properties 中的内容
设置 catalina 日志的级别为: FINE
1catalina.org.apache.juli.FileHandler.level = FINE
禁用 catalina 日志的输出:
1catalina.org.apache.juli.FileHandler.level = OFF
输出 catalina 所有的日志消息均输出:
1catalina.org.apache.juli.FileHandler.level = ALL
Tomcat 日志信息分为两类 :
一是运行中的日志,它主要记录运行的一些信息,尤其是一些异常错误日志信息 。
二是访问日志信息,它记录的访问的时间,IP ,访问的资料等相关信息。
访问日志信息需要编辑${catalina}/conf/server.xml 文件,打开如下注释
<!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> -->
Tomcat使用的日志配置文件:$CATALINA_BASE/conf/logging.properties
Tomcat日志管理类默认使用的是JULI:LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
Tomcat默认使用JULI日志系统(可以参考官网文档修改成使用log4j),它是对默认的JDK日志java.util.logging进行一定的封装,和标准JDK日志支持相同的配置。最大的不同是针对不同的classloader,可以使用不同的配置文件,使得tomcat下不同的Web应用程序可以使用各自独立的日志文件。也就是说,Tomcat下的默认日志有如下2个层次:
全局配置文件:That is usually done in the ${catalina.base}/conf/logging.properties file. The file is specified by the java.util.logging.config.file System property which is set by the startup scripts. If it is not readable or is not configured, the default is to use the ${java.home}/lib/logging.properties file in the JRE.
Web应用程序中使用WEB-INF/classes/logging.properties
默认的JRE中的logging.properties会把日志输出到System.err(ConsoleHandler)中,而默认的Tomcat下的配置文件conf/logging.properties会增加多个FileHandlers把日志输出到不同的文件。
JULI日志的扩展配置:
前缀,使得对于一个class可以由多个handler进行处理:A prefix may be added to handler names, so that multiple handlers of a single class may be instantiated. A prefix is a String which starts with a digit, and ends with '.'. For example, 22foobar. is a valid prefix.
引用系统变量,比如使用${catalina.base}/logs/prj.log直接把项目prj的日志定位到tomcat下logs目录:System property replacement is performed for property values which contain ${systemPropertyName}.
As in Java 6.0, loggers can define a list of handlers using the loggerName.handlers property.
By default, loggers will not delegate to their parent if they have associated handlers. This may be changed per logger using the loggerName.useParentHandlers property, which accepts a boolean value.
根looger:The root logger can define its set of handlers using the .handlers property.
Tomcat JULI日志格式:使用Engine,Host, Context来定义日志,Engine一般是Catalina。
org.apache.catalina.core.ContainerBase.[${engine}].[${host}].[${context}]
Tomcat下默认的的配置文件${catalina.base}/conf/logging.properties:
handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler