[ Python ] logging level name 추가 및 color 넣기

2019. 9. 29. 00:42분석 Python/구현 및 자료

우연히 다른 거 찾다가, log에 색깔을 먹인다길래, 신기해서 해보니, 해보고 공유ㅎ

log도 set level도 예전에 했던 방식보다 더 쉽게 할 수 있는 것도 있었다.

colorlog-4.0.2.tar.gz
0.03MB

import logging
from colorlog import ColoredFormatter

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

formatter = ColoredFormatter(
    "%(log_color)s[%(asctime)s] %(message)s",
    datefmt=None,
    reset=True,
    log_colors={
        'DEBUG':    'cyan',
        'INFO':     'white,bold',
        'INFOV':    'cyan,bold',
        'WARNING':  'yellow',
        'ERROR':    'red,bold',
        'CRITICAL': 'red,bg_white',
    },
    secondary_log_colors={},
    style='%'
)
ch.setFormatter(formatter)

log = logging.getLogger('attcap')
log.setLevel(logging.DEBUG)
log.handlers = []       # No duplicated handlers
log.propagate = False   # workaround for duplicated logs in ipython
log.addHandler(ch)

logging.addLevelName(logging.INFO + 1, 'INFOV')


def _infov(self, msg, *args, **kwargs):
    self.log(logging.INFO + 1, msg, *args, **kwargs)

logging.Logger.infov = _infov

https://github.com/gitlimlab/MAML-tf/blob/master/loginfo.py

 

gitlimlab/MAML-tf

Tensorflow Implementation of MAML. Contribute to gitlimlab/MAML-tf development by creating an account on GitHub.

github.com

 

728x90