python logging.Formatter定制

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

上一篇:java中static、final、static final的区别


下一篇:我的Java问题集(1)(重点、难点、疑点)未完待续...