为什么添加日志记录会改变处理异常的方式?

时间:2015-07-15 15:21:02

标签: python exception logging exception-handling

注意:解决问题之后,根据我对答案的最后评论:回溯不是未处理的异常,它们是从urllib3记录消息,所以我的最后一个问题是无效的( =一切都被正确捕获了)

我通过发送availability of an Elasticsearch server请求并捕获异常来测试.info()。以下代码是

 cd ~               # home directory
 ssh-keygen -t rsa  # Press enter for all values

import elasticsearch ### removing the code from here to a marker further down (basically the logging part makes the handling work fine import os import logging import logging.handlers import logging.config import tempfile import random import arrow from pythonjsonlogger import jsonlogger class CustomLogger(): """ whereform: usually __name__ = the name of the script index: th eindex to log to, ideally a group of activities (nessus for instance) type: an explicit type to log to, ideally an activity withon the group above """ # the start date of the logging, a common date for all handlers startdate = arrow.now('local').format('YYYY_MM_DD__HH_mm_ss') def __init__(self): # what LogRecords we want, in which order rec = ['asctime', 'created', 'filename', 'funcName', 'lineno', 'processName', 'levelname', 'message'] # initializing the logging module, getting all information from DEBUG up # the handler is randomized to have different names self.rootLogger = logging.getLogger(str(random.random())) self.rootLogger.setLevel(logging.DEBUG) # console consolefmt = logging.Formatter( " | ".join(["%({0:s})s".format(i) for i in rec]) ) consoleHandler = logging.StreamHandler() consoleHandler.setFormatter(consolefmt) consoleHandler.setLevel(logging.DEBUG) self.rootLogger.addHandler(consoleHandler) # json file jsonfmt = jsonlogger.JsonFormatter( " | ".join(["%({0:s})s".format(i) for i in rec]) ) #jsonfmt = "{'timestamp': '%(created)f'," + ','.join(["'{0:s}': %({0:s})s".format(i) for i in rec]) + "}" logdir = "/var/log/GetNessusScans/{date}/".format(date=CustomLogger.startdate) try: os.makedirs(logdir) except: # the directory exists already pass with tempfile.NamedTemporaryFile( mode='w', delete=False, prefix=logdir, suffix=".log" ) as f: logfilename = f.name # make it r by everyone #os.chmod(f.name, 0666) fileHandler = logging.FileHandler(logfilename) fileHandler.setFormatter(jsonfmt) fileHandler.setLevel(logging.DEBUG) self.rootLogger.addHandler(fileHandler) log = CustomLogger().rootLogger ### end of suspicious code, removing everything from this line up removes the problem import elasticsearch # initialize dataabase - the host does not exist for the purpose of the example es = elasticsearch.Elasticsearch(hosts="http://elk.example:9200/") try: info = es.info() except Exception as e: print("ELK is not available: {e}".format(e=e)) 调用实际上是REST API的包装器,调用最终由.info()处理。

这与下面的追溯一起崩溃。如果我删除注释的部分,处理就可以了。因此,日志部分确实引入了这个问题。请注意,最后一行是urllib3中的print

except

看起来呼叫已经多次尝试并且(正确地)在联系服务器时失败了。但是,如何添加日志记录会改变处理异常的方式?

另外:为什么其他异常不由我的GET http://elk.example.com:9200/ [status:N/A request:0.210s] Traceback (most recent call last): File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 544, in urlopen body=body, headers=headers) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 349, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Python34\lib\http\client.py", line 1088, in request self._send_request(method, url, body, headers) File "C:\Python34\lib\http\client.py", line 1126, in _send_request self.endheaders(body) File "C:\Python34\lib\http\client.py", line 1084, in endheaders self._send_output(message_body) File "C:\Python34\lib\http\client.py", line 922, in _send_output self.send(msg) File "C:\Python34\lib\http\client.py", line 857, in send self.connect() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 155, in connect conn = self._new_conn() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 134, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "C:\Python34\lib\site-packages\urllib3\util\connection.py", line 64, in create_connection for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python34\lib\socket.py", line 533, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python34\lib\site-packages\elasticsearch\connection\http_urllib3.py", line 74, in perform_request response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 597, in urlopen _stacktrace=sys.exc_info()[2]) File "C:\Python34\lib\site-packages\urllib3\util\retry.py", line 222, in increment raise six.reraise(type(error), error, _stacktrace) File "C:\Python34\lib\site-packages\urllib3\packages\six.py", line 309, in reraise raise value.with_traceback(tb) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 544, in urlopen body=body, headers=headers) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 349, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Python34\lib\http\client.py", line 1088, in request self._send_request(method, url, body, headers) File "C:\Python34\lib\http\client.py", line 1126, in _send_request self.endheaders(body) File "C:\Python34\lib\http\client.py", line 1084, in endheaders self._send_output(message_body) File "C:\Python34\lib\http\client.py", line 922, in _send_output self.send(msg) File "C:\Python34\lib\http\client.py", line 857, in send self.connect() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 155, in connect conn = self._new_conn() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 134, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "C:\Python34\lib\site-packages\urllib3\util\connection.py", line 64, in create_connection for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python34\lib\socket.py", line 533, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): urllib3.exceptions.ProtocolError: ('Connection aborted.', gaierror(11001, 'getaddrinfo failed')) GET http://elk.example.com:9200/ [status:N/A request:0.001s] Traceback (most recent call last): File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 544, in urlopen body=body, headers=headers) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 349, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Python34\lib\http\client.py", line 1088, in request self._send_request(method, url, body, headers) File "C:\Python34\lib\http\client.py", line 1126, in _send_request self.endheaders(body) File "C:\Python34\lib\http\client.py", line 1084, in endheaders self._send_output(message_body) File "C:\Python34\lib\http\client.py", line 922, in _send_output self.send(msg) File "C:\Python34\lib\http\client.py", line 857, in send self.connect() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 155, in connect conn = self._new_conn() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 134, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "C:\Python34\lib\site-packages\urllib3\util\connection.py", line 64, in create_connection for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python34\lib\socket.py", line 533, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python34\lib\site-packages\elasticsearch\connection\http_urllib3.py", line 74, in perform_request response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 597, in urlopen _stacktrace=sys.exc_info()[2]) File "C:\Python34\lib\site-packages\urllib3\util\retry.py", line 222, in increment raise six.reraise(type(error), error, _stacktrace) File "C:\Python34\lib\site-packages\urllib3\packages\six.py", line 309, in reraise raise value.with_traceback(tb) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 544, in urlopen body=body, headers=headers) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 349, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Python34\lib\http\client.py", line 1088, in request self._send_request(method, url, body, headers) File "C:\Python34\lib\http\client.py", line 1126, in _send_request self.endheaders(body) File "C:\Python34\lib\http\client.py", line 1084, in endheaders self._send_output(message_body) File "C:\Python34\lib\http\client.py", line 922, in _send_output self.send(msg) File "C:\Python34\lib\http\client.py", line 857, in send self.connect() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 155, in connect conn = self._new_conn() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 134, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "C:\Python34\lib\site-packages\urllib3\util\connection.py", line 64, in create_connection for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python34\lib\socket.py", line 533, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): urllib3.exceptions.ProtocolError: ('Connection aborted.', gaierror(11001, 'getaddrinfo failed')) GET http://elk.example.com:9200/ [status:N/A request:0.001s] Traceback (most recent call last): File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 544, in urlopen body=body, headers=headers) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 349, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Python34\lib\http\client.py", line 1088, in request self._send_request(method, url, body, headers) File "C:\Python34\lib\http\client.py", line 1126, in _send_request self.endheaders(body) File "C:\Python34\lib\http\client.py", line 1084, in endheaders self._send_output(message_body) File "C:\Python34\lib\http\client.py", line 922, in _send_output self.send(msg) File "C:\Python34\lib\http\client.py", line 857, in send self.connect() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 155, in connect conn = self._new_conn() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 134, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "C:\Python34\lib\site-packages\urllib3\util\connection.py", line 64, in create_connection for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python34\lib\socket.py", line 533, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python34\lib\site-packages\elasticsearch\connection\http_urllib3.py", line 74, in perform_request response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 597, in urlopen _stacktrace=sys.exc_info()[2]) File "C:\Python34\lib\site-packages\urllib3\util\retry.py", line 222, in increment raise six.reraise(type(error), error, _stacktrace) File "C:\Python34\lib\site-packages\urllib3\packages\six.py", line 309, in reraise raise value.with_traceback(tb) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 544, in urlopen body=body, headers=headers) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 349, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Python34\lib\http\client.py", line 1088, in request self._send_request(method, url, body, headers) File "C:\Python34\lib\http\client.py", line 1126, in _send_request self.endheaders(body) File "C:\Python34\lib\http\client.py", line 1084, in endheaders self._send_output(message_body) File "C:\Python34\lib\http\client.py", line 922, in _send_output self.send(msg) File "C:\Python34\lib\http\client.py", line 857, in send self.connect() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 155, in connect conn = self._new_conn() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 134, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "C:\Python34\lib\site-packages\urllib3\util\connection.py", line 64, in create_connection for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python34\lib\socket.py", line 533, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): urllib3.exceptions.ProtocolError: ('Connection aborted.', gaierror(11001, 'getaddrinfo failed')) GET http://elk.example.com:9200/ [status:N/A request:0.000s] Traceback (most recent call last): File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 544, in urlopen body=body, headers=headers) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 349, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Python34\lib\http\client.py", line 1088, in request self._send_request(method, url, body, headers) File "C:\Python34\lib\http\client.py", line 1126, in _send_request self.endheaders(body) File "C:\Python34\lib\http\client.py", line 1084, in endheaders self._send_output(message_body) File "C:\Python34\lib\http\client.py", line 922, in _send_output self.send(msg) File "C:\Python34\lib\http\client.py", line 857, in send self.connect() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 155, in connect conn = self._new_conn() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 134, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "C:\Python34\lib\site-packages\urllib3\util\connection.py", line 64, in create_connection for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python34\lib\socket.py", line 533, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python34\lib\site-packages\elasticsearch\connection\http_urllib3.py", line 74, in perform_request response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 597, in urlopen _stacktrace=sys.exc_info()[2]) File "C:\Python34\lib\site-packages\urllib3\util\retry.py", line 222, in increment raise six.reraise(type(error), error, _stacktrace) File "C:\Python34\lib\site-packages\urllib3\packages\six.py", line 309, in reraise raise value.with_traceback(tb) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 544, in urlopen body=body, headers=headers) File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 349, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Python34\lib\http\client.py", line 1088, in request self._send_request(method, url, body, headers) File "C:\Python34\lib\http\client.py", line 1126, in _send_request self.endheaders(body) File "C:\Python34\lib\http\client.py", line 1084, in endheaders self._send_output(message_body) File "C:\Python34\lib\http\client.py", line 922, in _send_output self.send(msg) File "C:\Python34\lib\http\client.py", line 857, in send self.connect() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 155, in connect conn = self._new_conn() File "C:\Python34\lib\site-packages\urllib3\connection.py", line 134, in _new_conn (self.host, self.port), self.timeout, **extra_kw) File "C:\Python34\lib\site-packages\urllib3\util\connection.py", line 64, in create_connection for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): File "C:\Python34\lib\socket.py", line 533, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): urllib3.exceptions.ProtocolError: ('Connection aborted.', gaierror(11001, 'getaddrinfo failed')) ELK is not available: ConnectionError(('Connection aborted.', gaierror(11001, 'getaddrinfo failed'))) caused by: ProtocolError(('Connection aborted.', gaierror(11001, 'getaddrinfo failed'))) 子句处理?(参见开头的注释)

评论1:关注@Anand S Kumar的评论,我重写了代码(并更改了问题标题)以包含日志记录。它通常结构更好,我从我的模块中撕掉相关部分来重现问题

评论2:这个问题是可能不是特定于Elasticsearch,细节只是给出了上下文(这解释了为什么没有 elasticsearch 标签)

1 个答案:

答案 0 :(得分:0)

问题似乎是您为所有记录器设置了Debug级别的记录器,这导致urllib3记录异常。您应该尝试将requestsurllib3的日志记录级别设置为警告甚至更高,以禁止来自urllib3的邮件。

示例 -

logging.getLogger("requests").setLevel(logging.WARNING)

或者

 logging.getLogger("urllib3").setLevel(logging.WARNING)
相关问题