improve logging a bit
This commit is contained in:
parent
2e6d268de0
commit
1564f4cf8a
@ -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 = []
|
||||
|
@ -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,10 +58,13 @@ 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
|
||||
if self.__write_to_syslog:
|
||||
handler = SysLogHandler(
|
||||
address="/dev/log"
|
||||
)
|
||||
|
25
main.py
25
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,
|
||||
|
Loading…
Reference in New Issue
Block a user