forked from CCExtractor/sample-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_configuration.py
42 lines (35 loc) · 1.37 KB
/
log_configuration.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
import logging
import logging.handlers
import os
class LogConfiguration:
"""
This class handles common logging options for the entire project
"""
def __init__(self, folder, filename, debug=False):
# create console handler
self._consoleLogger = logging.StreamHandler()
self._consoleLogger.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
if debug:
self._consoleLogger.setLevel(logging.DEBUG)
else:
self._consoleLogger.setLevel(logging.INFO)
# create a file handler
path = os.path.join(folder, 'logs', '{name}.log'.format(name=filename))
self._fileLogger = logging.handlers.RotatingFileHandler(path, maxBytes=1024 * 1024, backupCount=20)
self._fileLogger.setLevel(logging.DEBUG)
# create a logging format
formatter = logging.Formatter('[%(name)s][%(levelname)s][%(asctime)s] %(message)s')
self._fileLogger.setFormatter(formatter)
@property
def file_logger(self):
return self._fileLogger
@property
def console_logger(self):
return self._consoleLogger
def create_logger(self, name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
# add the handlers to the logger
logger.addHandler(self.file_logger)
logger.addHandler(self.console_logger)
return logger