""" Logging helpers | |
Hacked together by / Copyright 2020 Ross Wightman | |
""" | |
import logging | |
import logging.handlers | |
class FormatterNoInfo(logging.Formatter): | |
def __init__(self, fmt='%(levelname)s: %(message)s'): | |
logging.Formatter.__init__(self, fmt) | |
def format(self, record): | |
if record.levelno == logging.INFO: | |
return str(record.getMessage()) | |
return logging.Formatter.format(self, record) | |
def setup_default_logging(default_level=logging.INFO, log_path=''): | |
console_handler = logging.StreamHandler() | |
console_handler.setFormatter(FormatterNoInfo()) | |
logging.root.addHandler(console_handler) | |
logging.root.setLevel(default_level) | |
if log_path: | |
file_handler = logging.handlers.RotatingFileHandler(log_path, maxBytes=(1024 ** 2 * 2), backupCount=3) | |
file_formatter = logging.Formatter("%(asctime)s - %(name)20s: [%(levelname)8s] - %(message)s") | |
file_handler.setFormatter(file_formatter) | |
logging.root.addHandler(file_handler) | |