def make_colors(): """Colorize logging """ if __colored_logger: return from ostap.logger.colorized import set_with_colors set_with_colors(True) if not with_colors(): return def makeName(level, fg=None, bg=None, blink=False, underline=False, bgb=False, fgb=False): name = logging.getLevelName(level) bold = fg is None and bg is None and not uderline bold = True return colored_string(name, fg, bg, bold, blink, underline, fg_bright=fgb, bg_bright=bgb) from ostap.logger.colorized import RED, BLUE, YELLOW, GREEN, WHITE logging.addLevelName( logging.CRITICAL, makeName(logging.CRITICAL, fg=RED, bg=BLUE, blink=True)) logging.addLevelName( logging.WARNING, makeName(logging.WARNING, fg=RED, bg=YELLOW, underline=True, bgb=True)) logging.addLevelName( logging.ERROR, makeName(logging.ERROR, fg=YELLOW, bg=RED, blink=True, bgb=True, fgb=True)) logging.addLevelName(logging.INFO, makeName(logging.INFO, bg=BLUE, fg=WHITE)) logging.addLevelName(logging.DEBUG, makeName(logging.DEBUG, bg=GREEN, fg=WHITE)) logging.addLevelName( logging.VERBOSE, makeName(logging.VERBOSE, bg=YELLOW, fg=WHITE, bgb=True, fgb=True)) __colored_logger.append(1) return with_colors()
def emit(self, record): """Emit an ddecolorize the record """ lname = logging_levels.get(record.levelno, '') if not lname: lname = '%s' % record.levelno record.levelname = lname if with_colors(): record.msg = decolorize(record.msg) return logging.FileHandler.emit(self, record)
def reset_colors(): """Reset colorization of logging >>> reset_colors() """ for a in logging_levels: logging.addLevelName(a, logging_levels[a]) # while __colored_logger: __colored_logger.pop() from ostap.logger.colorized import set_with_colors set_with_colors(False) return with_colors()
def __exit__(self, *_): if self.with_color and not with_colors(): make_colors() elif with_colors() and not self.with_color: reset_colors()
def __enter__(self): self.with_color = with_colors() return self
def __enter__(self): self.with_color = with_colors() if self.color and not self.with_color: make_colors() elif self.with_color and not self.color: reset_colors() return self