"""Nicer logger."""
import logging
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
logging.STRUCTURE = 35 # "STRUCTURE" log level
logging.SUCCESS = 39
logging.addLevelName(logging.STRUCTURE, "STRUCTURE")
logging.addLevelName(logging.SUCCESS, "SUCCESS")
# The background is set with 40 plus the number of the color, and the foreground with 30
# These are the sequences need to get colored ouput
RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[1;%dm"
BOLD_SEQ = "\033[1m"
COLORS = {
"WARNING": YELLOW,
"INFO": WHITE,
"DEBUG": BLUE,
"CRITICAL": YELLOW,
"ERROR": RED,
"SUCCESS": CYAN,
}
[docs]
class ColoredLogger(logging.Logger):
FORMAT = "$BOLD$RESET%(levelname)-18s| %(message)s"
COLOR_FORMAT = formatter_message(FORMAT, True)
[docs]
def __init__(self, name):
logging.Logger.__init__(self, name, logging.WARNING)
color_formatter = ColoredFormatter(self.COLOR_FORMAT)
console = logging.StreamHandler()
console.setFormatter(color_formatter)
self.errored = 0
self.addHandler(console)
return
def _structure(self, msg, *args, **kwargs):
if self.isEnabledFor(logging.STRUCTURE):
self._log(logging.STRUCTURE, msg, args, **kwargs)
[docs]
def success(self, msg, *args, **kwargs):
self.errored -= 1
if self.isEnabledFor(logging.SUCCESS):
self._log(logging.SUCCESS, msg, args, **kwargs)
[docs]
def error(self, msg, *args, **kwargs):
self.errored += 1
if self.isEnabledFor(logging.ERROR):
self._log(logging.ERROR, msg, args, **kwargs)
logging.setLoggerClass(ColoredLogger)
logger = logging.getLogger("edges-io")
logger.setLevel(logging.WARNING)
logging.setLoggerClass(logging.Logger)