[英]Python logger handler config for exception logging
I have written this piece of code to get namespace from xml document. 我已经编写了这段代码来从xml文档中获取名称空间。 I am trying to handle exception, and write full trace to the log.
我正在尝试处理异常,并将完整的跟踪记录写入日志。 however trace is not getting written with custom message in log (though i can see it on screen).
但是,跟踪没有在日志中写入自定义消息(尽管我可以在屏幕上看到它)。
I believe, i am missing somthing in Logger handler config. 我相信,我在Logger处理程序配置中缺少任何内容。 is there any specific configuration we need to deal with?
我们需要处理任何特定的配置吗? below is my logger config so far.
以下是到目前为止我的记录器配置。
Any help will be appreciated.! 任何帮助将不胜感激。!
logger = logging.getLogger(__name__)
hdlr = logging.FileHandler(r'C:\link.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
def get_ns(xmlroot):
"""Retrieve XML Document Namespace """
try:
logger.info("Trying to get XML namespace detail")
nsmap = xmlroot.nsmap.copy()
logger.info("Creating XML Namespace Object, {0}".format(nsmap))
nsmap['xmlns'] = nsmap.pop(None)
except (KeyError, SystemExit):
logging.exception("XML files does not contain namespace, Halting Program! ")
sys.exit()
else:
for ns in nsmap.values():
logger.info("Retrieved XML Namespace {0}".format(ns))
return ns
output on screen: 屏幕输出:
ERROR:root:XML files does not contain namespace, Halting Program!
Traceback (most recent call last):
File "C:\link.log", line 28, in get_ns
nsmap['xmlns'] = nsmap.pop(None)
KeyError: None
Change 更改
logging.exception("XML files does not contain namespace, Halting Program! ")
to 至
logger.exception("XML files does not contain namespace, Halting Program! ")
Since it's logger
that you have configured to write to file C:\\link.log
. 由于它是
logger
,因此您已配置为写入文件C:\\link.log
。
Using logging.exception
uses the "root logger", which outputs to the console by default. 使用
logging.exception
使用“ root logger”,默认情况下,它会输出到控制台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.