From c21c2869f70b2aed1d33675a4e46b7358e3d719f Mon Sep 17 00:00:00 2001 From: Halali Date: Thu, 25 Oct 2018 12:06:33 +0200 Subject: [PATCH] Initial commit --- bazarr/logger.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ bazarr/main.py | 39 +------------------ views/logs.tpl | 14 ++++--- 3 files changed, 107 insertions(+), 43 deletions(-) create mode 100644 bazarr/logger.py diff --git a/bazarr/logger.py b/bazarr/logger.py new file mode 100644 index 000000000..d44cfdaf2 --- /dev/null +++ b/bazarr/logger.py @@ -0,0 +1,97 @@ +import os +import sys +import logging +import re + +from logging.handlers import TimedRotatingFileHandler +from get_argv import config_dir +from get_settings import get_general_settings + +log_level = get_general_settings()[4] +if log_level is None: + log_level = "INFO" + + +class OneLineExceptionFormatter(logging.Formatter): + def formatException(self, exc_info): + """ + Format an exception so that it prints on a single line. + """ + result = super(OneLineExceptionFormatter, self).formatException(exc_info) + return repr(result) # or format into one line however you want to + + def format(self, record): + s = super(OneLineExceptionFormatter, self).format(record) + if record.exc_text: + s = s.replace('\n', '') + '|' + return s + + +class NoExceptionFormatter(logging.Formatter): + def format(self, record): + record.exc_text = '' # ensure formatException gets called + return super(NoExceptionFormatter, self).format(record) + + def formatException(self, record): + return '' + + +def configure_logging(): + log = logging.getLogger() + log.handlers = [] + + global fh + fh = TimedRotatingFileHandler(os.path.join(config_dir, 'log/bazarr.log'), when="midnight", interval=1, + backupCount=7) + f = OneLineExceptionFormatter('%(asctime)s|%(levelname)-8s|%(name)-32s|%(message)s|', + '%d/%m/%Y %H:%M:%S') + fh.setFormatter(f) + fh.addFilter(BlacklistFilter()) + logging.getLogger("enzyme").setLevel(logging.CRITICAL) + logging.getLogger("apscheduler").setLevel(logging.WARNING) + if log_level == 'debug': + logging.getLogger("subliminal").setLevel(logging.DEBUG) + else: + logging.getLogger("subliminal").setLevel(logging.CRITICAL) + logging.getLogger("guessit").setLevel(logging.WARNING) + logging.getLogger("rebulk").setLevel(logging.WARNING) + logging.getLogger("stevedore.extension").setLevel(logging.CRITICAL) + log.setLevel(log_level) + log.addHandler(fh) + + ch = logging.StreamHandler() + cf = NoExceptionFormatter('%(asctime)s - %(levelname)s :: %(message)s', + '%Y-%m-%d %H:%M:%S') + ch.setFormatter(cf) + ch.setLevel(logging.INFO) + log.addHandler(ch) + + +class BlacklistFilter(logging.Filter): + """ + Log filter for blacklisted tokens and passwords + """ + def __init__(self): + pass + + def filter(self, record): + try: + apikeys = re.findall(r'apikey(?:=|%3D)([a-zA-Z0-9]+)', record.msg) + for apikey in apikeys: + record.msg = record.msg.replace(apikey, 8 * '*' + apikey[-2:]) + + args = [] + for arg in record.args: + apikeys = re.findall(r'apikey(?:=|%3D)([a-zA-Z0-9]+)', arg) if isinstance(arg, basestring) else [] + for apikey in apikeys: + arg = arg.replace(apikey, 8 * '*' + apikey[-2:]) + args.append(arg) + record.args = tuple(args) + except: + pass + return True + + +def empty_log(): + fh.doRollover() + diff --git a/bazarr/main.py b/bazarr/main.py index 8d37e45e0..a922b5f9a 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -21,42 +21,7 @@ from update_db import * from get_settings import get_general_settings, get_proxy_settings import logging -from logging.handlers import TimedRotatingFileHandler - -log_level = get_general_settings()[4] -if log_level is None: - log_level = "INFO" - -class OneLineExceptionFormatter(logging.Formatter): - def formatException(self, exc_info): - """ - Format an exception so that it prints on a single line. - """ - result = super(OneLineExceptionFormatter, self).formatException(exc_info) - return repr(result) # or format into one line however you want to - - def format(self, record): - s = super(OneLineExceptionFormatter, self).format(record) - if record.exc_text: - s = s.replace('\n', '') + '|' - return s - -def configure_logging(): - global fh - fh = TimedRotatingFileHandler(os.path.join(config_dir, 'log/bazarr.log'), when="midnight", interval=1, backupCount=7) - f = OneLineExceptionFormatter('%(asctime)s|%(levelname)s|%(message)s|', - '%d/%m/%Y %H:%M:%S') - fh.setFormatter(f) - logging.getLogger("enzyme").setLevel(logging.CRITICAL) - logging.getLogger("apscheduler").setLevel(logging.WARNING) - logging.getLogger("subliminal").setLevel(logging.CRITICAL) - logging.getLogger("guessit").setLevel(logging.WARNING) - logging.getLogger("rebulk").setLevel(logging.WARNING) - logging.getLogger("stevedore.extension").setLevel(logging.CRITICAL) - root = logging.getLogger() - root.setLevel(log_level) - root.addHandler(fh) - +from logger import configure_logging, empty_log configure_logging() import requests @@ -449,7 +414,7 @@ def emptylog(): authorize() ref = request.environ['HTTP_REFERER'] - fh.doRollover() + empty_log() logging.info('BAZARR Log file emptied') redirect(ref) diff --git a/views/logs.tpl b/views/logs.tpl index ea8fe473f..b8ec92f0a 100644 --- a/views/logs.tpl +++ b/views/logs.tpl @@ -34,25 +34,27 @@ %line = log.split('|') \\ %try: -{{line[2]}}\\ +{{line[3]}}\\ %except: \\ %end