Coverage for opt/mealie/lib/python3.12/site-packages/mealie/core/logger/config.py: 65%

32 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-11-25 15:32 +0000

1import json 1a

2import logging 1a

3import pathlib 1a

4import typing 1a

5from logging import config as logging_config 1a

6 

7__dir = pathlib.Path(__file__).parent 1a

8__conf: dict[str, str] | None = None 1a

9 

10 

11def _load_config(path: pathlib.Path, substitutions: dict[str, str] | None = None) -> dict[str, typing.Any]: 1a

12 with open(path) as file: 1a

13 if substitutions: 13 ↛ 26line 13 didn't jump to line 26 because the condition on line 13 was always true1a

14 contents = file.read() 1a

15 for key, value in substitutions.items(): 1a

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) 1a

22 

23 json_data = json.loads(contents) 1a

24 

25 else: 

26 json_data = json.load(file) 

27 

28 return json_data 1a

29 

30 

31def log_config() -> dict[str, str]: 1a

32 if __conf is None: 32 ↛ 33line 32 didn't jump to line 33 because the condition on line 32 was never true1a

33 raise ValueError("logger not configured, must call configured_logger first") 

34 

35 return __conf 1a

36 

37 

38def configured_logger( 1a

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 

46 

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 

53 

54 if config_override: 54 ↛ 55line 54 didn't jump to line 55 because the condition on line 54 was never true1a

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 true1a

58 __conf = _load_config(__dir / "logconf.prod.json", substitutions) 1a

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}") 

65 

66 logging_config.dictConfig(config=__conf) 1a

67 return logging.getLogger() 1a