且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Python编写仅附加gzip压缩的日志文件

更新时间:2023-11-10 09:02:16

注意:在Unix系统上,您应该认真考虑使用为该确切任务编写的外部程序:

Note: On unix systems you should seriously consider using an external program, written for this exact task:

  • logrotate (旋转,压缩并通过邮件发送系统日志)

您可以将轮换次数设置得如此之高,以使第一个文件在100年内被删除左右.

You can set the number of rotations so high, that the first file would be deleted in 100 years or so.

在Python 2中,logging.FileHandler采用关键字参数encoding,可以将其设置为bz2zlib.

In Python 2, logging.FileHandler takes an keyword argument encoding that can be set to bz2 or zlib.

这是因为logging 使用 codecs模块,该模块又将bz2(或zlib)视为 encoding :

This is because logging uses the codecs module, which in turn treats bz2 (or zlib) as encoding:

>>> import codecs
>>> with codecs.open("on-the-fly-compressed.txt.bz2", "w", "bz2") as fh:
...     fh.write("Hello World\n")

$ bzcat on-the-fly-compressed.txt.bz2 
Hello World


Python 3版本(尽管文档提及 bz2作为别名,实际上您必须使用bz2_codec-至少使用3.2.3):


Python 3 version (although the docs mention bz2 as alias, you'll actually have to use bz2_codec - at least w/ 3.2.3):

>>> import codecs
>>> with codecs.open("on-the-fly-compressed.txt.bz2", "w", "bz2_codec") as fh:
...     fh.write(b"Hello World\n")

$ bzcat on-the-fly-compressed.txt.bz2 
Hello World