完整的日志管理(python)
类似的问题已经被问过,但我还没有找到一种容易做到的方法。
我们有一些各种类型的应用程序日志,这些日志填满了空间,并且我们面临其他不需要的问题。如何编写监控脚本(压缩特定大小的文件、移动它们、监视它们等)来进行此维护?我正在寻找一个简单的解决方案(例如使用什么?),如果可能的话,使用 python 或者只是一个 shell 脚本。
谢谢。
Similar questions have been asked, but I have not come across an easy-to-do-it way
We have some application logs of various kinds which fill up the space and we face other unwanted issues. How do I write a monitoring script(zipping files of particular size, moving them, watching them, etc..) for this maintenance? I am looking for a simple solution(as in what to use?), if possible in python or maybe just a shell script.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行此操作的“标准”方法(至少在大多数 Gnu/Linux 发行版上)是使用 logrotate。我在 Debian 机器上看到一个
/etc/logrotate.conf
,其中详细说明了要轮换哪些文件以及以什么频率轮换。它是由每日 cron 条目触发的。这就是我推荐的。如果您希望应用程序本身执行此操作(这确实很痛苦,因为这不是它的工作),您可以考虑编写一个自定义 日志处理程序。 RotatingFileHandler(或 TimedRotatingFileHandler)可能有效,但您可以编写自定义的。
大多数系统默认设置为自动轮换 syslog 发出的日志文件。您可能需要考虑使用 SysLogHandler 并记录到系统日志(从所有应用程序,无论语言如何),以便系统基础架构自动为您处理事情。
The "standard" way of doing this (atleast on most Gnu/Linux distros) is to use logrotate. I see a
/etc/logrotate.conf
on my Debian machine which has details on which files to rotate and at what frequency. It's triggered by a daily cron entry. This is what I'd recommend.If you want your application itself to do this (which is a pain really since it's not it's job), you could consider writing a custom log handler. A RotatingFileHandler (or TimedRotatingFileHandler) might work but you can write a custom one.
Most systems are by default set up to automatically rotate log files which are emitted by syslog. You might want to consider using the SysLogHandler and logging to syslog (from all your apps regardless of language) so that the system infrastructure automatically takes care of things for you.
使用 logrotate 来为您完成这项工作。
请记住,在少数情况下,它可能无法正常工作,例如,如果日志记录应用程序使日志文件始终打开,并且在文件被删除并重新创建时无法恢复它。
多年来,我很少遇到这样的应用程序,但即使对于它们,您也可以配置 logrotate 以在轮换日志时重新启动它们。
Use logrotate to do the work for you.
Remember that there are few cases where it may not work properly, for example if the logging application keeps the log file always open and is not able to resume it if the file is removed and recreated.
Over the years I encountered few applications like that, but even for them you could configure logrotate to restart them when it rotates the logs.