您的位置:首页 > 编程语言 > Python开发

python logging.Formatter定制

2014-07-01 20:02 183 查看
需要实现在打印 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.INFO 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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: