背景:前几天运维同事通知我服务器磁盘快满了,让我清理一下服务器没用的文件。不看不知道,一看吓一跳,应用的Tomcat日志与log4j日志一直没有做清理,系统上线多久,日志就保留了多久,这些日志占据了系统大量的磁盘空间。为此记录一下如何让日志只保留最近7天
1.Tomcat日志
我们进入Tomcat的conf
目录下,其中logging.properties
文件是专门用于配置Tomcat日志的。配置方案如下:
shell
# 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.
1catalina.org.apache.juli.FileHandler.maxDays = 7
1catalina.org.apache.juli.FileHandler.limit = 5000000000
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.
2localhost.org.apache.juli.FileHandler.maxDays = 7
3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.FileHandler.prefix = manager.
3manager.org.apache.juli.FileHandler.maxDays = 7
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.
4host-manager.org.apache.juli.FileHandler.maxDays = 7
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
其中我们可以看到 xxxx.maxDays
这个参数,就是配置日志最大的保留日期,这里我填的是7,就是保留7天。
2.log4j日志
2.1log4j1
这里我查阅了大量的资料和观看源码,最终得出结论:log4j1不支持保留最近7天!,即使你是用上了log4j-extras这个包,也做不到保留最近7天!
log4j1最常用的滚动记录日志方式有两种:org.apache.log4j.RollingFileAppender
和org.apache.log4j.DailyRollingFileAppender
。
-
org.apache.log4j.RollingFileAppender
是根据文件的大小做日志分割,他有两个参数MaxFileSize
和MaxBackupIndex
,分别表示文件的大小以及保留文件的数量。输出的文件命名是:ali.log.1、ali.log.2、ali.log.3,输出多少个文件取决于MaxBackupIndex
设置多少。这种模式不是按照天数来生成文件的,只要日志的大小到达MaxFileSize
的值,就会输出一个日志文件。 -
org.apache.log4j.DailyRollingFileAppender
是每天对日志做分割,他有这个参数datePattern
,默认值是'.'yyyy-MM-dd
,意思是每天输出的文件命名是ali.log.2024-07-17、ali.log.2024-07-18、ali.log.2024-07-19,当然如果你不喜欢这种日期的输出方式,也是可以做更改的。这种模式会每天一直输出一份日志文件,不会停止!
特别注意:以上两种模式的参数,是不能够混合使用的,比如我选择了RollingFileAppender模式,然后又配置了datePattern参数,这样是不行的,即使你这样也配置了,也不会生效!
那如果我的系统用了log4j1这个框架,如何做到日志按日期拆分的同时又能保留最近7天呢?以下两种方法:
1.使用linux的crontab定时任务,每隔一段时间定时做删除
2.将log4j1升级到log4j2,但这种方式需要自行评估升级风险,且log4j1的配置文件与log4j2差异巨大
2.2log4j2
log4j2的配置方式可以参考这篇文章:https://blog.csdn.net/weixin_41979605/article/details/118421323