From 1564f4cf8a2ec7520e9c021cfbb14c6872d1d063 Mon Sep 17 00:00:00 2001 From: mike Date: Sun, 9 Jun 2024 06:51:51 -0700 Subject: [PATCH] improve logging a bit --- domain/BackupRotator.py | 18 +++++++++++++++--- domain/Logger.py | 40 +++++++++++++++++++++++++++++----------- main.py | 25 ++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/domain/BackupRotator.py b/domain/BackupRotator.py index e55a834..37b2565 100755 --- a/domain/BackupRotator.py +++ b/domain/BackupRotator.py @@ -28,10 +28,22 @@ import yaml class BackupRotator: - def __init__(self, debug:bool = False): + def __init__( + self, + debug: bool = False, + systemd: bool = False, + write_to_syslog: bool = False + ): - self.__logger = Logger(name=type(self).__name__, debug=debug) - self.__config_helper = Config(logger=self.__logger) + self.__logger = Logger( + name=type(self).__name__, + debug=debug, + systemd=systemd, + write_to_syslog=write_to_syslog, + ) + self.__config_helper = Config( + logger=self.__logger + ) self.__dry_run = False self.__configs = [] diff --git a/domain/Logger.py b/domain/Logger.py index 733d162..8e3fada 100644 --- a/domain/Logger.py +++ b/domain/Logger.py @@ -7,13 +7,26 @@ import sys class Logger: - def __init__(self, name: str, debug: bool = False): + def __init__( + self, + name: str, + debug: bool = False, + write_to_syslog: bool = False, + systemd: bool = False + ): self.__name = name + self.__debug = debug + self.__write_to_syslog = write_to_syslog + self.__systemd = systemd + + self._init_logger() + + def _init_logger(self): self.__logger = logging.getLogger(self.__name) - - if debug: + + if self.__debug: level = logging.DEBUG else: level = logging.INFO @@ -35,7 +48,9 @@ class Logger: ) handler.setLevel(level) handler.addFilter(lambda entry: entry.levelno <= logging.INFO) - handler.setFormatter(formatter_full) + handler.setFormatter( + formatter if self.__systemd else formatter_full + ) self.__logger.addHandler(handler) # Console output / stream handler (STDERR) @@ -43,16 +58,19 @@ class Logger: stream=sys.stderr ) handler.setLevel(logging.WARNING) - handler.setFormatter(formatter_full) + handler.setFormatter( + formatter if self.__systemd else formatter_full + ) self.__logger.addHandler(handler) # Syslog handler - handler = SysLogHandler( - address="/dev/log" - ) - handler.setLevel(level) - handler.setFormatter(formatter) - self.__logger.addHandler(handler) + if self.__write_to_syslog: + handler = SysLogHandler( + address="/dev/log" + ) + handler.setLevel(level) + handler.setFormatter(formatter) + self.__logger.addHandler(handler) # This is annoying inside cron self.debug("Test debug log") diff --git a/main.py b/main.py index 835bace..5166ebc 100755 --- a/main.py +++ b/main.py @@ -21,6 +21,27 @@ def main(): help="Verbose/Debug logging mode" ) + parser.add_argument( + "--systemd", + default=False, + dest="systemd", + action="store_true", + help=( + "Pass if this program will be spawned inside systemd" + " or another system that already adds timestamps to log messages." + ) + ) + + parser.add_argument( + "--syslog", "--write-to-syslog", + default=False, + dest="write_to_syslog", + action="store_true", + help=( + "Pass if you'd like this program to write to syslog." + ) + ) + parser.add_argument( "--config", "-c", dest="config_files", @@ -40,7 +61,9 @@ def main(): args = parser.parse_args() rotator = BackupRotator( - debug=args.debug + debug=args.debug, + systemd=args.systemd, + write_to_syslog=args.write_to_syslog, ) rotator.run( configs=args.config_files,