logs.py 948 B

1234567891011121314151617181920212223242526272829303132
  1. import gzip
  2. from logging.handlers import TimedRotatingFileHandler
  3. import os
  4. import shutil
  5. class LogFilter(object):
  6. def filter(self, record):
  7. if not record.name:
  8. return True
  9. if record.name == 'tornado.access':
  10. return False
  11. return True
  12. class GzipTimedRotatingFileHandler(TimedRotatingFileHandler):
  13. def __init__(self, filename, when='midnight', interval=1, backupCount=52,
  14. encoding=None, delay=False, utc=False):
  15. TimedRotatingFileHandler.__init__(
  16. self, filename, when=when, interval=interval,
  17. backupCount=backupCount, encoding=encoding,
  18. delay=delay, utc=utc
  19. )
  20. self.rotator = self._rotator
  21. def _rotator(self, source, dest):
  22. with gzip.open(dest, 'wb') as gzip_file, \
  23. open(source, 'rb') as source_file:
  24. shutil.copyfileobj(source_file, gzip_file)
  25. os.remove(source)