Created
February 24, 2016 06:41
-
-
Save methane/b3706c2f9cc8fb1271c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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