python logging.Formatter定制

时间:2014-07-02 08:44:51   收藏:0   阅读:432

需要实现在打印 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定制,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!