Skip to content

Commit

Permalink
added log filehandler and cleaned up init scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
picciama committed Oct 6, 2023
1 parent 50613b5 commit c9d781d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
6 changes: 1 addition & 5 deletions oktoberfest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.")

Check warning on line 36 in oktoberfest/__init__.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/__init__.py#L36

Added line #L36 was not covered by tests

sys.modules.update({f"{__name__}.{m}": globals()[m] for m in ["pl", "pp", "pr", "re"]})
4 changes: 1 addition & 3 deletions oktoberfest/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from rich import traceback

from oktoberfest import __copyright__, __version__, logger, runner
from oktoberfest import runner

Check warning on line 5 in oktoberfest/__main__.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/__main__.py#L5

Added line #L5 was not covered by tests

"""triqler.__main__: executed when bootstrap directory is called as script."""

Expand All @@ -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)

Expand Down
37 changes: 29 additions & 8 deletions oktoberfest/runner.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Check warning on line 383 in oktoberfest/runner.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/runner.py#L376-L383

Added lines #L376 - L383 were not covered by tests

logger.info(f"Oktoberfest version {__version__}\n{__copyright__}")
logger.info("Job executed with the following config:")
logger.info(json.dumps(conf.data, indent=4))

Check warning on line 387 in oktoberfest/runner.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/runner.py#L385-L387

Added lines #L385 - L387 were not covered by tests

try:

Check warning on line 389 in oktoberfest/runner.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/runner.py#L389

Added line #L389 was not covered by tests
if job_type == "SpectralLibraryGeneration":
generate_spectral_lib(config_path)

Check warning on line 391 in oktoberfest/runner.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/runner.py#L391

Added line #L391 was not covered by tests
elif job_type == "CollisionEnergyCalibration":
run_ce_calibration(config_path)

Check warning on line 393 in oktoberfest/runner.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/runner.py#L393

Added line #L393 was not covered by tests
elif job_type == "Rescoring":
run_rescoring(config_path)

Check warning on line 395 in oktoberfest/runner.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/runner.py#L395

Added line #L395 was not covered by tests
else:
raise ValueError(f"Unknown job_type in config: {job_type}")

Check warning on line 397 in oktoberfest/runner.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/runner.py#L397

Added line #L397 was not covered by tests
finally:
file_handler.close()
base_logger.removeHandler(file_handler)

Check warning on line 400 in oktoberfest/runner.py

View check run for this annotation

Codecov / codecov/patch

oktoberfest/runner.py#L399-L400

Added lines #L399 - L400 were not covered by tests

0 comments on commit c9d781d

Please sign in to comment.