From 67c16995daeda72fdb99aafdec1b6fe3f2fa0d6a Mon Sep 17 00:00:00 2001 From: Adam Gessaman Date: Thu, 12 Feb 2026 17:19:29 -0800 Subject: [PATCH] fix: Enhance logging configuration handling - Updated logging setup to use default values when the [Logging] section is missing, ensuring consistent behavior. - Improved handling of colored output and log file configuration based on the presence of the [Logging] section across multiple modules. - Refactored related code in core and service plugins for better clarity and maintainability. --- modules/core.py | 20 ++++++++++++------- .../service_plugins/map_uploader_service.py | 9 ++++++--- .../service_plugins/packet_capture_service.py | 4 +++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/modules/core.py b/modules/core.py index 8c2e396..2b1717e 100644 --- a/modules/core.py +++ b/modules/core.py @@ -756,11 +756,21 @@ use_zulu_time = false Configures the logging system based on settings in the config file. Sets up console and file handlers, formatters, and log levels for both the bot and the underlying meshcore library. + If [Logging] section is missing, uses defaults (console/journal only, no file). """ - log_level = getattr(logging, self.config.get('Logging', 'log_level', fallback='INFO')) - + if self.config.has_section('Logging'): + log_level = getattr(logging, self.config.get('Logging', 'log_level', fallback='INFO')) + colored_output = self.config.getboolean('Logging', 'colored_output', fallback=True) + log_file = self.config.get('Logging', 'log_file', fallback='meshcore_bot.log') + meshcore_log_level = getattr(logging, self.config.get('Logging', 'meshcore_log_level', fallback='INFO')) + else: + log_level = logging.INFO + colored_output = True + log_file = '' # Console/journal only when no [Logging] section + meshcore_log_level = logging.INFO + # Create formatter - if self.config.getboolean('Logging', 'colored_output', fallback=True): + if colored_output: formatter = colorlog.ColoredFormatter( '%(log_color)s%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S', @@ -791,8 +801,6 @@ use_zulu_time = false self.logger.addHandler(console_handler) # File handler - log_file = self.config.get('Logging', 'log_file', fallback='meshcore_bot.log') - # Strip whitespace and check if empty log_file = log_file.strip() if log_file else '' @@ -824,8 +832,6 @@ use_zulu_time = false self.logger.propagate = False # Configure meshcore library logging (separate from bot logging) - meshcore_log_level = getattr(logging, self.config.get('Logging', 'meshcore_log_level', fallback='INFO')) - # Configure all possible meshcore-related loggers meshcore_loggers = [ 'meshcore', diff --git a/modules/service_plugins/map_uploader_service.py b/modules/service_plugins/map_uploader_service.py index 184fc76..e5392f2 100644 --- a/modules/service_plugins/map_uploader_service.py +++ b/modules/service_plugins/map_uploader_service.py @@ -87,7 +87,9 @@ class MapUploaderService(BaseServicePlugin): if not bot_formatter: try: import colorlog - if bot.config.getboolean('Logging', 'colored_output', fallback=True): + colored = (bot.config.getboolean('Logging', 'colored_output', fallback=True) + if bot.config.has_section('Logging') else True) + if colored: bot_formatter = colorlog.ColoredFormatter( '%(log_color)s%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S', @@ -115,8 +117,9 @@ class MapUploaderService(BaseServicePlugin): console_handler.setFormatter(bot_formatter) self.logger.addHandler(console_handler) - # Also add file handler to write to the same log file as the bot - log_file = bot.config.get('Logging', 'log_file', fallback='meshcore_bot.log') + # Also add file handler to write to the same log file as the bot (skip if no [Logging] section) + log_file = (bot.config.get('Logging', 'log_file', fallback='meshcore_bot.log') + if bot.config.has_section('Logging') else '') if log_file: # Resolve log file path (relative paths resolved from bot root, absolute paths used as-is) log_file = resolve_path(log_file, bot.bot_root) diff --git a/modules/service_plugins/packet_capture_service.py b/modules/service_plugins/packet_capture_service.py index b7a3313..0478078 100644 --- a/modules/service_plugins/packet_capture_service.py +++ b/modules/service_plugins/packet_capture_service.py @@ -83,7 +83,9 @@ class PacketCaptureService(BaseServicePlugin): if not bot_formatter: try: import colorlog - if bot.config.getboolean('Logging', 'colored_output', fallback=True): + colored = (bot.config.getboolean('Logging', 'colored_output', fallback=True) + if bot.config.has_section('Logging') else True) + if colored: bot_formatter = colorlog.ColoredFormatter( '%(log_color)s%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S',