Config: Don't fail on config error, fix Windows config dir (#426)

* Support setting logger level (env) before config load.
This commit is contained in:
Dominique Lasserre 2025-02-08 00:45:11 +01:00 committed by GitHub
parent da4994ca39
commit d05b161e24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 9 deletions

View File

@ -281,6 +281,12 @@ class ConfigEOS(SingletonMixin, SettingsEOSDefaults):
- This method logs a warning if the default configuration file cannot be copied.
- It ensures that a fallback to the default configuration file is always possible.
"""
setting_sources = [
init_settings,
env_settings,
dotenv_settings,
]
file_settings: Optional[ConfigFileSourceMixin] = None
config_file, exists = cls._get_config_file_path()
config_dir = config_file.parent
@ -292,20 +298,21 @@ class ConfigEOS(SingletonMixin, SettingsEOSDefaults):
logger.warning(f"Could not copy default config: {exc}. Using default config...")
config_file = cls.config_default_file_path
config_dir = config_file.parent
file_settings = JsonConfigSettingsSource(settings_cls, json_file=config_file)
try:
file_settings = JsonConfigSettingsSource(settings_cls, json_file=config_file)
setting_sources.append(file_settings)
except Exception as e:
logger.error(
f"Error reading config file '{config_file}' (falling back to default config): {e}"
)
default_settings = JsonConfigSettingsSource(
settings_cls, json_file=cls.config_default_file_path
)
GeneralSettings._config_folder_path = config_dir
GeneralSettings._config_file_path = config_file
return (
init_settings,
env_settings,
dotenv_settings,
file_settings,
default_settings,
)
setting_sources.append(default_settings)
return tuple(setting_sources)
@classmethod
@classproperty
@ -462,7 +469,7 @@ class ConfigEOS(SingletonMixin, SettingsEOSDefaults):
logger.debug(f"Environment config dir: '{env_dir}'")
if env_dir is not None:
config_dirs.append(env_dir.resolve())
config_dirs.append(Path(user_config_dir(cls.APP_NAME)))
config_dirs.append(Path(user_config_dir(cls.APP_NAME, cls.APP_AUTHOR)))
config_dirs.append(Path.cwd())
for cdir in config_dirs:
cfile = cdir.joinpath(cls.CONFIG_FILE_NAME)

View File

@ -52,6 +52,10 @@ def get_logger(
# Create a logger with the specified name
logger = pylogging.getLogger(name)
logger.propagate = True
# This is already supported by pydantic-settings in LoggingCommonSettings, however in case
# loading the config itself fails and to set the level before we load the config, we set it here manually.
if logging_level is None and (env_level := os.getenv("EOS_LOGGING__LEVEL")) is not None:
logging_level = env_level
if logging_level is not None:
level = logging_str_to_level(logging_level)
logger.setLevel(level)