Python,想要通过日志轮转和压缩进行日志记录
任何人都可以建议一种在 python 中进行日志记录的方法:
- 每天
- 进行日志轮换 轮换时压缩日志
- 可选 - 删除最旧的日志文件以保留 X MB 的可用空间
- 可选 - sftp 日志文件到服务器
感谢您的任何回复, 弗雷德
Can anyone suggest a way in python to do logging with:
- log rotation every day
- compression of logs when they're rotated
- optional - delete oldest log file to preserve X MB of free space
- optional - sftp log files to server
Thanks for any responses,
Fred
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
encoding='bz2'
参数。 (请注意,此“技巧”仅适用于 Python2。“bz2”不再被视为 Python3 中的编码。)您可以(间接)使用 RotatingFileHandler 进行安排。通过设置 maxBytes 参数,日志文件达到一定大小时将滚动。通过设置
backupCount
参数,您可以控制保留的翻转次数。这两个参数一起允许您控制日志文件消耗的最大空间。您也可以将TimeRotatingFileHandler
子类化以将此行为合并到其中。只是为了好玩,以下是如何子类化
TimeRotatingFileHandler
。当您运行下面的脚本时,它会将日志文件写入/tmp/log_rotate*
。如果
time.sleep
值较小(例如 0.1),日志文件会很快填满,达到 maxBytes 限制,然后滚动。如果
time.sleep
较大(例如 1.0),日志文件会慢慢填满,未达到 maxBytes 限制,但当达到定时间隔(10 秒)时,它们无论如何都会滚动。下面的所有代码均来自 logging/handlers.py 。我只是以最直接的方式将 TimeRotatingFileHandler 与 RotatingFileHandler 结合起来。
encoding='bz2'
parameter. (Note this "trick" will only work for Python2. 'bz2' is no longer considered an encoding in Python3.)You could (indirectly) arrange this using a RotatingFileHandler. By setting the
maxBytes
parameter, the log file will rollover when it reaches a certain size. By setting thebackupCount
parameter, you can control how many rollovers are kept. The two parameters together allow you to control the maximum space consumed by the log files. You could probably subclass theTimeRotatingFileHandler
to incorporate this behavior into it as well.Just for fun, here is how you could subclass
TimeRotatingFileHandler
. When you run the script below, it will write log files to/tmp/log_rotate*
.With a small value for
time.sleep
(such as 0.1), the log files fill up quickly, reach the maxBytes limit, and are then rolled over.With a large
time.sleep
(such as 1.0), the log files fill up slowly, the maxBytes limit is not reached, but they roll over anyway when the timed interval (of 10 seconds) is reached.All the code below comes from logging/handlers.py. I simply meshed TimeRotatingFileHandler with RotatingFileHandler in the most straight-forward way possible.
在旋转期间压缩日志文件的另一种方法(Python 3.3 中的新功能)是使用 BaseRotatingHandler(以及所有继承的)类属性 rotator 例如:
更多内容,您可以参阅 此处。
The other way to compress logfile during rotate (new in python 3.3) is using BaseRotatingHandler (and all inherited) class attribute rotator for example:
More you can see here.
除了 unutbu 的答案之外:以下是如何修改 TimedRotatingFileHandler 以使用 zip 文件进行压缩。
In addition to unutbu's answer: here's how to modify the TimedRotatingFileHandler to compress using zip files.
请注意:Python 3 中的类签名已更改。这是我的 Python 3.6 工作示例
Be warned: The class signatures have changed in python 3. Here is my working example for python 3.6
我想现在加入派对已经太晚了,但这就是我所做的。我创建了一个继承
logging.handlers.RotatingFileHandler
类的新类,并在移动文件之前添加了几行以 gzip 文件。https://github.com/rkreddy46/python_code_reference/blob/master/compressed_log_rotator。 py
I guess it's too late to join the party, but here is what I did. I created a new class inheriting
logging.handlers.RotatingFileHandler
class and added a couple of lines to gzip the file before moving it.https://github.com/rkreddy46/python_code_reference/blob/master/compressed_log_rotator.py
这是我的解决方案(修改自 evgenek),简单并且在 gzip 压缩巨大日志文件时不会阻止 python 代码:
Here is my solution(modified from evgenek), simple and does not block python code while gzipping huge log files:
我看到很多答案都集中在重写处理程序类的 doRollover 方法,该方法很好并且有效,但我认为更干净且推荐的解决方案是定义 rotator 属性在处理程序类上,该类在
rotate
方法中进行检查,而不是覆盖doRollover
。I see lot of answers focussing on overriding the
doRollover
method of the handler class, which is fine and works but I think a much cleaner and recommended solution is to define therotator
attribute on the handler class which is checked for in therotate
method rather than overridingdoRollover
.我认为最好的选择是使用 TimedRotatingFileHandler 的当前实现,并将日志文件重命名为旋转版本后只需压缩它:
I think that the best option will be to use current implementation of
TimedRotatingFileHandler
and after renaming log file to the rotated version just compress it:要复制文件,请对复制的文件进行 gzip(使用纪元时间),然后以不会扰乱日志记录模块的方式清除现有文件:
To copy the file, gzip the copied file (using epoch time), and then clearing out the existing file in a way that won't upset the logging module:
从 python 3.3 开始,您可以轻松扩展 BaseRotatingFileHandler 的任何子类来实现 gzipping:
Since python 3.3 you can easily extend any subclass of BaseRotatingFileHandler to implement gzipping: