Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, header, **kwargs):
super(FormatterWithHeader, self).__init__(**kwargs)
self.header = header
# Override the normal format method
self.format = self.first_line_format
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
log_file_name = os.path.join(log_dir, log_name)
file_handler = logging.FileHandler(log_file_name, mode='a')
file_handler.setLevel(logging.DEBUG)
fmt = '%(levelname)s{delimiter}%(asctime)s{delimiter}%(message)s'.format(delimiter=delimiter)
if header:
header = header if header.startswith("#") else "#" + header
header += "\n" + delimiter.join(names)
formatter = FormatterWithHeader(header, fmt=fmt, datefmt='%m/%d/%Y %H:%M:%S')
else:
formatter = FormatterWithHeader(delimiter.join(names), fmt=fmt, datefmt='%m/%d/%Y %H:%M:%S')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
cls.logger = logger
run_id = time.strftime('%Y%m%d_%H:%M', time.localtime())
log_name = "WD_bot_run-{}.log".format(run_id)
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
log_file_name = os.path.join(log_dir, log_name)
file_handler = logging.FileHandler(log_file_name, mode='a')
file_handler.setLevel(logging.DEBUG)
fmt = '%(levelname)s{delimiter}%(asctime)s{delimiter}%(message)s'.format(delimiter=delimiter)
if header:
header = header if header.startswith("#") else "#" + header
header += "\n" + delimiter.join(names)
formatter = FormatterWithHeader(header, fmt=fmt, datefmt='%m/%d/%Y %H:%M:%S')
else:
formatter = FormatterWithHeader(delimiter.join(names), fmt=fmt, datefmt='%m/%d/%Y %H:%M:%S')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
cls.logger = logger
if not log_name:
run_id = time.strftime('%Y%m%d_%H:%M', time.localtime())
log_name = "WD_bot_run-{}.log".format(run_id)
logger = logging.getLogger('WD_logger')
logger.setLevel(logging.DEBUG)
log_file_name = os.path.join(log_dir, log_name)
file_handler = logging.FileHandler(log_file_name, mode='a')
file_handler.setLevel(logging.DEBUG)
fmt = '%(levelname)s{delimiter}%(asctime)s{delimiter}%(message)s'.format(delimiter=delimiter)
if header:
header = header if header.startswith("#") else "#" + header
formatter = FormatterWithHeader(header, fmt=fmt, datefmt='%m/%d/%Y %H:%M:%S')
else:
formatter = logging.Formatter(fmt=fmt, datefmt='%m/%d/%Y %H:%M:%S')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
cls.logger = logger
def first_line_format(self, record):
# First time in, switch back to the normal format function
self.format = super(FormatterWithHeader, self).format
return self.header + "\n" + self.format(record)