Add IP filtering for logging

This commit is contained in:
Halali 2018-11-10 19:53:27 +01:00
parent cb1ea10add
commit ed50acd7cc
2 changed files with 69 additions and 20 deletions

View File

@ -7,6 +7,8 @@ from logging.handlers import TimedRotatingFileHandler
from get_argv import config_dir
from get_settings import get_general_settings
logger = logging.getLogger()
debug = get_general_settings()[4]
if debug is False:
log_level = "INFO"
@ -38,9 +40,21 @@ class NoExceptionFormatter(logging.Formatter):
def configure_logging():
log = logging.getLogger()
log.handlers = []
logger.handlers = []
logger.setLevel(log_level)
# Console logging
ch = logging.StreamHandler()
cf = NoExceptionFormatter('%(asctime)-15s - %(name)-32s (%(thread)x) : %(levelname)s (%(module)s:%(lineno)d) '
'- %(message)s')
ch.setFormatter(cf)
ch.setLevel(log_level)
# ch.addFilter(MyFilter())
logger.addHandler(ch)
#File Logging
global fh
fh = TimedRotatingFileHandler(os.path.join(config_dir, 'log/bazarr.log'), when="midnight", interval=1,
backupCount=7)
@ -48,6 +62,7 @@ def configure_logging():
'%d/%m/%Y %H:%M:%S')
fh.setFormatter(f)
fh.addFilter(BlacklistFilter())
fh.addFilter(PublicIPFilter())
if debug is True:
logging.getLogger("apscheduler").setLevel(logging.DEBUG)
@ -62,23 +77,18 @@ def configure_logging():
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)
fh.setLevel(log_level)
logger.addHandler(fh)
# Console logging
ch = logging.StreamHandler()
cf = NoExceptionFormatter('%(asctime)s - %(levelname)s :: %(message)s',
'%Y-%m-%d %H:%M:%S')
ch.setFormatter(cf)
class MyFilter(logging.Filter):
def __init__(self):
pass
logging.getLogger("apscheduler").setLevel(logging.WARNING)
logging.getLogger("subliminal").setLevel(logging.CRITICAL)
logging.getLogger("enzyme").setLevel(logging.CRITICAL)
logging.getLogger("guessit").setLevel(logging.WARNING)
logging.getLogger("rebulk").setLevel(logging.WARNING)
logging.getLogger("stevedore.extension").setLevel(logging.CRITICAL)
ch.setLevel(logging.INFO)
log.addHandler(ch)
def filter(self, record):
if record.name != 'root':
return False
return True
class BlacklistFilter(logging.Filter):
@ -106,6 +116,43 @@ class BlacklistFilter(logging.Filter):
return True
class PublicIPFilter(logging.Filter):
"""
Log filter for public IP addresses
"""
def __init__(self):
pass
def filter(self, record):
try:
# Currently only checking for ipv4 addresses
ipv4 = re.findall(r'[0-9]+(?:\.[0-9]+){3}(?!\d*-[a-z0-9]{6})', record.msg)
for ip in ipv4:
record.msg = record.msg.replace(ip, ip.partition('.')[0] + '.***.***.***')
args = []
for arg in record.args:
ipv4 = re.findall(r'[0-9]+(?:\.[0-9]+){3}(?!\d*-[a-z0-9]{6})', arg) if isinstance(arg, basestring) else []
for ip in ipv4:
arg = arg.replace(ip, ip.partition('.')[0] + '.***.***.***')
args.append(arg)
record.args = tuple(args)
except:
pass
return True
def empty_log():
fh.doRollover()
def update_settings(debug):
if debug == 'False':
level = "INFO"
else:
level = "DEBUG"
print debug, level
logging.getLogger().setLevel(level)
for handler in logging.getLogger().handlers:
handler.setLevel(level)

View File

@ -23,7 +23,7 @@ update_notifier()
from get_settings import get_general_settings, get_proxy_settings
import logging
from logger import configure_logging, empty_log
from logger import configure_logging, empty_log, update_settings
configure_logging()
import requests
@ -1100,8 +1100,8 @@ def save_settings():
settings_general = get_general_settings()
before = (unicode(settings_general[0]), int(settings_general[1]), unicode(settings_general[2]), unicode(settings_general[4]), unicode(settings_general[3]), unicode(settings_general[12]), unicode(settings_general[13]), unicode(settings_general[14]))
after = (unicode(settings_general_ip), int(settings_general_port), unicode(settings_general_baseurl), unicode(settings_general_debug), unicode(settings_general_pathmapping), unicode(settings_general_use_sonarr), unicode(settings_general_use_radarr), unicode(settings_general_pathmapping_movie))
before = (unicode(settings_general[0]), int(settings_general[1]), unicode(settings_general[2]), unicode(settings_general[3]), unicode(settings_general[12]), unicode(settings_general[13]), unicode(settings_general[14]))
after = (unicode(settings_general_ip), int(settings_general_port), unicode(settings_general_baseurl), unicode(settings_general_pathmapping), unicode(settings_general_use_sonarr), unicode(settings_general_use_radarr), unicode(settings_general_pathmapping_movie))
from six import text_type
cfg = ConfigParser()
@ -1129,6 +1129,8 @@ def save_settings():
cfg.set('general', 'use_embedded_subs', text_type(settings_general_embedded))
cfg.set('general', 'only_monitored', text_type(settings_general_only_monitored))
cfg.set('general', 'adaptive_searching', text_type(settings_general_adaptive_searching))
update_settings(settings_general_debug)
if after != before:
configured()