2015-12-13 09:34:12 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
class logger(object):
|
|
|
|
def __init__(self, name, level, logfile):
|
|
|
|
self.name = name
|
|
|
|
self.level = level
|
|
|
|
self.logfile = logfile
|
|
|
|
|
|
|
|
def config(self):
|
|
|
|
# define logger and set logging level
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
|
|
|
if self.level == 'CRITICAL':
|
|
|
|
level = logging.CRITICAL
|
|
|
|
elif self.level == 'ERROR':
|
|
|
|
level = logging.ERROR
|
|
|
|
elif self.level == 'WARNING':
|
|
|
|
level = logging.WARNING
|
|
|
|
elif self.level == 'DEBUG':
|
|
|
|
level = logging.DEBUG
|
|
|
|
else:
|
|
|
|
level = logging.INFO
|
|
|
|
|
|
|
|
logger.setLevel(level)
|
|
|
|
|
|
|
|
# set request requests module log level
|
|
|
|
logging.getLogger("requests").setLevel(logging.CRITICAL)
|
|
|
|
|
2016-09-01 07:07:33 +00:00
|
|
|
if self.logfile:
|
2016-08-31 21:11:04 +00:00
|
|
|
# define handler to log into file
|
|
|
|
file_log_handler = logging.FileHandler(self.logfile)
|
|
|
|
logger.addHandler(file_log_handler)
|
|
|
|
|
|
|
|
# define logging format for file
|
|
|
|
file_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
file_log_handler.setFormatter(file_formatter)
|
2015-12-13 09:34:12 +00:00
|
|
|
|
|
|
|
# define handler to log into console
|
|
|
|
stderr_log_handler = logging.StreamHandler()
|
|
|
|
logger.addHandler(stderr_log_handler)
|
|
|
|
|
2016-08-31 21:11:04 +00:00
|
|
|
# define logging format for console
|
2019-04-29 13:48:04 +00:00
|
|
|
console_formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] | %(message)s')
|
2015-12-13 09:34:12 +00:00
|
|
|
stderr_log_handler.setFormatter(console_formatter)
|
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
return logging.getLogger(self.name)
|