tools.py 文件源码

python
阅读 39 收藏 0 点赞 0 评论 0

项目:sc-controller 作者: kozec 项目源码 文件源码
def init_logging(prefix="", suffix=""):
    """
    Initializes logging, sets custom logging format and adds one
    logging level with name and method to call.

    prefix and suffix arguments can be used to modify log level prefixes.
    """
    logging.basicConfig(format=LOG_FORMAT)
    logger = logging.getLogger()
    # Rename levels
    logging.addLevelName(10, prefix + "D" + suffix) # Debug
    logging.addLevelName(20, prefix + "I" + suffix) # Info
    logging.addLevelName(30, prefix + "W" + suffix) # Warning
    logging.addLevelName(40, prefix + "E" + suffix) # Error
    # Create additional, "verbose" level
    logging.addLevelName(15, prefix + "V" + suffix) # Verbose
    # Add 'logging.verbose' method
    def verbose(self, msg, *args, **kwargs):
        return self.log(15, msg, *args, **kwargs)
    logging.Logger.verbose = verbose
    # Wrap Logger._log in something that can handle utf-8 exceptions
    old_log = logging.Logger._log
    def _log(self, level, msg, args, exc_info=None, extra=None):
        args = tuple([
            (str(c).decode("utf-8") if type(c) is str else c)
            for c in args
        ])
        msg = msg if type(msg) is unicode else str(msg).decode("utf-8")
        old_log(self, level, msg, args, exc_info, extra)
    logging.Logger._log = _log
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号