我正在开发一个在 Ubuntu Server (16.04) 中运行的 Java 应用程序,该应用程序从某些文件夹复制日志文件,然后在用户关闭会话时进行合并。在 ubuntu 服务器中,rsyslog 会过滤并不断从不同的设备(外部)生成日志,并将日志存储在文件夹中。
在 Java 应用程序复制文件后,我需要轮换日志。为此,我计划使用 logrotate 程序。我创建了一个配置,以便在会话关闭后轮换这些文件(用户通过关闭应用程序中的会话来触发此操作)。
在文件夹(/media/content/persist/log/external/)中创建的文件如下:
-rwxr-xr-x 1 syslog syslog 283 Jul 1 20:49 192.168.10.100-syslog.log
-rwxr-xr-x 1 syslog syslog 24K Jul 1 21:25 192.168.10.11-syslog.log
-rwxr-xr-x 1 syslog syslog 8.6K Jul 1 21:25 192.168.10.10-syslog.log
-rwxr-xr-x 1 syslog syslog 439K Jul 1 21:40 192.168.10.12-syslog.log
日志轮换配置:**文件:rsyslog-fc-rotate.conf**
/media/content/persist/log/external/*-syslog.log
{
rotate 4
maxsize 5M
compress
missingok
notifempty
postrotate
invoke-rc.d rsyslog rotate > /dev/null
endscript
}
当我手动运行 logrotate 时,我得到了下面的结果,没有发生任何旋转
> logrotate -f -d -v rsyslog-fc-rotate.conf
reading config file rsyslog-fc-rotate.conf
Handling 1 logs
rotating pattern: /media/content/persist/log/external/*-syslog.log
forced from command line (4 rotations)
empty log files are not rotated, log files >= 5242880 are rotated earlier, old logs are removed
considering log /media/content/persist/log/external/192.168.10.100-syslog.log
log needs rotating
considering log /media/content/persist/log/external/192.168.10.10-syslog.log
log needs rotating
considering log /media/content/persist/log/external/192.168.10.11-syslog.log
log needs rotating
considering log /media/content/persist/log/external/192.168.10.12-syslog.log
log needs rotating
rotating log /media/content/persist/log/external/192.168.10.100-syslog.log, log->rotateCount is 4
dateext suffix '-20240701'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /media/content/persist/log/external/192.168.10.100-syslog.log.4.gz to /media/content/persist/log/external/192.168.10.100-syslog.log.5.gz (rotatecount 4, logstart 1, i 4),
renaming /media/content/persist/log/external/192.168.10.100-syslog.log.3.gz to /media/content/persist/log/external/192.168.10.100-syslog.log.4.gz (rotatecount 4, logstart 1, i 3),
renaming /media/content/persist/log/external/192.168.10.100-syslog.log.2.gz to /media/content/persist/log/external/192.168.10.100-syslog.log.3.gz (rotatecount 4, logstart 1, i 2),
renaming /media/content/persist/log/external/192.168.10.100-syslog.log.1.gz to /media/content/persist/log/external/192.168.10.100-syslog.log.2.gz (rotatecount 4, logstart 1, i 1),
renaming /media/content/persist/log/external/192.168.10.100-syslog.log.0.gz to /media/content/persist/log/external/192.168.10.100-syslog.log.1.gz (rotatecount 4, logstart 1, i 0),
renaming /media/content/persist/log/external/192.168.10.100-syslog.log to /media/content/persist/log/external/192.168.10.100-syslog.log.1
running postrotate script
running script with arg /media/content/persist/log/external/192.168.10.100-syslog.log: "
invoke-rc.d rsyslog reload > /dev/null
"
compressing log with: /bin/gzip
removing old log /media/content/persist/log/external/192.168.10.100-syslog.log.5.gz
error: error opening /media/content/persist/log/external/192.168.10.100-syslog.log.5.gz: No such file or directory
所有文件都重复出现此消息,并且没有发生轮换。不确定失败的原因。
在我使用此配置的应用程序中,每次关闭会话时都需要轮换文件。此 logrorate 调用将来自 Java 服务。