From 1b692a56b455868727bcc6a240fc197e2f806348 Mon Sep 17 00:00:00 2001 From: Daniel Van Der Maden Date: Sat, 18 Apr 2020 16:20:57 -0700 Subject: [PATCH] [logging] Add log rotating to ControlledLogger --- Makefile | 0 README.md | 0 pyhmy/logging.py | 21 ++++++++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) mode change 100644 => 100755 Makefile mode change 100644 => 100755 README.md diff --git a/Makefile b/Makefile old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/pyhmy/logging.py b/pyhmy/logging.py index 2d77e92..5511b4a 100644 --- a/pyhmy/logging.py +++ b/pyhmy/logging.py @@ -1,7 +1,20 @@ -import os import threading import datetime +import gzip +import os import logging +import logging.handlers + + +class _GZipRotator: + def __call__(self, source, dest): + os.rename(source, dest) + f_in = open(dest, 'rb') + f_out = gzip.open("%s.gz" % dest, 'wb') + f_out.writelines(f_in) + f_out.close() + f_in.close() + os.remove(dest) class ControlledLogger: @@ -9,7 +22,7 @@ class ControlledLogger: A simple logger that only writes to file when the 'write' method is called. """ - def __init__(self, logger_name, log_dir): + def __init__(self, logger_name, log_dir, backup_count=5): """ :param logger_name: The name of the logger and logfile :param log_dir: The directory in which to save this log file (can be abs or relative). @@ -18,8 +31,10 @@ class ControlledLogger: log_dir = log_dir[:-1] log_dir = os.path.realpath(log_dir) os.makedirs(log_dir, exist_ok=True) - handler = logging.FileHandler(f"{log_dir}/{logger_name}.log") + handler = logging.handlers.TimedRotatingFileHandler(f"{log_dir}/{logger_name}.log", 'midnight', 1, + backupCount=backup_count) handler.setFormatter(logging.Formatter('%(levelname)s - %(message)s')) + handler.rotator = _GZipRotator() self.filename = handler.baseFilename self.logger = logging.getLogger(logger_name)