|
2 | 2 | import os |
3 | 3 | import sys |
4 | 4 | import yaml |
5 | | -import logging |
| 5 | +import logging.config |
6 | 6 | import subprocess |
7 | 7 | from pathlib import Path |
8 | 8 | from datetime import datetime |
9 | | -from typing import Optional |
10 | | - |
11 | | - |
12 | | -class Logging: |
13 | | - """Logging utility class for configuring application logging.""" |
14 | | - |
15 | | - @staticmethod |
16 | | - def setup_logging( |
17 | | - default_path="support/logging.yaml", |
18 | | - default_level=logging.INFO, |
19 | | - env_key="LOG_CFG", |
20 | | - ): |
21 | | - """Set up logging configuration for the application.""" |
22 | | - path = default_path |
23 | | - value = os.getenv(env_key, None) |
24 | | - if value: |
25 | | - path = value |
26 | | - if os.path.exists(path): |
27 | | - with open(path, "rt") as f: |
28 | | - config = yaml.safe_load(f.read()) |
29 | | - logging.config.dictConfig(config) |
30 | | - else: |
31 | | - logging.basicConfig(level=default_level) |
| 9 | +from typing import Optional, List |
| 10 | + |
| 11 | +level_mapping = logging.getLevelNamesMapping() |
| 12 | + |
| 13 | + |
| 14 | +def setup_logging(log_file=None, default_level="INFO"): |
| 15 | + """Setup logging configuration for the root logger. |
| 16 | +
|
| 17 | + Run this function in the beginning of the simulation to setup the root logger. |
| 18 | + """ |
| 19 | + |
| 20 | + try: |
| 21 | + level = level_mapping[default_level] |
| 22 | + except KeyError: |
| 23 | + raise ValueError("Invalid log level option {}".format(default_level)) |
| 24 | + |
| 25 | + root_logger = logging.getLogger() |
| 26 | + root_logger.setLevel(level) |
| 27 | + formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 28 | + root_logger.handlers = [] |
| 29 | + |
| 30 | + # Stream to stdout |
| 31 | + handler = logging.StreamHandler(sys.stdout) |
| 32 | + handler.setFormatter(formatter) |
| 33 | + root_logger.addHandler(handler) |
| 34 | + |
| 35 | + # Stream to file if specified |
| 36 | + if log_file is not None: |
| 37 | + file_handler = logging.FileHandler(log_file) |
| 38 | + file_handler.setFormatter(formatter) |
| 39 | + root_logger.addHandler(file_handler) |
32 | 40 |
|
33 | 41 |
|
34 | 42 | class SimulationLogger: |
@@ -102,7 +110,7 @@ def _find_root_dir(self, folder_name: str) -> Optional[Path]: |
102 | 110 | return parent |
103 | 111 | return None |
104 | 112 |
|
105 | | - def _run_git_cmd(self, args: list[str]) -> Optional[str]: |
| 113 | + def _run_git_cmd(self, args: List[str]) -> Optional[str]: |
106 | 114 | try: |
107 | 115 | return ( |
108 | 116 | subprocess.check_output(["git"] + args, stderr=subprocess.DEVNULL) |
@@ -132,7 +140,7 @@ def _get_invocation_command(self) -> str: |
132 | 140 | def _get_python_version(self) -> str: |
133 | 141 | return sys.version.replace("\n", " ") |
134 | 142 |
|
135 | | - def _get_installed_packages(self) -> list[str]: |
| 143 | + def _get_installed_packages(self) -> List[str]: |
136 | 144 | try: |
137 | 145 | output = subprocess.check_output( |
138 | 146 | [sys.executable, "-m", "pip", "freeze"], stderr=subprocess.DEVNULL |
|
0 commit comments