zoukankan      html  css  js  c++  java
  • ANSI colored Python logging — Gist

    ANSI colored Python logging — Gist

    import logging
    from termcolor import colored
    class ColorLog(object):
        colormap = dict(
            debug=dict(color='grey', attrs=['bold']),
            info=dict(color='white'),
            warn=dict(color='yellow', attrs=['bold']),
            warning=dict(color='yellow', attrs=['bold']),
            error=dict(color='red'),
            critical=dict(color='red', attrs=['bold']),
        )
        def __init__(self, logger):
            self._log = logger
        def __getattr__(self, name):
            if name in ['debug', 'info', 'warn', 'warning', 'error', 'critical']:
                return lambda s, *args: getattr(self._log, name)(
                    colored(s, **self.colormap[name]), *args)
            return getattr(self._log, name)
    log = ColorLog(logging.getLogger(__name__))
    if __name__ == '__main__':
        log.setLevel(logging.DEBUG)
        stdout = logging.StreamHandler()
        stdout.setLevel(logging.DEBUG)
        log.addHandler(stdout)
        log.debug("booooring . . .")
        log.info("pleasing anecdote")
        log.warn("awkward utterance")
        log.error("drunken rudeness")
  • 相关阅读:
    算法-转
    单页 SEO-转
    浅谈MVVM设计模式
    iOS-UIView动画
    iOS 核心动画(下)
    iOS开发-核心动画(Core Animation)
    iOS-CALayer的介绍
    SVN Xcode不能提交.a文件
    iOS 毛玻璃效果
    Quartz2D学习总结
  • 原文地址:https://www.cnblogs.com/lexus/p/2504396.html
Copyright © 2011-2022 走看看