需要实现在打印 WARN, ERROR, CRITICAL的log时显示函数/方法名和行号,在INFO级不显示
import logging def AltCustomFormatter(logging.Formatter): def __init__(self, fmt=None, datefmt=None): super(AltCustomFormatter, self).__init__(fmt, datefmt) def format(self, record): # 如果你添加了多个handler,你会发现我们的定制消息被重复了多次, # 我们在record里设置一个marker来避免 if record.levelno > logging.DEBUG and not hasattr(record, 'is_custom'): record.msg = "[%s, %s, %s] %s" % (record.filename, record.lineno, record.funcName, record.msg) record.is_custom = True return super(AltCustomFormatter, self).format(record)
python logging.Formatter定制
时间: 2024-10-10 08:48:21