COMMON: logger no longer looks for hardcoded yaml, but looks for a yaml in the format f"logging_{env}.yaml" where env is an environement varaible tos et the yaml to dev or prod and the path is dependnet on the logger_config file path.

This commit is contained in:
martinappleby committed 2025-11-05 10:59:53 +01:00
1 parent 612241999a
commit 0df121f03c
1 file changed
+21 -4
+21 -4
View File
@@ -1,5 +1,7 @@
import logging
import logging.config
from pathlib import Path
import yaml
import os
@@ -20,12 +22,27 @@ class QtLogHandler(logging.Handler):
except Exception:
self.handleError(record)
def setup_logger(name="aareDAQ", base_dir: str | None = '/tmp/mxlogs'):
def get_config_path(env: str = "dev") -> Path:
module_dir = Path(__file__).parent
config_filename = f"logging_{env}.yaml"
config_path = module_dir / config_filename
if not config_path.exists():
raise FileNotFoundError(f"Logging config not found: {config_path}")
return config_path
def setup_logger(name="aareDAQ", base_dir: str | None = '/tmp/mxlogs', config_path: str | None = None):
# switch to production mode using: $ APP_ENV=prod python main.py
env = os.getenv("APP_ENV", "dev") # default: dev
config_file = (
"/sls/MX/applications/aaredaq/common/src/aaredaqlib/logging_prod.yaml" if env == "prod" else "/sls/MX/applications/aaredaq/common/src/aaredaqlib/logging_dev.yaml"
)
if config_path is None:
config_file = get_config_path(env)
else:
config_file = Path(config_path)
with open(config_file, "r") as f:
config = yaml.safe_load(f.read())