Centos logrotate不每天轮换日志

Centos logrotate not rotating logs daily

我有一个问题,我在 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 status/debug:

    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

在默认状态下 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 个旧日志。

您可以根据需要更改这些设置,并且有一些很好的指南 here and here 解释了所有选项,并展示了如何更改为可能对您的需求更有用的滚动附加程序。

log4j 也负责轮换,但如果你正在做类似 tail -f cat.out 的事情并且日志轮换,你将需要重新跟踪文件以继续观看它,否则它只会出现在中途停止(就像其他日志一样)

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

要进行每日轮换,您需要使用这些设置;

DailyRollingFileAppender

DailyRollingFileAppender rotates log files based on frequency of time allowing customization upto minute. Date Patterns allowed as part of the Appender are as follows:

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