日志有两种

  • 访问日志
  • 运行日志

访问日志

日志配置(注释即可关闭):

  1. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  2. prefix="localhost_access_log." suffix=".txt"
  3. pattern="%h %l %u %t &quot;%r&quot; %s %b" />

实例:

  1. -rwxrwxr-x 1 worker worker 84813 Jan 9 13:39 localhost_access_log.2020-01-09.txt

内容:

  1. 10.200.9.86 - - [09/Jan/2020:13:38:56 +0800] "POST /provider/48/provider-detail.htm HTTP/1.1" 200 591
  2. 10.200.9.86 - - [09/Jan/2020:13:39:02 +0800] "GET /index.htm HTTP/1.1" 200 6311
  3. 10.200.9.86 - - [09/Jan/2020:13:39:02 +0800] "POST /provider/48/provider-detail.htm HTTP/1.1" 200 591
  4. 10.200.9.86 - - [09/Jan/2020:13:39:07 +0800] "GET /index.htm HTTP/1.1" 200 6311
  5. 10.200.9.86 - - [09/Jan/2020:13:39:07 +0800] "POST /provider/48/provider-detail.htm HTTP/1.1" 500 3887
  6. 10.200.9.86 - - [09/Jan/2020:13:39:10 +0800] "POST /app/list.htm HTTP/1.1" 200 1498
  7. 10.200.9.86 - - [09/Jan/2020:13:39:15 +0800] "POST /app/xxx-consumer/nodes.htm HTTP/1.1" 200 12
  8. 10.200.9.86 - - [09/Jan/2020:13:39:18 +0800] "POST /app/list.htm HTTP/1.1" 200 1442
  9. 10.200.9.86 - - [09/Jan/2020:13:39:20 +0800] "POST /dubbo-ui/loadAppsType.htm HTTP/1.1" 404 1042
  10. 10.200.9.86 - - [09/Jan/2020:13:39:20 +0800] "POST /dubbo-ui/loadServiceProtocols.htm HTTP/1.1" 404 1042

运行日志

  • 运行日志分为5类:catalina 、localhost 、manager 、admin 、host-manager


  • 日志的级别分为7种:SEVERE (highest value) > WARNING > INFO > CONFIG > FINE > FINER > FINEST (lowest value),除此之外,还可以使用OFF关闭相关日志,使用ALL输出所有级别的日志


  • 修改 TOMCAT_HOME/conf/logging.properties 中的内容,设定某类日志的级别
  1. # 设置 catalina 日志的级别为: FINE
  2. catalina.org.apache.juli.FileHandler.level = FINE
  3. # 禁用 catalina 日志的输出:
  4. catalina.org.apache.juli.FileHandler.level = OFF
  5. # 输出 catalina 所有的日志消息:
  6. catalina.org.apache.juli.FileHandler.level = ALL

实际配置文件:

  1. 1catalina.org.apache.juli.AsyncFileHandler.level = FINE
  2. 1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
  3. 1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
  4. 1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90 # 保留90天,可以修改
  5. 1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8
  6. 2localhost.org.apache.juli.AsyncFileHandler.level = FINE
  7. 2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
  8. 2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
  9. 2localhost.org.apache.juli.AsyncFileHandler.maxDays = 90
  10. 2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8

catalina日志太大的问题

方式1:直接关闭日志

image.png

方式二:使用logrotate

CentOS6.5后自带logrotate程序,可以解决catalina.out的日志轮转问题

  1. cat >/etc/logrotate.d/tomcat
  2. /usr/local/tomcat/logs/catalina.out{
  3. copytruncate
  4. daily
  5. rotate 7
  6. missingok
  7. compress
  8. size 16M
  9. }

解释:

  1. 要轮转的文件
  2. 创建新的catalina.out副本,截断源catalina.out文件
  3. 每天进行catalina.out文件的轮转
  4. 至多保留7个副本
  5. 文件丢失了,轮转不报错
  6. 使用压缩
  7. catalina.out文件大于16MB,就轮转