简体   繁体   English

用于异常记录的Python记录器处理程序配置

[英]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.

相关问题 Python记录器在未捕获的异常后停止记录 - Python logger stops logging after uncaught exception 配置文件中的错误:处理程序 -Python 日志记录 - Error in config file : handler -Python logging Python日志记录 - 如何继承根记录器级别和处理程序 - Python Logging - How to inherit root logger level & handler 将自定义处理程序添加到没有记录器实例的Python记录模块中? - Adding custom handler to Python logging module without a logger instance? python日志yaml配置文件中的通用记录器名称 - universal logger name in python logging yaml config file 在日志记录配置文件中禁用特定 python 模块的记录器 - Disable logger of specific python module in logging config file Python日志记录-具有root记录器的多个模块中的配置文件 - Python logging - config file in multiple modules with root logger 如果 python 日志处理程序引发异常会发生什么? - What happens if a python logging handler raises an exception? 对使用配置文件的 python 日志记录感到困惑(仅记录在配置文件中为 root_logger 设置的内容) - Confused about python logging with config file (only logs what is set for root_logger in config file) python日志记录:记录器setLevel()是否未强制执行? - python logging: logger setLevel() is not enforced?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM