compresslog now working correctly
[compresslog.git] / compresslog.py
blobe1e099af6f55796ea5693f31c6482ff8f0c857b1
1 #!/usr/bin/python
3 ## echo | compresslog /path/to/my/log
5 import sys
6 import signal
7 import gzip
8 import zlib
10 ## init globals used in sig handler
11 # fh in append, binary
12 f = gzip.open(sys.argv[1], 'ab')
14 def sig_flush(signum, frame):
15 f.flush(zlib.Z_FULL_FLUSH)
17 def sig_flushexit(signum, frame):
18 f.close()
20 ## ensure we flush on various signals
21 # USR1/USR2: user asked to flush out
22 # HUP: apache is no longer writing to us (hung up)
23 # TERM: apache is shutting us down
24 signal.signal(signal.SIGUSR1, sig_flush)
25 signal.signal(signal.SIGUSR2, sig_flush)
26 signal.signal(signal.SIGHUP, sig_flushexit)
27 signal.signal(signal.SIGTERM, sig_flushexit)
29 # while / readline() sidesteps magic input buffering
30 while 1:
31 line = sys.stdin.readline()
32 if not line:
33 break
34 if not f.closed:
35 f.write(line)
37 if not f.closed:
38 f.close()