简体   繁体   English

编写仅在Python中附加gzip压缩日志文件

[英]Writing append only gzipped log files in Python

I am building a service where I log plain text format logs from several sources (one file per source). 我正在构建一个服务,我从几个源(每个源一个文件)记录纯文本格式日志。 I do not intend to rotate these logs as they must be around forever. 我不打算旋转这些日志,因为它们必须永远存在。

To make these forever around files smaller I hope I could gzip them in fly. 为了使这些文件永远变小,我希望我能在飞行中对它们进行gzip。 As they are log data, the files compress very well. 由于它们是日志数据,因此文件压缩得非常好。

What is a good approach in Python to write append-only gzipped text files, so that the writing can be later resumed when service goes on and off? 在Python中编写仅附加gzip压缩文本文件的好方法是什么,以便以后可以在服务开启和关闭时恢复写入? I am not that worried about losing few lines, but if gzip container itself breaks down and the file becomes unreadable that's no no. 我并不担心丢失几行,但如果gzip容器本身发生故障并且文件变得不可读,那就不行了。

Also, if it's no go, I can simply write them in as plain text without gzipping if it's not worth of the hassle. 此外,如果它不行,我可以简单地将它们写成纯文本而不用gzipping,如果它不值得麻烦。

Note: On unix systems you should seriously consider using an external program, written for this exact task: 注意:在unix系统上,您应该认真考虑使用为此确切任务编写的外部程序:

  • logrotate (rotates, compresses, and mails system logs) logrotate (旋转,压缩和邮件系统日志)

You can set the number of rotations so high, that the first file would be deleted in 100 years or so. 您可以将旋转次数设置得如此之高,以便在100年左右的时间内删除第一个文件。


In Python 2, logging.FileHandler takes an keyword argument encoding that can be set to bz2 or zlib . 在Python 2中, logging.FileHandler采用可以设置为bz2zlib的关键字参数encoding

This is because logging uses the codecs module, which in turn treats bz2 (or zlib ) as encoding : 这是因为logging 使用 codecs模块,后者又将bz2 (或zlib )视为编码

>>> 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 version (although the docs mention bz2 as alias, you'll actually have to use bz2_codec - at least w/ 3.2.3): Python 3版本(尽管文档提到 bz2为别名,你实际上必须使用bz2_codec - 至少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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM