Coverage for /home/jonathan/pyClass/Camino/camino/logger/logger_handler.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-02 12:10 -0600

1""" 

2This is the loging handler module. 

3 

4This module hold the _Logger class, which is a 

5singleton object to encapsulate the logging process. 

6""" 

7 

8import logging.config 

9import os 

10import yaml 

11from inspect import getframeinfo, stack 

12import pathlib 

13 

14LOG_FILE = "logger.yaml" 

15CONFIG_DIR = "config" 

16 

17 

18class _Logger(object): 

19 """ 

20 This is the class that encapsulates the Log actions. 

21 

22 This is intended to be a private class exposed thru the module 

23 __init__.py. 

24 """ 

25 

26 def __init__(self): 

27 """ 

28 This is the constructor for the Log object. 

29 

30 It reads the loger.yaml file from the config module of the Camino 

31 application. 

32 """ 

33 

34 self.logger = None 

35 

36 absolute_path = os.path.dirname(__file__) 

37 relative_path = "../" + CONFIG_DIR + "/" + LOG_FILE 

38 full_path = os.path.join(absolute_path, relative_path) 

39 with open(full_path, 'r') as f: 

40 config = yaml.safe_load(f.read()) 

41 logging.config.dictConfig(config) 

42 self.logger = logging.getLogger() 

43 

44 def message(self, level: str, message: str) -> None: 

45 """ 

46 This is the action method for the Log object. 

47 

48 Log levels (in ascending order) are: 

49 debug 

50 info 

51 warning 

52 error 

53 critical 

54 

55 the default log level is 'info'. 

56 

57 :param level: The log level to use. 

58 :param message: The message to log. 

59 :return: None. 

60 """ 

61 

62 caller = getframeinfo(stack()[1][0]) 

63 path = pathlib.PurePath(caller.filename) 

64 output = "{}/{}:{} - {}".format(path.name, caller.function, caller.lineno, message) 

65 if level == "debug": 

66 self.logger.debug(output) 

67 elif level == "info": 

68 self.logger.info(output) 

69 elif level == "warning": 

70 self.logger.warning(output) 

71 elif level == "error": 

72 self.logger.error(output) 

73 elif level == "critical": 

74 self.logger.critical(output) 

75 else: 

76 self.logger.info(output)