tomcat修改log文件输出格式
apache-tomcat-9.0.27目录结构如下:
|-- bin
|-- conf
|-- lib
|-- logs
|-- temp
|-- webapps
|-- work
默认配置启动tomcat,发现logs下日志文件名格式如下:
total 20
-rw-r----- 1 tomcat tomcat 5976 Aug 21 15:43 catalina.2020-08-21.log
-rw-r----- 1 tomcat tomcat 5976 Aug 21 15:43 catalina.out
-rw-r----- 1 tomcat tomcat 0 Aug 21 15:43 host-manager.2020-08-21.log
-rw-r----- 1 tomcat tomcat 408 Aug 21 15:43 localhost.2020-08-21.log
-rw-r----- 1 tomcat tomcat 0 Aug 21 15:43 localhost_access_log.2020-08-21.txt
-rw-r----- 1 tomcat tomcat 0 Aug 21 15:43 manager.2020-08-21.log
日志分类说明:
JULI logging levels for Tomcat
Level |
Log content |
SEVERE |
Serious failures |
WARNING |
Potential problems |
INFO |
Informational messages |
CONFIG |
Static configuration messages |
FINE |
Trace messages |
FINER |
Detailed trace messages |
FINEST |
Highly detailed trace messages |
logging.properties默认配置如下:
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.
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90
1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8
2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.maxDays = 90
2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8
3manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.
3manager.org.apache.juli.AsyncFileHandler.maxDays = 90
3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
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.
4host-manager.org.apache.juli.AsyncFileHandler.maxDays = 90
4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
java.util.logging.ConsoleHandler.encoding = UTF-8
############################################################
# 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
关闭catalina.YYYY-MM-DD.log localhost.YYYY-MM-DD.log manager.YYYY-MM-DD.log host-manager.YYYY-MM-DD.log文件,只开启catalina.out,localhost_access_log.YYYY-MM-DD.txt文件,则:
# 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
handlers = java.util.logging.ConsoleHandler
.handlers = java.util.logging.ConsoleHandler
或者(好像仍然有空文件产生)
1catalina.org.apache.juli.AsyncFileHandler.level = OFF
2localhost.org.apache.juli.AsyncFileHandler.level = OFF
3manager.org.apache.juli.AsyncFileHandler.level = OFF
4host-manager.org.apache.juli.AsyncFileHandler.level = OFF
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = OFF
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = OFF
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = OFF
只关闭以上文件名称中的日期,则修改logging.properties中的内容:
1catalina.org.apache.juli.AsyncFileHandler.rotatable = false
2localhost.org.apache.juli.AsyncFileHandler.rotatable = false
3manager.org.apache.juli.AsyncFileHandler.rotatable = false
4host-manager.org.apache.juli.AsyncFileHandler.rotatable = false
关闭catalina.out
vim bin/catalina.sh
if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT=/dev/null
fi
或者
vim conf/logging.properties
.handlers = 1catalina.org.apache.juli.FileHandler
关闭localhost_access_log.YYYY-MM-DD.txt文件
<!-- 在server.xml配置文件中指定,注释以下内容 -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
-->
想要切割catalina.out
可以配置tomcat使用log4j方式,禁用logging.properties,下载log4j.jar
“$CATALINA_HOME/lib”中创建log4j.properties
log4j.rootLogger=INFO, R
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.base}/logs/tomcat.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
可以使用logrotate的方式切割
/opt/webadmin/tomcat_8001/logs/catalina.out {
notifempty
daily
dateext
missingok
dateformat -%Y%m%d%s
rotate 90
compress
create 644 root root
sharedscripts
postrotate
/usr/bin/kill -USR1 ps -ef | grep /opt/webadmin/tomcat_8001 |grep -v grep | awk '{print $2}'
endscript
}
# debug调试验证(实际不会运行)
logrotate -d /etc/logrotate.d/webadmin_tomcat_8081
# 强制轮询
/usr/sbin/logrotate -vf /etc/logrotate.d/webadmin_tomcat_8081
# Logrotate的记录日志
logrotate自身的日志通常存放于/var/lib/logrotate/logrotate.status