1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-12-13 11:32:20 -05:00

Added better error handling code to logger class.

Added improved filtering of sensitive data to logger class.
Added more loggers to logger class with finer control over whats logged.
This commit is contained in:
echel0n 2014-12-16 23:20:37 -08:00
parent 466ced4c02
commit 628f2fed1f

View File

@ -63,47 +63,66 @@ class CensorLoggingAdapter(logging.LoggerAdapter):
class Logger(object): class Logger(object):
def __init__(self): def __init__(self):
self.logger = CensorLoggingAdapter(logging.getLogger('sickrage'), censoredItems) self.logger = CensorLoggingAdapter(logging.getLogger('sickrage'), censoredItems)
self.loggers = [
logging.getLogger('sickrage'),
logging.getLogger('tornado.general'),
logging.getLogger('tornado.application'),
]
self.consoleLogging = False self.consoleLogging = False
self.fileLogging = False self.fileLogging = False
self.debugLogging = False self.debugLogging = False
self.logFile = None self.logFile = None
def initLogging(self, consoleLogging=False, fileLogging=False, debugLogging=False): def initLogging(self, consoleLogging=False, fileLogging=False, debugLogging=False):
# set logging filename self.logFile = self.logFile or os.path.join(sickbeard.LOG_DIR, 'sickrage.log')
if not self.logFile: self.debugLogging = debugLogging
self.logFile = os.path.join(sickbeard.LOG_DIR, 'sickrage.log') self.consoleLogging = consoleLogging
self.fileLogging = fileLogging
# add a new logging level DB # add a new logging level DB
logging.addLevelName(DB, 'DB') logging.addLevelName(DB, 'DB')
# don't propergate to root logger # set custom root logger
logging.getLogger('sickrage').propagate = False for logger in self.loggers:
if logger is not self.logger.logger:
logger.root = self.logger.logger
logger.parent = self.logger.logger
# set minimum logging level allowed # set minimum logging level allowed for loggers
logging.getLogger('sickrage').setLevel(DB) for logger in self.loggers:
logger.setLevel(DB)
# console log handler # console log handler
if consoleLogging: if self.consoleLogging:
console = logging.StreamHandler() console = logging.StreamHandler()
console.setLevel(INFO if not debugLogging else DEBUG)
console.setFormatter(logging.Formatter('%(asctime)s %(levelname)s::%(message)s', '%H:%M:%S')) console.setFormatter(logging.Formatter('%(asctime)s %(levelname)s::%(message)s', '%H:%M:%S'))
logging.getLogger('sickrage').addHandler(console) console.setLevel(INFO if not self.debugLogging else DEBUG)
for logger in self.loggers:
logger.addHandler(console)
# rotating log file handler # rotating log file handler
if fileLogging: if self.fileLogging:
rfh = logging.handlers.RotatingFileHandler(self.logFile, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8') rfh = logging.handlers.RotatingFileHandler(self.logFile, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8')
rfh.setLevel(DEBUG)
rfh.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(message)s', '%Y-%m-%d %H:%M:%S')) rfh.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(message)s', '%Y-%m-%d %H:%M:%S'))
logging.getLogger('sickrage').addHandler(rfh) rfh.setLevel(DEBUG)
for logger in self.loggers:
logger.addHandler(rfh)
def log(self, msg, level=INFO, *args, **kwargs): def log(self, msg, level=INFO, *args, **kwargs):
meThread = threading.currentThread().getName() meThread = threading.currentThread().getName()
message = meThread + u" :: " + msg message = meThread + u" :: " + msg
self.logger.log(level, message, *args, **kwargs) # pass exception information if debugging enabled
if level == ERROR: if level == ERROR:
kwargs["exc_info"] = 1
classes.ErrorViewer.add(classes.UIError(message)) classes.ErrorViewer.add(classes.UIError(message))
self.logger.log(level, message, *args, **kwargs)
def log_error_and_exit(self, error_msg, *args, **kwargs): def log_error_and_exit(self, error_msg, *args, **kwargs):
self.log(error_msg, ERROR, *args, **kwargs) self.log(error_msg, ERROR, *args, **kwargs)