From c9d781dedbae04b5fb47bfb2c01a8d9291f8b7c4 Mon Sep 17 00:00:00 2001 From: Mario Picciani Date: Fri, 6 Oct 2023 14:39:33 +0200 Subject: [PATCH] added log filehandler and cleaned up init scripts --- oktoberfest/__init__.py | 6 +----- oktoberfest/__main__.py | 4 +--- oktoberfest/runner.py | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/oktoberfest/__init__.py b/oktoberfest/__init__.py index d5682e9a..5ae5b24b 100644 --- a/oktoberfest/__init__.py +++ b/oktoberfest/__init__.py @@ -9,22 +9,18 @@ import logging.handlers import sys -import time from oktoberfest import plotting as pl from oktoberfest import predict as pr from oktoberfest import preprocessing as pp from oktoberfest import rescore as re -from . import runner - CONSOLE_LOG_LEVEL = logging.INFO logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) if len(logger.handlers) == 0: formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(name)s::%(funcName)s %(message)s") - converter = time.gmtime # add console handler console_handler = logging.StreamHandler(sys.stdout) console_handler.setLevel(CONSOLE_LOG_LEVEL) @@ -37,6 +33,6 @@ error_handler.setFormatter(formatter) logger.addHandler(error_handler) else: - logger.info("Logger already initizalized. Resuming normal operation.") + logger.info("Logger already initialized. Resuming normal operation.") sys.modules.update({f"{__name__}.{m}": globals()[m] for m in ["pl", "pp", "pr", "re"]}) diff --git a/oktoberfest/__main__.py b/oktoberfest/__main__.py index c194c50b..1a3cbb4f 100644 --- a/oktoberfest/__main__.py +++ b/oktoberfest/__main__.py @@ -2,7 +2,7 @@ from rich import traceback -from oktoberfest import __copyright__, __version__, logger, runner +from oktoberfest import runner """triqler.__main__: executed when bootstrap directory is called as script.""" @@ -28,8 +28,6 @@ def _parse_args(): def main(): """Execution of oktoberfest from terminal.""" - logger.info(f"Oktoberfest version {__version__}\n{__copyright__}") - args = _parse_args() runner.run_job(args.config_path) diff --git a/oktoberfest/runner.py b/oktoberfest/runner.py index 9ad680e7..bc710eff 100644 --- a/oktoberfest/runner.py +++ b/oktoberfest/runner.py @@ -1,9 +1,12 @@ +import datetime +import json import logging from pathlib import Path from typing import List, Type, Union from spectrum_io.spectral_library import MSP, DLib, SpectralLibrary, Spectronaut +from oktoberfest import __copyright__, __version__ from oktoberfest import plotting as pl from oktoberfest import predict as pr from oktoberfest import preprocessing as pp @@ -369,11 +372,29 @@ def run_job(config_path: Union[str, Path]): conf.check() job_type = conf.job_type - if job_type == "SpectralLibraryGeneration": - generate_spectral_lib(config_path) - elif job_type == "CollisionEnergyCalibration": - run_ce_calibration(config_path) - elif job_type == "Rescoring": - run_rescoring(config_path) - else: - raise ValueError(f"Unknown job_type in config: {job_type}") + # add file handler to root logger + base_logger = logging.getLogger() + formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(name)s::%(funcName)s %(message)s") + suffix = datetime.datetime.now().strftime("%Y_%m_%d-%H_%M_%S") + logging_output = conf.output / f"{job_type}_{suffix}.log" + file_handler = logging.FileHandler(filename=logging_output) + file_handler.setLevel(logging.DEBUG) + file_handler.setFormatter(formatter) + base_logger.addHandler(file_handler) + + logger.info(f"Oktoberfest version {__version__}\n{__copyright__}") + logger.info("Job executed with the following config:") + logger.info(json.dumps(conf.data, indent=4)) + + try: + if job_type == "SpectralLibraryGeneration": + generate_spectral_lib(config_path) + elif job_type == "CollisionEnergyCalibration": + run_ce_calibration(config_path) + elif job_type == "Rescoring": + run_rescoring(config_path) + else: + raise ValueError(f"Unknown job_type in config: {job_type}") + finally: + file_handler.close() + base_logger.removeHandler(file_handler)