From 8a724400b8d3d0fe167ce9219c23a663725e764d Mon Sep 17 00:00:00 2001 From: "Florian Paul Azim Hoberg (@gyptazy)" Date: Mon, 14 Apr 2025 06:52:04 +0200 Subject: [PATCH] tecdebt: Improve logging handler creation Fixes: #185 --- ...ing-handler-for-no-systemd-integration.yml | 2 +- ..._allow_use_of_minutes_instead_of_hours.yml | 2 +- proxlb/utils/logger.py | 28 ++++++++----------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/.changelogs/1.1.1/185-logging-handler-for-no-systemd-integration.yml b/.changelogs/1.1.1/185-logging-handler-for-no-systemd-integration.yml index cfcda5b..b2c57dc 100644 --- a/.changelogs/1.1.1/185-logging-handler-for-no-systemd-integration.yml +++ b/.changelogs/1.1.1/185-logging-handler-for-no-systemd-integration.yml @@ -1,2 +1,2 @@ fix: - - add handler to log messages with severity less than info to the screen when there is no systemd integration, for instance, inside a docker container. [#185] \ No newline at end of file + - add handler to log messages with severity less than info to the screen when there is no systemd integration, for instance, inside a docker container (by @glitchvern) [#185] diff --git a/.changelogs/1.1.1/187_allow_use_of_minutes_instead_of_hours.yml b/.changelogs/1.1.1/187_allow_use_of_minutes_instead_of_hours.yml index da76ac2..d0c56c7 100644 --- a/.changelogs/1.1.1/187_allow_use_of_minutes_instead_of_hours.yml +++ b/.changelogs/1.1.1/187_allow_use_of_minutes_instead_of_hours.yml @@ -1,2 +1,2 @@ fixed: - - allow the use of minutes instead of hours and only accept hours or minutes in the format [#187] \ No newline at end of file + - allow the use of minutes instead of hours and only accept hours or minutes in the format (by @glitchvern) [#187] diff --git a/proxlb/utils/logger.py b/proxlb/utils/logger.py index c0b7db4..ff149be 100644 --- a/proxlb/utils/logger.py +++ b/proxlb/utils/logger.py @@ -83,26 +83,22 @@ class SystemdLogger: self.logger = logging.getLogger(name) self.logger.setLevel(level) - # Create a JournalHandler for systemd integration if this - # is supported on the underlying OS. + # Create a logging handler depending on the + # capabilities of the underlying OS where systemd + # logging is preferred. if SYSTEMD_PRESENT: # Add a JournalHandler for systemd integration - journal_handler = JournalHandler() - journal_handler.setLevel(level) - # Set a formatter to include the logger's name and log message - formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') - journal_handler.setFormatter(formatter) - # Add handler to logger - self.logger.addHandler(journal_handler) + handler = JournalHandler() else: - # Add a handler for no systemd integration + # Add a stdout handler as a fallback handler = logging.StreamHandler(sys.stdout) - handler.setLevel(level) - # Set a formatter to include the logger's name and log message - formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') - handler.setFormatter(formatter) - # Add handler to logger - self.logger.addHandler(handler) + + handler.setLevel(level) + # Set a formatter to include the logger's name and log message + formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + handler.setFormatter(formatter) + # Add handler to logger + self.logger.addHandler(handler) def set_log_level(self, level: str) -> None: """