Centos logrotate每天不旋转日志

时间:2019-08-05 07:57:02

标签: centos7 logrotate

我有一个问题,我在Centos7中为catalina.out配置了每日logrotation,但是它没有旋转,如果强制运行logrotate,它将旋转catalina,但不是每天自动运行。

logrotate.d / tomcat配置文件:

/usr/local/tomcat7/logs/catalina.out
{
    daily
    rotate 30
    missingok
    compress
    copytruncate
}

logrotate.conf:

    # see "man logrotate" for details
# rotate log files daily
daily

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}


# system-specific logs may be also be configured here.

logrotate状态/调试:

    rotating pattern: /usr/local/tomcat7/logs/catalina.out
 after 1 days (30 rotations)
empty log files are rotated, old logs are removed
considering log /usr/local/tomcat7/logs/catalina.out
  log does not need rotating (log has been already rotated)

"/usr/local/tomcat7/logs/catalina.out" 2019-8-5-9:25:18

1 个答案:

答案 0 :(得分:0)

在默认状态下,tomcat使用log4j

您应该在/etc/tomcat/log4j.properties有一个文件,其中包含日志管理的配置。

默认配置为(从测试盒中获取):

log4j.rootLogger=debug, R 
log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.File=${catalina.home}/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 
log4j.logger.org.apache.catalina=DEBUG, R
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
log4j.logger.org.apache.catalina.core=DEBUG, R
log4j.logger.org.apache.catalina.session=DEBUG, R

基于该配置,当日志增长到10MB时,它们将自动旋转,并最多保留10条旧日志。

您可以根据需要更改这些设置,并且有一些不错的指南herehere解释了所有选项,并说明了如何更改为滚动式附录,这可能更有用满足您的需求。

也可以使用log4j进行轮换,但是如果您执行tail -f cat.out之类的操作并且日志轮换了,则需要重新设置文件尾部以继续观看,否则它似乎会中途停止(很多情况下,就像其他日志一样)

请记住删除您尝试通过logrotate应用的任何配置,以免以后出现问题!

要每天轮换,您需要使用以下设置;

  

DailyRollingFileAppender

     

DailyRollingFileAppender根据时间频率旋转日志文件   允许自定义时间长达一分钟。日期格式允许作为一部分   追加者如下:

yyyy-MM Roll over to new log file beginning on first day of every month
yyyy-ww Roll over to new log file beginning on first day of every week
yyyy-MM-dd Roll over daily
yyyy-MM-dd-a Roll over on midday and midnight
yyyy-MM-dd-HH Roll over every hour
yyyy-MM-dd-HH-mm Roll over every minute

这将提供以下配置:

log4j.rootLogger=INFO, fileLogger
log4j.appender.fileLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.fileLogger.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.fileLogger.File=example.log
log4j.appender.fileLogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.fileLogger.datePattern='.'yyyy-MM-dd-HH-mm