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
评论列表
文章目录