|
|
|
@ -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) |
|
|
|
|