1
0
mirror of https://github.com/moparisthebest/SickRage synced 2025-01-05 10:58:01 -05:00

Fixed sickragetv/sickrage-issues#109 - resolves logging issues related to new code added in via last updates.

This commit is contained in:
echel0n 2014-12-16 18:01:36 -08:00
parent 3214f879ac
commit 466ced4c02
3 changed files with 80 additions and 54 deletions

View File

@ -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)

View File

@ -17,8 +17,8 @@
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
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)

View File

@ -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