Skip to content

Instantly share code, notes, and snippets.

@methane
Created February 24, 2016 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methane/b3706c2f9cc8fb1271c8 to your computer and use it in GitHub Desktop.
Save methane/b3706c2f9cc8fb1271c8 to your computer and use it in GitHub Desktop.
"""
Compress FileHandler output with subprocess gzip.
"""
import subprocess
import logbook
from logbook.helpers import is_unicode
class GZFileHandler(logbook.FileHandler):
proc = None
def __init__(self, filename, complevel=4, **kwargs):
kwargs['delay'] = False
logbook.FileHandler.__init__(self, filename, **kwargs)
cmd = ["gzip", "-" + str(complevel)]
self.proc = subprocess.Popen(cmd, bufsize=-1,
stdin=subprocess.PIPE,
stdout=self.stream.fileno(),
close_fds=True)
def write(self, item):
if is_unicode(item):
item = item.encode(self.encoding)
self.proc.stdin.write(item)
def close(self):
self.proc.stdin.close()
self.proc.wait()
logbook.FileHandler.close(self)
def main():
handler = GZFileHandler("foo.log.gz")
with handler.applicationbound():
for i in range(10000):
logbook.info("Hello, {}", i)
handler.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment