Skip to content

Commit

Permalink
adapt config search
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-45 committed Dec 17, 2024
1 parent f4d8fde commit 294ee69
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
4 changes: 3 additions & 1 deletion WLEDLipSync.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ def on_ok_click():

config_file = work_dir + "/WLEDLipSync/config/WLEDLipSync.ini"
# create logger
logger = utils.setup_logging(work_dir + '/WLEDLipSync/config/logging.ini', 'WLEDLogger')
logger = utils.setup_logging(log_config_path=work_dir + '/WLEDLipSync/config/logging.ini',
handler_name='WLEDLogger',
config_path=config_file)

if sys.platform.lower() != "win32":
file_to_set = work_dir + '/WLEDLipSync/' + utils.info_window_exe_name()
Expand Down
19 changes: 10 additions & 9 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,36 @@ def error(self, msg, *args, **kwargs):
super().error(msg, *args, **kwargs)


def setup_logging(config_path='logging_config.ini', handler_name: str = None):
def setup_logging(log_config_path='logging_config.ini', handler_name: str = None, config_path: str = 'config/WLEDLipSync.ini'):
"""
Sets up logging configuration based on a specified configuration file.
This function checks for the existence of a logging configuration file, applies the configuration if found,
and returns a logger instance configured according to the settings,
or falls back to a basic configuration if the file is not found.
Args:
config_path (str): The path to the logging configuration file. Defaults to 'logging_config.ini'.
log_config_path (str): The path to the logging configuration file. Defaults to 'logging_config.ini'.
handler_name (str, optional): The name of the logger handler to use. Defaults to None.
config_path (str , optional): global config file path
Returns:
logging.Logger: The configured logger instance.
"""
# Set the custom logger class
logging.setLoggerClass(CustomLogger)

if os.path.exists(config_path):
logging.config.fileConfig(config_path, disable_existing_loggers=True)
config_data = read_config()
if os.path.exists(log_config_path):
logging.config.fileConfig(log_config_path, disable_existing_loggers=True)
config_data = read_config(config_path)
if str2bool(config_data[1]['log_to_main']):
v_logger = logging.getLogger('WLEDLogger')
else:
v_logger = logging.getLogger(handler_name)
v_logger.debug(f"Logging configured using {config_path} for {handler_name}")
v_logger.debug(f"Logging configured using {log_config_path} for {handler_name}")
else:
logging.basicConfig(level=logging.INFO)
v_logger = logging.getLogger(handler_name)
v_logger.warning(f"Logging config file {config_path} not found. Using basic configuration.")
v_logger.warning(f"Logging config file {log_config_path} not found. Using basic configuration.")

return v_logger

Expand Down Expand Up @@ -798,7 +799,7 @@ def is_valid_hostname(hostname):
return False


def read_config():
def read_config(config_file:str = 'config/WLEDLipSync.ini'):
"""
Reads the configuration settings from a specified INI file.
This function loads the configuration file, retrieves various configuration sections,
Expand All @@ -810,7 +811,7 @@ def read_config():
"""
# load config file
lip_cfg = cfg.load('config/WLEDLipSync.ini')
lip_cfg = cfg.load(config_file)
# config keys
server_cfg = lip_cfg.get('server')
app_cfg = lip_cfg.get('app')
Expand Down

0 comments on commit 294ee69

Please sign in to comment.