1. 程式人生 > >Tomcat日誌系統詳解

Tomcat日誌系統詳解

mat core catalina res 最大的 tor dumps 異常 startup

技術分享

綜合:Tomcat下相關的日誌文件
catalina引擎的日誌文件,文件名:catalina.日期.log
Tomcat下內部代碼丟出的日誌,文件名localhost.日期.log(jsp頁面內部錯誤的異常,org.apache.jasper.runtime.HttpJspBase.service類丟出的,日誌信息就在該文件!)
Tomcat下默認manager應用日誌,文件名manager.日期.log
控制臺輸出的日誌,Linux下默認重定向到catalina.out
Access日誌(Servlet.xml配置)
應用程序通過log4j.properties:${catalina.base}/logs/probe.log重定向過來的日誌


JULI:org.apache.juli.FileHandler對應的日誌文件名:{prefix}.{date}.{suffix} 默認juli.日期.log.

Tomcat下Web應用程序可以使用如下3種日誌:
使用JDK提供的日誌java.util.logging.
使用Java Servlets規範中定義的日誌javax.servlet.ServletContext.log(...)
使用其他日誌框架,如log4j

不同Web應用程序下使用的Servlet日誌(或者日誌框架提供的日誌)是相互獨立的(這與Tomcat的class loader有關,參考Class Loader HOW-TO
)。如果Web應用程序使用的是java.util.logging日誌,那麽它們並不相互獨立,這是因為java.util.logging是由JAVA系統中的Bootstrap ClassLoader來加載的,因此它在各Web應用程序之間是共享的!
Tomcat使用的日誌配置文件:

$CATALINA_BASE/conf/logging.properties 

Tomcat日誌管理類默認使用的是

JULI:LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" 

Java的stdout and stderr會被重定向到$CATALINA_BASE/logs/catalina.out,同時:下面2種類型的錯誤信息,也會被記錄在這裏
Uncaught exceptions printed by java.lang.ThreadGroup.uncaughtException(..
Thread dumps, if you requested them via a system signal

Access訪問日誌:它與一般的日誌有關系但不太一樣,它在Servlet.xml中的Context或者 Host或者Engine中配置。在上述的配置節中增加下述的Value就行,具體參考:The Valve Component

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"    
       prefix="localhost_access_log." suffix=".logs" pattern="common" resolveHosts="false"/>

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把日誌輸出到不同的文件。

Tomcat JULI日誌格式:使用Engine,Host, Context來定義日誌,Engine一般是Catalina。

org.apache.catalina.core.ContainerBase.[${engine}].[${host}].[${context}] 

Tomcat下默認的的配置文件

${catalina.base}/conf/logging.properties: 
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

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.

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

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler

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

# For example, set the org.apache.catalina.util.LifecycleBase logger to log
# each component that extends LifecycleBase changing state:
#org.apache.catalina.util.LifecycleBase.level = FINE

# To see debug messages in TldLocationsCache, uncomment the following line:
#org.apache.jasper.compiler.TldLocationsCache.level = FINE

# To see debug messages for HTTP/2 handling, uncomment the following line:
#org.apache.coyote.http2.level = FINE

# To see debug messages for WebSocket handling, uncomment the following line:
#org.apache.tomcat.websocket.level = FINE

參考:

http://blog.csdn.net/xysoul/article/details/50347285

Tomcat日誌系統詳解