Coverage for conflog/config.py: 100%
50 statements
« prev ^ index » next coverage.py v7.2.4, created at 2024-01-14 05:52 +0000
« prev ^ index » next coverage.py v7.2.4, created at 2024-01-14 05:52 +0000
1"""A module for managing logging configurations.
2"""
3from typing import Union
4import logging
5from .loaders import environ_loader, ini_loader, json_loader, xml_loader, yaml_loader
7LEVELS = {
8 'debug': logging.DEBUG,
9 'warning': logging.WARNING,
10 'info': logging.INFO,
11 'error': logging.ERROR,
12 'critical': logging.CRITICAL
13}
15DEFAULT_HANDLERS = 'stream'
16DEFAULT_DATEFMT = '%d-%b-%y %H:%M:%S'
17DEFAULT_FILENAME = 'conflog.log'
18DEFAULT_FILEMODE = 'w'
19DEFAULT_FORMAT = '%(asctime)s --> %(name)s - %(levelname)s - %(message)s'
20DEFAULT_LEVEL = 'info'
21DEFAULT_EXTRAS = {}
23class Config():
24 """A class for managing logging configurations.
25 """
27 def __init__(self, conf_files: Union[None, list]=None, conf_dict: Union[None, list]=None):
28 """Initialise config by loading and merging
29 the configuration options from files and environment
30 variables, with optional configuration dictionary overwriting
31 everything being specified.
32 """
34 self.conf = {}
36 # Load configurations from files
37 for conf_file in (conf_files or []):
39 curr_conf = {}
41 if conf_file.endswith('.ini'):
42 curr_conf = ini_loader.load(conf_file)
43 elif conf_file.endswith('.json'):
44 curr_conf = json_loader.load(conf_file)
45 elif conf_file.endswith('.xml'):
46 curr_conf = xml_loader.load(conf_file)
47 elif conf_file.endswith('.yaml'):
48 curr_conf = yaml_loader.load(conf_file)
50 self.conf = {**self.conf, **curr_conf}
52 # Load configurations from environment variables
53 # Environment variables configuration overwrites all configuration
54 # files supplied
55 self.conf = {**self.conf, **environ_loader.load()}
57 # Overwrite everything if configuration dictionary is supplied
58 if conf_dict:
59 self.conf = {**self.conf, **conf_dict}
61 def get_handlers(self) -> str:
62 """Get handlers.
63 Handlers is a comma separated value of the handler
64 types to be used.
65 If handlers is not specified, default to 'stream'.
66 Currently supported handlers are 'stream' and 'file'.
67 """
68 return self.conf.get('handlers', DEFAULT_HANDLERS).split(',')
70 def get_datefmt(self) -> str:
71 """Get date format.
72 If date format is not specified, default to '%d-%b-%y %H:%M:%S'.
73 """
74 return self.conf.get('datefmt', DEFAULT_DATEFMT)
76 def get_filename(self) -> str:
77 """Get log filename.
78 If log filename is not specified, default to 'conflog.log'.
79 """
80 return self.conf.get('filename', DEFAULT_FILENAME)
82 def get_filemode(self) -> str:
83 """Get file mode.
84 If file mode is not specified, default to 'w'.
85 """
86 return self.conf.get('filemode', DEFAULT_FILEMODE)
88 def get_format(self) -> str:
89 """Get log format.
90 If log format is not specified, default to
91 '%(asctime)s --> %(name)s - %(levelname)s - %(message)s'.
92 """
93 return self.conf.get('format', DEFAULT_FORMAT)
95 def get_level(self) -> int:
96 """Get log level.
97 If log level is not specified, default to 'info'.
98 """
99 level = self.conf.get('level', DEFAULT_LEVEL)
100 return LEVELS[level.lower()]
102 def get_extras(self) -> dict:
103 """Get extras.
104 Extras is a dictionary of extra message parameters
105 to be added to the log.
106 If extras is not specified, default to an empty dictionary.
107 """
108 extras = self.conf.get('extras', DEFAULT_EXTRAS)
109 if isinstance(extras, str):
110 _extras = {}
111 for pair in extras.split(','):
112 key, value = pair.split('=')
113 _extras[key] = value
114 extras = _extras
115 return extras