【问题标题】:how to display exact line number (where the error has occured) in logging library如何在日志库中显示确切的行号(发生错误的位置)
【发布时间】:2016-07-30 06:22:42
【问题描述】:

在下面的代码中,错误出现在第 7 行。但是,日志在第 10 行显示错误,其中提到了日志记录。

import logging
import urllib2
logging.basicConfig(filename='example.log',level=logging.DEBUG,format='%(levelname) -10s %(asctime)s %(module)s:%(lineno)s %(funcName)s %(message)s')
def main():
    try:
        urls = "http://www.simplyhired.com/a/job-detaw/jobkey-67b4efe169eee7b2cf2ed47d49b1845070ea37/rid-racliggzfyjgqwfzrlvnqyjtcserhrri/cjp-3/pub_id-1002"
        site = urllib2.urlopen(urls).read()
        mathfail = 1/0
    except Exception, e:
        logging.critical(str(e))

main()

日志中显示如下:

CRITICAL   2016-04-08 15:28:47,063 testt:10 main HTTP Error 404: Not Found

我希望它显示发生错误的行号,而不是提到日志记录的行号。它应该显示第 7 行而不是第 10 行

【问题讨论】:

    标签: python exception logging exception-handling error-logging


    【解决方案1】:

    我认为您正在寻找堆栈跟踪

    import logging
    import urllib2
    import traceback
    logging.basicConfig(filename='example.log',level=logging.DEBUG,format='%(levelname) -10s %(asctime)s %(module)s:%(lineno)s %(funcName)s %(message)s')
    def main():
        try:
            urls = "http://www.simplyhired.com/a/job-detaw/jobkey-67b4efe169eee7b2cf2ed47d49b1845070ea37/rid-racliggzfyjgqwfzrlvnqyjtcserhrri/cjp-3/pub_id-1002"
            site = urllib2.urlopen(urls).read()
            mathfail = 1/0
        except Exception:
            logging.critical(traceback.format_exc())
    
    main()
    

    【讨论】:

    • 这不是我要找的。它必须是日志。
    • @dsl1990 你可以把它改成 logging.critical(traceback.format_exc())
    【解决方案2】:

    这给了我预期的输出:

    import logging
    import urllib2
    logging.basicConfig(filename='example.log',level=logging.DEBUG,format='%(levelname) -10s %(asctime)s %(module)s:%(lineno)s %(funcName)s %(message)s')
    def main():
        try:
            urls = "http://www.simplyhired.com/a/job-detaw/jobkey-67b4efe169eee7b2cf2ed47d49b1845070ea37/rid-racliggzfyjgqwfzrlvnqyjtcserhrri/cjp-3/pub_id-1002"
            site = urllib2.urlopen(urls).read()
            mathfail = 1/0
        except Exception, e:
            logging.exception(str(e))
    
    main()
    

    日志文件中的输出是:

    ERROR      2016-04-08 16:18:35,430 testt:10 main HTTP Error 404: Not Found
    Traceback (most recent call last):
      File "testt.py", line 7, in main
        site = urllib2.urlopen(urls).read()
      File "/home/deepu/anaconda/lib/python2.7/urllib2.py", line 154, in urlopen
        return opener.open(url, data, timeout)
      File "/home/deepu/anaconda/lib/python2.7/urllib2.py", line 437, in open
        response = meth(req, response)
      File "/home/deepu/anaconda/lib/python2.7/urllib2.py", line 550, in http_response
        'http', request, response, code, msg, hdrs)
      File "/home/deepu/anaconda/lib/python2.7/urllib2.py", line 475, in error
        return self._call_chain(*args)
      File "/home/deepu/anaconda/lib/python2.7/urllib2.py", line 409, in _call_chain
        result = func(*args)
      File "/home/deepu/anaconda/lib/python2.7/urllib2.py", line 558, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    HTTPError: HTTP Error 404: Not Found
    

    【讨论】:

      猜你喜欢
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多