From 5e30c2f7dad9d123065c351efb4d6a4f393c35a2 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 27 Mar 2023 20:12:37 -0700 Subject: [PATCH] Further upgrades to Logging --- domain/BackupRotator.py | 1 - domain/Logger.py | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/domain/BackupRotator.py b/domain/BackupRotator.py index c7a28bd..dbdf7c1 100755 --- a/domain/BackupRotator.py +++ b/domain/BackupRotator.py @@ -22,7 +22,6 @@ import os # import pprint import shutil import sys -import syslog import time import yaml diff --git a/domain/Logger.py b/domain/Logger.py index 7f373f6..5761f4f 100644 --- a/domain/Logger.py +++ b/domain/Logger.py @@ -1,5 +1,7 @@ import logging +from logging.handlers import SysLogHandler + import sys class Logger: @@ -17,13 +19,39 @@ class Logger: self.__logger.setLevel(level) - formatter = logging.Formatter('[%(asctime)s][%(name)s][%(levelname)s] %(message)s') + formatter = logging.Formatter('[%(name)s][%(levelname)s] %(message)s') + formatter_full = logging.Formatter('[%(asctime)s][%(name)s][%(levelname)s] %(message)s') - # Console output / stream handler - handler = logging.StreamHandler() + # Console output / stream handler (STDOUT) + handler = logging.StreamHandler( + stream=sys.stdout + ) + handler.setLevel(level) + handler.addFilter(lambda entry: entry.levelno <= logging.INFO) + handler.setFormatter(formatter_full) + self.__logger.addHandler(handler) + + # Console output / stream handler (STDERR) + handler = logging.StreamHandler( + stream=sys.stderr + ) + handler.setLevel(logging.WARNING) + handler.setFormatter(formatter_full) + self.__logger.addHandler(handler) + + # Syslog handler + handler = SysLogHandler( + address="/dev/log" + ) handler.setLevel(level) handler.setFormatter(formatter) self.__logger.addHandler(handler) + + self.debug("Test debug log") + self.info("Test info log") + self.warn("Test warn log") + self.error("Test error log") + def debug(self, s): self.__logger.debug(s)