Logrotate 文件名中包含日期的文件
我正在尝试在 RHEL 中为 tomcat6 日志配置 logrotate。目前,logrotate 对于 catalina.out 日志工作得很好,它被正确地旋转和压缩。
问题在于其中包含日期的文件,例如:
catalina.2012-01-20.log
catalina.2012-01-21.log
catalina.2012-01-22.log
这些文件没有被轮换。我知道我必须在 /etc/logrotate.d/tomcat6 文件中配置这些,其中配置了 catalina.out 的轮换。但我无法配置它。
我想要的只是每天压缩这些旧文件,除了当前日期日志文件。
有人可以帮我解决这个问题吗?
谢谢 诺曼 A.
I am trying to configure logrotate in RHEL for tomcat6 logs. Currently, logrotate works fine for catalina.out log, it is rotated and compressed properly.
The problem is with the files with date in them like:
catalina.2012-01-20.log
catalina.2012-01-21.log
catalina.2012-01-22.log
These files are not being rotated. I understand that I have to configure these in /etc/logrotate.d/tomcat6 file where rotation for catalina.out is configured. But I am not able to configure it.
All I want is these older files to be compressed daily, except the current date log file.
Can anybody help me out on this, please!!
Thanks
Noman A.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
(有史以来第一篇文章,如果它看起来像喝醉的蜘蛛已经格式化了,那么抱歉)
在使用我们的朋友谷歌之后,在这里,我不记得我还能在哪里使用 logrotate (而不是 cron 或其他类似的东西)来实现一些东西。
中有以下内容
我在 /var/log/rsync/:和以下 logrotate 文件
:我认为这是完全合理的。但在它拒绝工作并发现它永远不会工作(由这篇文章提供)之后,我想知道是否可以捏造它使其工作。
经过大量测试和调整后,我设法通过以下方式捏造它:
进入名为 /etc/logrotate.d/local-rsync 的 logrotate 配置文件。然后创建虚拟日志文件:
然后强制执行 logrotate:
给出:
现在只需等待明天早上...
我意识到 cron 会更整洁,但是我在 logrotate 配置文件中有另一个元素,并且希望将两者放在一起。
虚拟文件的好处是它不占用任何空间!
您可能会发现有一天它似乎没有旋转任何东西。我花了一段时间才弄清楚原因,但后来它发生了变化。 find -mtime +1 是一整天(即 24*60 分钟),如果自上次创建日志后不到 24 小时内开始每日日志轮转,那么有时似乎不起作用。如果它困扰您,那么使用 23 小时和 find -mmin +1380 可能更合适。
(First post ever so if it looks like a drunk spider has formatted it then sorry)
After using our friend Google, here and I can't remember where else I managed to achieve something using logrotate (rather than cron or some other equivalent).
I have a the following in /var/log/rsync/:
and the following logrotate file:
which I thought was perfectly reasonable. But after it refused to work and on finding out that it would never work (courtesy of this post) I wondered if it could be fudged to make it work.
After much testing and tweaking I managed to fudge it the following way:
into a logrotate config file called /etc/logrotate.d/local-rsync. Then create the dummy log file:
then force a logrotate with:
which gives:
Now just wait for tomorrow morning...
I realise that cron would be tidier however I have another element in the logrotate config file and wanted to keep the two together.
Bonus with the dummy file is that it doesn't take up any space!
You may find that it does not appear to have rotated anything one day. It took me while to work out why but then it twigged. find -mtime +1 is whole days (i.e. 24*60 minutes) and if the daily logrotate kicked in less than 24 hours since the last time/time the logs were created then it sometimes appears not to have worked. If it bothers you then using 23 hours with find -mmin +1380 might be more appropriate.
我花了相当长的时间阅读了大量文档。 Logrotate 似乎无法对文件名中包含日期的不同文件进行分组。 Logrotate 不能做我们需要它做的事情。
您有两个选项可以更改 java / tomcat 提供的日志记录功能,使其不在文件名中包含日期。
http://tomcat.apache.org/tomcat-6.0-doc/logging。 第二种更快的方法是使用您自己的小脚本来为
您完成工作,即使用
find
。 https://serverfault.com/questions/256218/logrotation-when-filenames-includes-date< /a>, https://serverfault.com/a/256231/71120我选择了第二个选项,因为我们的开发人员已经在文件名中编码了日期。所以它需要保持这种状态。
-mtime +5
将 find 设置为仅查找早于 5 天的文件。从
find
的文档中。根据评论更新
如果您特别想删除,这是一种快速的方法。
如果您需要其他命令,您可以随时将 exec rm {} \; 替换为其他命令。
I spent a quite a while reading a lot of documentation. Logrotate does not seem to be able to group the different files with dates included in the name of the file. Logrotate can not do what we need it to do.
You have two options change the logging facility provided by java / tomcat to not include the date in the file name.
http://tomcat.apache.org/tomcat-6.0-doc/logging.html
The second and quicker way is to use your own little script to do the work for you, using
find
. https://serverfault.com/questions/256218/logrotation-when-filenames-includes-date, https://serverfault.com/a/256231/71120I went with the second option, because our developers have coded for dates in the files names. So it needs to stay that way.
The
-mtime +5
sets find to only look for files who are older then 5 days.From
find
's documentation.Updated as per comment
If you specifically want to delete, this is a quick way to do it.
If you need to some other command you can always replace the
exec rm {} \;
with something else./etc/cron.d/rotate_tomcat_logs 中类似以下内容:
Something like this in /etc/cron.d/rotate_tomcat_logs:
/路径/到/日志/*.log {
米索克
压缩
旋转 7
这种
类型的事情不能正常工作,因为正如其他人指出的那样,tomcat 有自己的日志轮换。您可以使用简单的 cron 来删除旧文件或关闭访问日志阀门上的轮换。通过关闭日志轮换(并可能更改文件名模式),上述 logrotate 和其他类似的配置将正常工作。
最重要的是,您应该使用 logrotate 或 tomcat 中内置的日志轮转,但不能同时使用两者。
/path/to/logs/*.log {
missingok
compress
rotate 7
}
this type of thing doesn't work normally because as others point out tomcat has its own log rotation. You can either use a simple cron to delete old files or turn off rotation on the access log valve. By turning off log rotation (and possible changing the filename patter), the above logrotate and other similar configs will work fine.
The bottom line is you should use logrotate or the built in log rotation in tomcat but not both at the same time.
要在旋转文件中包含日期,您可以使用“dateext”选项。
旋转的文件应创建类似于下面的内容
唯一的缺点是您每天不能运行它多次,因为该文件将具有该日期的明确名称。
上面的示例来自我在 GC 上的 k8s 中运行的 Nginx docker 容器。 logrotate版本是3.11.0。
希望有帮助!
更新:
来自手册页 https://linux.die.net/man/8/logrotate
To include a date in the rotated file, you can probably use 'dateext' option.
The rotated file should get created similar to below
The only downside is you won't be able to run it more than once per day as the file would have a definite name for that date.
The above example is from my Nginx docker container running in k8s on GC. The logrotate version is 3.11.0.
Hope that helps!
Update:
From man pages https://linux.die.net/man/8/logrotate
另外,您可以添加 crons 而不是硬编码 logrotate。
这样,您将删除超过 30 天的日志,并压缩超过 1 天的日志。
Also, you can add crons instead hardcode logrotate.
In this way, you will delete the logs older than 30 days, and zip the logs older than 1.
也许您可以按照中所述从日志文件名中删除日期
如何从 tomcat 日志中删除日期模式能够使用 logrotate 规则。
这至少对我的本地主机访问日志有用。
Probably you can remove the date from the log file names as described in
How to remove the date pattern from tomcat logs to be able to use logrotate rules.
This worked for me at least for localhost access log.
好吧,我对任何答案都不完全满意,即使那些指出 logrotate 不支持这一点的答案(即只是删除由其他应用程序旋转的文件) 场景肯定是正确的(也许对该工具提出功能请求?)。
因此,我想分享我所采用的另一种方法。与“
find /path/to/logs -mtime +7 -delete
”解决方案不同,此解决方案不会在指定时间段后删除所有旧日志时间。因此,这里是一个示例单行 bash 命令,它仅在磁盘上留下最后 N 个日志(无论何时运行):最后,为了完全涵盖该主题,最后一个替代解决方案不是旋转日志文件(例如,在 Tomcat 中使用
rotatable=false
- 请参阅其文档)并照常使用logrotate
,但不要忘记将其与<一href="https://unix.stackexchange.com/questions/33447/why-we-should-use-create-and-copytruncate-together/39509">'copytruncate' 选项。欢迎提出建议...
Well, I was not fully satisfied with any of the answers, even though the ones stating that the
logrotate
doesn't support this (i. e. just to remove files rotated by some other application) scenario are surely correct (raise a feature request on that tool, maybe?).So, I would like to share an alternative approach I've come to. Unlike the "
find /path/to/logs -mtime +7 -delete
" solution, this one won't remove all the old logs after a specified period of time. So here it comes, an example one-liner bash command which leaves just N last logs on the disk (whenever it is run):Finally, to cover the topic completely, the last alternative solution is not to rotate the log files (e. g. use
rotatable=false
in case of Tomcat - see its docs) and use thelogrotate
as usually, but don't forget to use it with the 'copytruncate' option.Suggestions are welcome...
在日志轮换文件中,使用
rotate #
,其中 # 是在删除日志之前要保留的日志数量。In your log rotate file, use
rotate #
, where # is the number of logs you want to keep before removing them.要在 tomcat [linux] 中启用每日日志轮换,并将日期后缀添加到 catalina 文件,请执行以下更改。
下载 cronolog 软件包并安装在 Linux 操作系统中
wget http://pkgs.fedoraproject.org/repo/pkgs/cronolog/cronolog-1.6.2.tar.gz/md5/a44564fd5a5b061a5691b9a837d04979/cronolog-1.6.2.tar.gz
打开使用 vi 命令修改 apache-tomcat-7.0.65/bin/catalina.sh 文件并更改以下行
示例:/opt/apache-tomcat-7.0.65/bin/catalina.sh
上述更改后保存文件并重新启动 tomcat 以应用更改。
To enable daily log rotation in tomcat [linux] with postfix of date to catalina file, do the below changes.
Download cronolog package and install in linux os
wget http://pkgs.fedoraproject.org/repo/pkgs/cronolog/cronolog-1.6.2.tar.gz/md5/a44564fd5a5b061a5691b9a837d04979/cronolog-1.6.2.tar.gz
open apache-tomcat-7.0.65/bin/catalina.sh file using vi command and change the lines as given below
example : /opt/apache-tomcat-7.0.65/bin/catalina.sh
After the above changes save the file and restart the tomcat to apply changes.
当前的答案可能有效,但我发现它们使事情变得太复杂,而不是使用 logrotate 已经可以很好地处理事情。如果 logrotate 已经处理了这个问题,那么您不需要手动指定命令。如果您只是解决 catalina 执行 logrotation 的初始问题并让 logrotate 处理它,则不需要复杂的配置。
看起来文件已经在旋转,如问题中日志文件名称中的日期所示:
那么,哪个组件已经做到了这一点,它是在 /etc/logrotate.d/tomcat* 中配置的吗?当前您系统上该文件的内容是什么?如果我没记错的话,catalina 可能已经这样做了:每天创建一个新的日志文件并将旧文件移动到新位置。如果您希望 logrotate 来处理此问题,您可能需要更改 tomcat / catalina 的默认行为,如此答案 首先。
之后,catalina 应始终写入
catalina.log
。那么你就可以把轮转/压缩/删除完全交给logrotate来负责。
如果你想压缩旧文件,你可以将 compress 添加到 logrotate 配置中:
例如,你有:
}
你应该得到什么结果:
如果事情不起作用:
资源:
The current answers may work but I find they make things too complicated instead of using logrotate in a way where it already handles things very well. You don't need to specify commands manually if logrotate already handles this. You don't need complicated configuration if you just address the initial problem of catalina doing the logrotation and let logrotate handle it.
It looks like the files are already being rotated as illustrated by the date in the logfile names in the question:
So, which component already does this, is it configured in /etc/logrotate.d/tomcat*? What is the content of this file on your system currently? If I remember correctly, catalina may already doing this itself: Creating a new logfile every day and moving older files to their new location. If you want logrotate to handle this instead, you may want to change the default behaviour of tomcat / catalina, as described in this answer first.
After that, catalina should always write to
catalina.log
.Then you can put the rotation / compression / deletion entirely into the responsibility of logrotate.
If you want to compress older files, you can add compress to the logrotate configuration:
For example, you have:
}
What you should get as a result:
If things are not working:
Resources: