chore: improve code structure in logging module

This commit is contained in:
Matthias
2025-02-15 08:24:35 +01:00
parent c3c6207cda
commit f14c4ebcc8

View File

@@ -75,8 +75,7 @@ def setup_logging(config: Config) -> None:
# config['logfilename']), which defaults to '/dev/log', applicable for most # config['logfilename']), which defaults to '/dev/log', applicable for most
# of the systems. # of the systems.
address = (s[1], int(s[2])) if len(s) > 2 else s[1] if len(s) > 1 else "/dev/log" address = (s[1], int(s[2])) if len(s) > 2 else s[1] if len(s) > 1 else "/dev/log"
handler_sl = get_existing_handlers(SysLogHandler) if handler_sl := get_existing_handlers(SysLogHandler):
if handler_sl:
logging.root.removeHandler(handler_sl) logging.root.removeHandler(handler_sl)
handler_sl = SysLogHandler(address=address) handler_sl = SysLogHandler(address=address)
# No datetime field for logging into syslog, to allow syslog # No datetime field for logging into syslog, to allow syslog
@@ -92,8 +91,7 @@ def setup_logging(config: Config) -> None:
"You need the cysystemd python package be installed in " "You need the cysystemd python package be installed in "
"order to use logging to journald." "order to use logging to journald."
) )
handler_jd = get_existing_handlers(JournaldLogHandler) if handler_jd := get_existing_handlers(JournaldLogHandler):
if handler_jd:
logging.root.removeHandler(handler_jd) logging.root.removeHandler(handler_jd)
handler_jd = JournaldLogHandler() handler_jd = JournaldLogHandler()
# No datetime field for logging into journald, to allow syslog # No datetime field for logging into journald, to allow syslog
@@ -102,8 +100,7 @@ def setup_logging(config: Config) -> None:
handler_jd.setFormatter(Formatter("%(name)s - %(levelname)s - %(message)s")) handler_jd.setFormatter(Formatter("%(name)s - %(levelname)s - %(message)s"))
logging.root.addHandler(handler_jd) logging.root.addHandler(handler_jd)
else: else:
handler_rf = get_existing_handlers(RotatingFileHandler) if handler_rf := get_existing_handlers(RotatingFileHandler):
if handler_rf:
logging.root.removeHandler(handler_rf) logging.root.removeHandler(handler_rf)
try: try:
logfile_path = Path(logfile) logfile_path = Path(logfile)