Log fix bug where log consosle format set to None would throw an exception

Also add prefix "[SettingsLoader] " to print statements in SettingsLoader if we do not write to log
This commit is contained in:
Clemens Schwaighofer
2026-01-23 15:14:31 +09:00
parent 8d97f09e5e
commit 84286593f6
4 changed files with 57 additions and 5 deletions

View File

@@ -577,7 +577,7 @@ class SettingsLoader:
self.log.logger.log(Log.get_log_level_int(level), msg, stacklevel=2) self.log.logger.log(Log.get_log_level_int(level), msg, stacklevel=2)
if self.log is None or self.always_print: if self.log is None or self.always_print:
if print_error: if print_error:
print(msg) print(f"[SettingsLoader] {msg}")
if level == 'ERROR': if level == 'ERROR':
# remove any prefix [!] for error message list # remove any prefix [!] for error message list
self.__error_msg.append(msg.replace('[!] ', '').strip()) self.__error_msg.append(msg.replace('[!] ', '').strip())

View File

@@ -602,9 +602,9 @@ class Log(LogParent):
__setting = self.DEFAULT_LOG_SETTINGS.get(__log_entry, True) __setting = self.DEFAULT_LOG_SETTINGS.get(__log_entry, True)
default_log_settings[__log_entry] = __setting default_log_settings[__log_entry] = __setting
# check console log type # check console log type
default_log_settings['console_format_type'] = cast('ConsoleFormat', log_settings.get( if (console_format_type := log_settings.get('console_format_type')) is None:
'console_format_type', self.DEFAULT_LOG_SETTINGS['console_format_type'] console_format_type = self.DEFAULT_LOG_SETTINGS['console_format_type']
)) default_log_settings['console_format_type'] = cast('ConsoleFormat', console_format_type)
# check log queue # check log queue
__setting = log_settings.get('log_queue', self.DEFAULT_LOG_SETTINGS['log_queue']) __setting = log_settings.get('log_queue', self.DEFAULT_LOG_SETTINGS['log_queue'])
if __setting is not None: if __setting is not None:

View File

@@ -27,7 +27,8 @@ def main():
"per_run_log": True, "per_run_log": True,
# "console_format_type": ConsoleFormatSettings.NONE, # "console_format_type": ConsoleFormatSettings.NONE,
# "console_format_type": ConsoleFormatSettings.MINIMAL, # "console_format_type": ConsoleFormatSettings.MINIMAL,
"console_format_type": ConsoleFormat.TIME_MICROSECONDS | ConsoleFormat.NAME | ConsoleFormat.LEVEL, # "console_format_type": ConsoleFormat.TIME_MICROSECONDS | ConsoleFormat.NAME | ConsoleFormat.LEVEL,
"console_format_type": None,
# "console_format_type": ConsoleFormat.NAME, # "console_format_type": ConsoleFormat.NAME,
# "console_format_type": ( # "console_format_type": (
# ConsoleFormat.TIME | ConsoleFormat.TIMEZONE | ConsoleFormat.LINENO | ConsoleFormat.LEVEL # ConsoleFormat.TIME | ConsoleFormat.TIMEZONE | ConsoleFormat.LINENO | ConsoleFormat.LEVEL

View File

@@ -28,6 +28,7 @@ def tmp_log_path(tmp_path: Path) -> Path:
@pytest.fixture @pytest.fixture
def basic_log_settings() -> LogSettings: def basic_log_settings() -> LogSettings:
"""Basic log settings for testing""" """Basic log settings for testing"""
# Return a new dict each time to avoid state pollution
return { return {
"log_level_console": LoggingLevel.WARNING, "log_level_console": LoggingLevel.WARNING,
"log_level_file": LoggingLevel.DEBUG, "log_level_file": LoggingLevel.DEBUG,
@@ -308,4 +309,54 @@ class TestUpdateConsoleFormatter:
# Verify message was logged # Verify message was logged
assert "Test warning message" in caplog.text assert "Test warning message" in caplog.text
def test_log_console_format_option_set_to_none(
self, tmp_log_path: Path
):
"""Test that when log_console_format option is set to None, it uses ConsoleFormatSettings.ALL"""
# Save the original DEFAULT_LOG_SETTINGS to restore it after test
original_default = Log.DEFAULT_LOG_SETTINGS.copy()
try:
# Reset DEFAULT_LOG_SETTINGS to ensure clean state
Log.DEFAULT_LOG_SETTINGS = {
"log_level_console": Log.DEFAULT_LOG_LEVEL_CONSOLE,
"log_level_file": Log.DEFAULT_LOG_LEVEL_FILE,
"per_run_log": False,
"console_enabled": True,
"console_color_output_enabled": True,
"console_format_type": ConsoleFormatSettings.ALL,
"add_start_info": True,
"add_end_info": False,
"log_queue": None,
}
# Create a fresh settings dict with console_format_type explicitly set to None
settings: LogSettings = {
"log_level_console": LoggingLevel.WARNING,
"log_level_file": LoggingLevel.DEBUG,
"per_run_log": False,
"console_enabled": True,
"console_color_output_enabled": False,
"console_format_type": None, # type: ignore
"add_start_info": False,
"add_end_info": False,
"log_queue": None,
}
# Verify that None is explicitly set in the input
assert settings['console_format_type'] is None
log = Log(
log_path=tmp_log_path,
log_name="test_log",
log_settings=settings
)
# Verify that None was replaced with ConsoleFormatSettings.ALL
# The Log class should replace None with the default value (ALL)
assert log.log_settings['console_format_type'] == ConsoleFormatSettings.ALL
finally:
# Restore original DEFAULT_LOG_SETTINGS
Log.DEFAULT_LOG_SETTINGS = original_default
# __END__ # __END__