You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
1015 B
29 lines
1015 B
4 years ago
|
""" 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)
|