Commit 79ce12e0 authored by Vinay Sajip's avatar Vinay Sajip

Issue #20558: Improved implementation of error handling.

parent a4b9c878
......@@ -24,8 +24,18 @@ Copyright (C) 2001-2014 Vinay Sajip. All Rights Reserved.
To use, simply 'import logging' and log away!
"""
import sys, logging, logging.handlers, socket, struct, os, traceback, re
import types, cStringIO
import cStringIO
import errno
import io
import logging
import logging.handlers
import os
import re
import socket
import struct
import sys
import traceback
import types
try:
import thread
......@@ -38,10 +48,7 @@ from SocketServer import ThreadingTCPServer, StreamRequestHandler
DEFAULT_LOGGING_CONFIG_PORT = 9030
if sys.platform == "win32":
RESET_ERROR = 10054 #WSAECONNRESET
else:
RESET_ERROR = 104 #ECONNRESET
RESET_ERROR = errno.ECONNRESET
#
# The following code implements a socket listener for on-the-fly
......@@ -510,21 +517,21 @@ class DictConfigurator(BaseConfigurator):
level = handler_config.get('level', None)
if level:
handler.setLevel(logging._checkLevel(level))
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to configure handler '
'%r: %s' % (name, e))
loggers = config.get('loggers', EMPTY_DICT)
for name in loggers:
try:
self.configure_logger(name, loggers[name], True)
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to configure logger '
'%r: %s' % (name, e))
root = config.get('root', None)
if root:
try:
self.configure_root(root, True)
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to configure root '
'logger: %s' % e)
else:
......@@ -539,7 +546,7 @@ class DictConfigurator(BaseConfigurator):
try:
formatters[name] = self.configure_formatter(
formatters[name])
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to configure '
'formatter %r: %s' % (name, e))
# Next, do filters - they don't refer to anything else, either
......@@ -547,7 +554,7 @@ class DictConfigurator(BaseConfigurator):
for name in filters:
try:
filters[name] = self.configure_filter(filters[name])
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to configure '
'filter %r: %s' % (name, e))
......@@ -561,7 +568,7 @@ class DictConfigurator(BaseConfigurator):
handler = self.configure_handler(handlers[name])
handler.name = name
handlers[name] = handler
except StandardError, e:
except StandardError as e:
if 'target not configured yet' in str(e):
deferred.append(name)
else:
......@@ -574,7 +581,7 @@ class DictConfigurator(BaseConfigurator):
handler = self.configure_handler(handlers[name])
handler.name = name
handlers[name] = handler
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to configure handler '
'%r: %s' % (name, e))
......@@ -615,7 +622,7 @@ class DictConfigurator(BaseConfigurator):
existing.remove(name)
try:
self.configure_logger(name, loggers[name])
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to configure logger '
'%r: %s' % (name, e))
......@@ -638,7 +645,7 @@ class DictConfigurator(BaseConfigurator):
if root:
try:
self.configure_root(root)
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to configure root '
'logger: %s' % e)
finally:
......@@ -650,7 +657,7 @@ class DictConfigurator(BaseConfigurator):
factory = config['()'] # for use in exception handler
try:
result = self.configure_custom(config)
except TypeError, te:
except TypeError as te:
if "'format'" not in str(te):
raise
#Name of parameter changed from fmt to format.
......@@ -680,7 +687,7 @@ class DictConfigurator(BaseConfigurator):
for f in filters:
try:
filterer.addFilter(self.config['filters'][f])
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to add filter %r: %s' % (f, e))
def configure_handler(self, config):
......@@ -689,7 +696,7 @@ class DictConfigurator(BaseConfigurator):
if formatter:
try:
formatter = self.config['formatters'][formatter]
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to set formatter '
'%r: %s' % (formatter, e))
level = config.pop('level', None)
......@@ -711,7 +718,7 @@ class DictConfigurator(BaseConfigurator):
config['class'] = cname # restore for deferred configuration
raise StandardError('target not configured yet')
config['target'] = th
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to set target handler '
'%r: %s' % (config['target'], e))
elif issubclass(klass, logging.handlers.SMTPHandler) and\
......@@ -724,7 +731,7 @@ class DictConfigurator(BaseConfigurator):
kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
try:
result = factory(**kwargs)
except TypeError, te:
except TypeError as te:
if "'stream'" not in str(te):
raise
#The argument name changed from strm to stream
......@@ -746,7 +753,7 @@ class DictConfigurator(BaseConfigurator):
for h in handlers:
try:
logger.addHandler(self.config['handlers'][h])
except StandardError, e:
except StandardError as e:
raise ValueError('Unable to add handler %r: %s' % (h, e))
def common_logger_config(self, logger, config, incremental=False):
......@@ -841,13 +848,9 @@ def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
traceback.print_exc()
if self.server.ready:
self.server.ready.set()
except socket.error, e:
if not isinstance(e.args, tuple):
except socket.error as e:
if e.errno != RESET_ERROR:
raise
else:
errcode = e.args[0]
if errcode != RESET_ERROR:
raise
class ConfigSocketReceiver(ThreadingTCPServer):
"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment