From 466ced4c02174f7492a24d5d4817d2c95b9a5fd3 Mon Sep 17 00:00:00 2001 From: echel0n Date: Tue, 16 Dec 2014 18:01:36 -0800 Subject: [PATCH] Fixed sickragetv/sickrage-issues#109 - resolves logging issues related to new code added in via last updates. --- sickbeard/__init__.py | 2 +- sickbeard/logger.py | 129 +++++++++++++++++++++++++----------------- tests/test_lib.py | 3 +- 3 files changed, 80 insertions(+), 54 deletions(-) diff --git a/sickbeard/__init__.py b/sickbeard/__init__.py index aca9a7d8..25406c00 100755 --- a/sickbeard/__init__.py +++ b/sickbeard/__init__.py @@ -561,7 +561,7 @@ def initialize(consoleLogging=True): fileLogging=False # init logging - logger.initLogging(consoleLogging=consoleLogging, fileLogging=fileLogging, debug=DEBUG) + logger.initLogging(consoleLogging=consoleLogging, fileLogging=fileLogging, debugLogging=DEBUG) # github api try:gh = Github().get_organization(GIT_ORG).get_repo(GIT_REPO) diff --git a/sickbeard/logger.py b/sickbeard/logger.py index d5522f13..c53c52a1 100644 --- a/sickbeard/logger.py +++ b/sickbeard/logger.py @@ -17,8 +17,8 @@ # along with SickRage. If not, see . from __future__ import with_statement - import os + import sys import logging import logging.handlers @@ -27,8 +27,6 @@ import threading import sickbeard from sickbeard import classes -censoredItems = {} - # log levels ERROR = logging.ERROR WARNING = logging.WARNING @@ -42,62 +40,89 @@ reverseNames = {u'ERROR': ERROR, u'DEBUG': DEBUG, u'DB': DB} -# send logging to null +censoredItems = {} + +class NullHandler(logging.Handler): + def emit(self, record): + pass + + class NullFilter(logging.Filter): def filter(self, record): pass -class CensorFilter(logging.Filter): - def filter(self, record): - for k, v in censoredItems.items(): - if v and len(v) > 0 and v in record.msg: - record.msg = record.msg.replace(v, len(v) * '*') - return True -def initLogging(logFile=None, consoleLogging=False, fileLogging=False, debug=False): - # set logging filename - if not logFile: - logFile = os.path.join(sickbeard.LOG_DIR, 'sickrage.log') - - # Add a new logging level DB - logging.addLevelName(DB, 'DB') - - # sickrage logger - sr_log = logging.getLogger() - sr_log.setLevel(DB) - - # tornado loggers - logging.getLogger("tornado.access").addFilter(NullFilter()) - - # console log handler - if consoleLogging: - console = logging.StreamHandler() - console.addFilter(CensorFilter()) - console.setLevel(INFO if not debug else DEBUG) - console.setFormatter(logging.Formatter('%(asctime)s %(levelname)s::%(message)s', '%H:%M:%S')) - sr_log.addHandler(console) - - # rotating log file handler - if fileLogging: - rfh = logging.handlers.RotatingFileHandler(logFile, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8') - rfh.addFilter(CensorFilter()) - rfh.setLevel(DEBUG) - rfh.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(message)s', '%Y-%m-%d %H:%M:%S')) - sr_log.addHandler(rfh) +class CensorLoggingAdapter(logging.LoggerAdapter): + def process(self, msg, kwargs): + for k, v in self.extra.items(): + if v and len(v) > 0 and v in msg: + msg = msg.replace(v, len(v) * '*') + return msg, kwargs -def log(msg, level=INFO, *args, **kwargs): - meThread = threading.currentThread().getName() - message = meThread + u" :: " + msg +class Logger(object): + def __init__(self): + self.logger = CensorLoggingAdapter(logging.getLogger('sickrage'), censoredItems) + self.consoleLogging = False + self.fileLogging = False + self.debugLogging = False + self.logFile = None - logging.log(level, message, *args, **kwargs) - if level == ERROR: - classes.ErrorViewer.add(classes.UIError(message)) + def initLogging(self, consoleLogging=False, fileLogging=False, debugLogging=False): + # set logging filename + if not self.logFile: + self.logFile = os.path.join(sickbeard.LOG_DIR, 'sickrage.log') -def log_error_and_exit(self, error_msg, *args, **kwargs): - log(error_msg, ERROR, *args, **kwargs) + # add a new logging level DB + logging.addLevelName(DB, 'DB') + + # don't propergate to root logger + logging.getLogger('sickrage').propagate = False + + # set minimum logging level allowed + logging.getLogger('sickrage').setLevel(DB) + + # console log handler + if consoleLogging: + console = logging.StreamHandler() + console.setLevel(INFO if not debugLogging else DEBUG) + console.setFormatter(logging.Formatter('%(asctime)s %(levelname)s::%(message)s', '%H:%M:%S')) + logging.getLogger('sickrage').addHandler(console) + + # rotating log file handler + if fileLogging: + 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')) + logging.getLogger('sickrage').addHandler(rfh) + + def log(self, msg, level=INFO, *args, **kwargs): + meThread = threading.currentThread().getName() + message = meThread + u" :: " + msg + + self.logger.log(level, message, *args, **kwargs) + if level == ERROR: + classes.ErrorViewer.add(classes.UIError(message)) + + def log_error_and_exit(self, error_msg, *args, **kwargs): + self.log(error_msg, ERROR, *args, **kwargs) + + if not self.consoleLogging: + sys.exit(error_msg.encode(sickbeard.SYS_ENCODING, 'xmlcharrefreplace')) + else: + sys.exit(1) + +class Wrapper(object): + instance = Logger() + + def __init__(self, wrapped): + self.wrapped = wrapped + + def __getattr__(self, name): + try: + return getattr(self.wrapped, name) + except AttributeError: + return getattr(self.instance, name) + +_globals = sys.modules[__name__] = Wrapper(sys.modules[__name__]) - if not self.consoleLogging: - sys.exit(error_msg.encode(sickbeard.SYS_ENCODING, 'xmlcharrefreplace')) - else: - sys.exit(1) \ No newline at end of file diff --git a/tests/test_lib.py b/tests/test_lib.py index ccb958f1..f87d4dc3 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -54,7 +54,8 @@ FILEDIR = os.path.join(TESTDIR, SHOWNAME) FILEPATH = os.path.join(FILEDIR, FILENAME) SHOWDIR = os.path.join(TESTDIR, SHOWNAME + " final") -sickbeard.logger.initLogging(os.path.join(os.path.join(TESTDIR, 'Logs'), 'test_sickbeard.log')) +sickbeard.logger.logFile = os.path.join(os.path.join(TESTDIR, 'Logs'), 'test_sickbeard.log') +sickbeard.logger.initLogging() #================= # prepare env functions