python logger.exception_python – 很奇怪:logger只使用第一个处理程序的格式化程序来处理异常...

我正在目睹日志记录模块以一种有趣的方式表现.我错过了什么吗?

我正在做通常的事情,有两个处理程序:一个StreamHandler,用于仅记录INFO和更高的控制台,以及一个FileHandler,它也将处理所有的DEBUG信息.

它工作正常,直到我决定为exeptions提供不同的格式.我想在文件中有一个完整的堆栈跟踪,但只是控制台上的异常类型和值.由于处理程序具有setFormatter函数,并且因为编写logging.Formatter的子类似乎很容易,所以我认为它会起作用.

控制台处理程序和文件处理程序都有自己的格式化程序.代码中的print语句证明了这一点.但是,对logger.exception的调用只会使用第一个处理程序的formatException =>该文件中的异常将以其应具有的格式记录在控制台中.更改logger.addHandler行的顺序,然后是遍布各处的文件处理程序的formatException.

import logging

import sys

class ConsoleFormatter(logging.Formatter):

def formatException(self, exc_info):

# Ugly but obvious way to see who's talking.

return "CONSOLE EXCEPTION %s: %s" % exc_info[:2]

def setup(path):

logger = logging.getLogger()

#

file_handler = logging.FileHandler(path, mode='w')

if __debug__:

file_handler.setLevel(logging.DEBUG)

else:

file_handler.setLevel(logging.INFO)

formatter = logging.Formatter("%(asctime)s %(levelname)-8s "

"%(name)-16s %(message)s "

"[%(filename)s@%(lineno)d in %(funcName)s]")

file_handler.setFormatter(formatter)

#

console_handler = logging.StreamHandler(sys.stderr)

console_handler.setLevel(logging.INFO)

console_formatter = ConsoleFormatter("%(levelname)-8s - %(message)s")

console_handler.setFormatter(console_formatter)

# >>> FUN HAPPENS HERE <<<

# Only the formatter of the first handler is used ! Both on the console

# and in the file. Change the order of these two lines to see.

logger.addHandler(console_handler)

logger.addHandler(file_handler)

#

# Proof that the two handlers have different formatters:

print logger.handlers

print file_handler.formatter.formatException

print console_formatter.formatException

#

logger.setLevel(logging.DEBUG)

logger.info("Logger ready.")

setup('test.log')

logger = logging.getLogger()

logger.debug("Only visible in the file.")

try:

1/0

except ZeroDivisionError:

logger.exception("boom")

这是怎么回事 ?

编辑:我顺便使用python 2.6.

编辑:关于“console_formatter”变量名称更正的代码错字.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值