Coverage for opt/mealie/lib/python3.12/site-packages/mealie/core/logger/config.py: 65%
32 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
1import json
2import logging
3import pathlib
4import typing
5from logging import config as logging_config
7__dir = pathlib.Path(__file__).parent
8__conf: dict[str, str] | None = None
11def _load_config(path: pathlib.Path, substitutions: dict[str, str] | None = None) -> dict[str, typing.Any]:
12 with open(path) as file:
13 if substitutions: 13 ↛ 26line 13 didn't jump to line 26 because the condition on line 13 was always true
14 contents = file.read()
15 for key, value in substitutions.items():
16 # Replaces the key matches
17 #
18 # Example:
19 # {"key": "value"}
20 # "/path/to/${key}/file" -> "/path/to/value/file"
21 contents = contents.replace(f"${{{key}}}", value)
23 json_data = json.loads(contents)
25 else:
26 json_data = json.load(file)
28 return json_data
31def log_config() -> dict[str, str]:
32 if __conf is None: 32 ↛ 33line 32 didn't jump to line 33 because the condition on line 32 was never true
33 raise ValueError("logger not configured, must call configured_logger first")
35 return __conf
38def configured_logger(
39 *,
40 mode: str,
41 config_override: pathlib.Path | None = None,
42 substitutions: dict[str, str] | None = None,
43) -> logging.Logger:
44 """
45 Configure the logger based on the mode and return the root logger
47 Args:
48 mode (str): The mode to configure the logger for (production, development, testing)
49 config_override (pathlib.Path, optional): A path to a custom logging config. Defaults to None.
50 substitutions (dict[str, str], optional): A dictionary of substitutions to apply to the logging config.
51 """
52 global __conf
54 if config_override: 54 ↛ 55line 54 didn't jump to line 55 because the condition on line 54 was never true
55 __conf = _load_config(config_override, substitutions)
56 else:
57 if mode == "production": 57 ↛ 59line 57 didn't jump to line 59 because the condition on line 57 was always true
58 __conf = _load_config(__dir / "logconf.prod.json", substitutions)
59 elif mode == "development":
60 __conf = _load_config(__dir / "logconf.dev.json", substitutions)
61 elif mode == "testing":
62 __conf = _load_config(__dir / "logconf.test.json", substitutions)
63 else:
64 raise ValueError(f"Invalid mode: {mode}")
66 logging_config.dictConfig(config=__conf)
67 return logging.getLogger()