-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
62 lines (54 loc) · 2.65 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import logging
import logging.handlers
import os
class LoggerConfig(object):
def __init__(self, config_path):
try:
if not config_path \
or type(config_path) \
is not str:
raise Exception("No path to logger config file!")
else:
with open(config_path, 'r', encoding='utf-8') as cfg_file:
self.__config = json.load(cfg_file)
self.ch_isEnabled = self.__config['handlers']['console']['enabled']
self.ch_level = self.__config['handlers']['console']['level']
self.ch_formatter = self.__config['handlers']['console']['formatter']
self.fh_isEnabled = self.__config['handlers']['file']['enabled']
self.fh_level = self.__config['handlers']['file']['level']
self.fh_dir = self.__config['handlers']['file']['dir']
self.fh_file = self.__config['handlers']['file']['file']
self.fh_formatter = self.__config['handlers']['file']['formatter']
if not os.path.exists(self.fh_dir):
os.mkdir(self.fh_dir)
except PermissionError as pe:
print("You have no permissions to create {} dir. {}".format(self.fh_dir, pe))
except FileNotFoundError as fnfe:
print("Config file not found! {}".format(fnfe))
except Exception as e:
print(e)
class LoggerFactory(object):
@staticmethod
def createLogger(loggerName):
try:
loggerConfig = LoggerConfig("C:\\PythonProgs\\BlackListParser\\renew_parser_belgie\\configs\\logger.conf")
logger = logging.getLogger(loggerName)
logger.setLevel("INFO")
if not len(logger.handlers):
if loggerConfig.ch_isEnabled:
ch = logging.StreamHandler()
ch.setLevel(loggerConfig.ch_level)
ch.setFormatter(logging.Formatter(loggerConfig.ch_formatter))
logger.addHandler(ch)
if loggerConfig.fh_isEnabled:
fh = logging.FileHandler(os.path.join(loggerConfig.fh_dir, loggerConfig.fh_file))
fh.setLevel(loggerConfig.fh_level)
fh.setFormatter(logging.Formatter(loggerConfig.fh_formatter))
logger.addHandler(fh)
except PermissionError as pe:
print("You have no permissions to create {} directory. {}".format(loggerConfig.fh_dir, pe))
except Exception as e:
print(e)
else:
return logger