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.
This commit is contained in:
Adam Gessaman
2026-02-12 17:19:29 -08:00
parent 09662337bb
commit 67c16995da
3 changed files with 22 additions and 11 deletions

View File

@@ -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',

View File

@@ -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)

View File

@@ -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',