major pep8 cleanup.

funneled scope of imports where possible
fixed #59
Added fstrings where possible
added logging both temporarily and in secondary places
This commit is contained in:
Nicholas St. Germain 2018-12-17 19:12:37 -06:00
parent 47f2f61082
commit 61fcf3b80c
13 changed files with 131 additions and 121 deletions

View file

@ -1,10 +1,10 @@
import logging
from logging.handlers import RotatingFileHandler
from logging import Filter, DEBUG, INFO, getLogger, Formatter, StreamHandler
from varken.helpers import mkdir_p
class BlacklistFilter(logging.Filter):
class BlacklistFilter(Filter):
"""
Log filter for blacklisted tokens and passwords
"""
@ -13,7 +13,7 @@ class BlacklistFilter(logging.Filter):
max_files = 5
log_folder = 'logs'
blacklisted_strings = ['apikey', 'username', 'password']
blacklisted_strings = ['apikey', 'username', 'password', 'url']
def __init__(self, filteredstrings):
super().__init__()
@ -23,9 +23,9 @@ class BlacklistFilter(logging.Filter):
for item in self.filtered_strings:
try:
if item in record.msg:
record.msg = record.msg.replace(item, 8 * '*' + item[-2:])
record.msg = record.msg.replace(item, 8 * '*' + item[-5:])
if any(item in str(arg) for arg in record.args):
record.args = tuple(arg.replace(item, 8 * '*' + item[-2:]) if isinstance(arg, str) else arg
record.args = tuple(arg.replace(item, 8 * '*' + item[-5:]) if isinstance(arg, str) else arg
for arg in record.args)
except TypeError:
pass
@ -39,27 +39,25 @@ class VarkenLogger(object):
# Set log level
if self.log_level:
self.log_level = logging.DEBUG
self.log_level = DEBUG
else:
self.log_level = logging.INFO
self.log_level = INFO
# Make the log directory if it does not exist
mkdir_p('{}/{}'.format(self.data_folder, BlacklistFilter.log_folder))
# Create the Logger
self.logger = logging.getLogger()
self.logger.setLevel(logging.DEBUG)
self.logger = getLogger()
self.logger.setLevel(DEBUG)
# Create a Formatter for formatting the log messages
logger_formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(module)s : %(message)s',
'%Y-%m-%d %H:%M:%S')
logger_formatter = Formatter('%(asctime)s : %(levelname)s : %(module)s : %(message)s', '%Y-%m-%d %H:%M:%S')
# Create the Handler for logging data to a file
file_logger = RotatingFileHandler('{}/{}/{}'.format(self.data_folder, BlacklistFilter.log_folder,
BlacklistFilter.filename), mode='a',
maxBytes=BlacklistFilter.max_size, backupCount=BlacklistFilter.max_files,
encoding=None, delay=0)
file_logger = RotatingFileHandler(f'{self.data_folder}/{BlacklistFilter.log_folder}/{BlacklistFilter.filename}',
mode='a', maxBytes=BlacklistFilter.max_size, encoding=None, delay=0,
backupCount=BlacklistFilter.max_files)
file_logger.setLevel(self.log_level)
@ -67,7 +65,7 @@ class VarkenLogger(object):
file_logger.setFormatter(logger_formatter)
# Add the console logger
console_logger = logging.StreamHandler()
console_logger = StreamHandler()
console_logger.setFormatter(logger_formatter)
console_logger.setLevel(self.log_level)