首页 > 编程语言 > 详细

python logging.Formatter定制

时间:2014-07-02 08:44:28      阅读:491      评论:0      收藏:0      [点我收藏+]

需要实现在打印 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

python logging.Formatter定制

原文:http://blog.csdn.net/xiaoaiwhc/article/details/36205695

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!