Tomcat内部日志默认使用JULI,即Apache Commons Logging一个重命名分支,后者实际为java.util.logging框架。因为JDK提供java.util.logging默认实现有太多束缚导致功能不丰富,比如关键的限制是不能对每个web应用程序进行日志记录,因为配置是针对每个vm的。因此,在默认配置中,tomcat将用一个名为JULI的容器友好实现替换默认的logmanager实现,该实现解决了这些缺点。
在Tomcat根目录conf文件夹下会有一个logging.properties文件$CATALINA_BASE/conf/logging.properties
,主要负责输出tomcat自身的日志文件。该文件是一个全局配置文件,在Tomcat启动脚本里面catalina.sh指定了配置:
如果该文件没有配置,则会尝试使用${java.home}/lib/logging.properties file in the JRE
。
.level= INFO
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Example to customize the SimpleFormatter output format
# to print one-line log message like this:
# <level>: <log message> [<date/time>]
#
# java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.xyz.foo.level = SEVERE
在web应用程序中,项目将会使用WEB-INF/classes/logging.properties
日志配置文件。
Tomcat运行在Unix系统上时,console里面的日志信息会默认写到catalina.out文件中(可以指定文件)。
【1】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.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.
3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.FileHandler.prefix = manager.
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.
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
############################################################
# 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.FileHandler
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.FileHandler
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
# 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
可以看到,tomcat自身的日志都生成在了./logs/文件夹内。
【2】Tomcat默认使用JULI日志系统,其日志信息分为两类
·
① 运行中的日志,它主要记录运行的一些信息,尤其是一些异常错误日志信息 。
② 访问日志信息,它记录的访问的时间,IP ,访问的资料等相关信息。
【3】Catalina引擎的日志文件,文件名catalina.日期.log
【4】Tomcat下内部代码抛出的日志,文件名localhost.日期.log
jsp页面内部错误的异常,org.apache.jasper.runtime.HttpJspBase.service类抛出的,日志信息就在该文件!
另外,如果项目启动不正常,比如抛出了如下异常,也不妨来这里查看一下。
java.lang.IllegalStateException: Illegal access: this web application instance has been
stopped already. Could not load []. The following stack trace is thrown for debugging
purposes as well as to attempt to terminate the thread which caused the illegal access.
【5】Tomcat下默认manager(host-manager)应用日志
至于manager应用和host-manager应用作用是什么,请查看官方文档
控制台输出的日志,Linux下默认重定向到catalina.out。
以上属于第一种–运行日志。
【6】Access日志
默认 tomcat 不记录访问日志,如下方法可以使 tomcat 记录访问日志
编辑 ${catalina}/conf/server.xml 文件;注 :${catalina} 是 tomcat 的安装目录
在${catalina}/conf/server.xml文件中可以看到访问日志配置:
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" pattern="%h %l %u %t "%r" %s %b"
prefix="localhost_access_log." suffix=".txt"/>
访问日志如下图:
日志内容如下图所示:
【7】日志类型与级别
Tomcat 日志分为下面5类:
catalina 、 localhost 、 manager 、 localhost_access、 host-manager
每类日志的级别分为如下 7 种:
SEVERE (highest value) > WARNING > INFO > CONFIG > FINE > FINER >
FINEST (lowest value)
日志级别的设定方法
修改 conf/logging.properties 中的内容,设定某类日志的级别
示例:
设置 catalina 日志的级别为: FINE
catalina.org.apache.juli.FileHandler.level = FINE
禁用 catalina 日志的输出:
catalina.org.apache.juli.FileHandler.level = OFF
输出 catalina 所有的日志消息均输出:
catalina.org.apache.juli.FileHandler.level = ALL