From 406a248ee7215dd954c5f76f2c783a5defd04e96 Mon Sep 17 00:00:00 2001 From: guillaumepichon Date: Wed, 30 Jul 2025 18:04:17 +0200 Subject: [PATCH 1/2] First logging implementation for PyAML. --- pyaml/__init__.py | 14 +++++++++++++- pyaml/pyaml.py | 14 +++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/pyaml/__init__.py b/pyaml/__init__.py index e5a48282..e160aaeb 100644 --- a/pyaml/__init__.py +++ b/pyaml/__init__.py @@ -12,7 +12,19 @@ __author__ = "pyAML collaboration" __author_email__ = "" +import logging.config +import os from pyaml.exception import PyAMLException -__all__ = [__version__, "YamlLoader", PyAMLException] +__all__ = [__version__, PyAMLException] + +config_file = os.getenv("PYAML_LOG_CONFIG", "pyaml_logging.conf") + +if os.path.exists(config_file): + logging.config.fileConfig(config_file, disable_existing_loggers=False) + +logger = logging.getLogger("pyaml") +level = os.getenv("PYAML_LOG_LEVEL", "").upper() +if len(level)>0: + logger.setLevel(getattr(logging, level, logging.WARNING)) diff --git a/pyaml/pyaml.py b/pyaml/pyaml.py index b8be837c..a5a8e3ec 100644 --- a/pyaml/pyaml.py +++ b/pyaml/pyaml.py @@ -1,6 +1,7 @@ """ PyAML main class """ +import logging from pydantic import BaseModel,ConfigDict from .instrument import Instrument @@ -11,6 +12,8 @@ # Define the main class name for this module PYAMLCLASS = "PyAML" +logger = logging.getLogger(__name__) + class ConfigModel(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) @@ -32,14 +35,15 @@ def get(self,name:str) -> Instrument: raise Exception(f"Instrument {name} not defined") return self.INSTRUMENTS[name] -def pyaml(fileName:str) -> PyAML: +def pyaml(filename:str) -> PyAML: """Load an accelerator middle layer""" + logger.log(logging.INFO, f"Loading PyAML from file '{filename}'") # Asume that all files are referenced from folder where main AML file is stored - if not os.path.exists(fileName): - raise Exception(f"{fileName} file nnot found") - rootfolder = os.path.abspath(os.path.dirname(fileName)) + if not os.path.exists(filename): + raise Exception(f"{filename} file not found") + rootfolder = os.path.abspath(os.path.dirname(filename)) set_root_folder(rootfolder) - aml_cfg = load(os.path.basename(fileName)) + aml_cfg = load(os.path.basename(filename)) aml:PyAML = depthFirstBuild(aml_cfg) return aml From f23550923b45d710efa6abe424c21e218fcb0ff4 Mon Sep 17 00:00:00 2001 From: guillaumepichon Date: Wed, 30 Jul 2025 18:04:59 +0200 Subject: [PATCH 2/2] Logging configuration for test should not be in conf management. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b57b65fc..00dbc4bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Dev environements .idea +test_pyaml_logging.conf # Byte-compiled / optimized / DLL files __pycache__/