summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVinay Sajip <vinay_sajip@yahoo.co.uk>2014-08-03 19:48:23 +0100
committerVinay Sajip <vinay_sajip@yahoo.co.uk>2014-08-03 19:48:23 +0100
commit6ef0b4dce43399bd2f9e07a3d0070286a806c10d (patch)
treee4f65d6e995a36e990409b5a6c5d58211ab4b917
parent90f08944cabd6415545ffceee8cc5eceb9ba252e (diff)
downloadlogutils-git-6ef0b4dce43399bd2f9e07a3d0070286a806c10d.tar.gz
Made _checkLevel portable (though strictly not needed).
-rw-r--r--logutils/dictconfig.py40
1 files changed, 22 insertions, 18 deletions
diff --git a/logutils/dictconfig.py b/logutils/dictconfig.py
index 4a2281f..5f6c0ad 100644
--- a/logutils/dictconfig.py
+++ b/logutils/dictconfig.py
@@ -14,7 +14,7 @@ try:
StandardError
except NameError:
StandardError = Exception
-
+
IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)
def valid_ident(s):
@@ -33,9 +33,13 @@ except ImportError:
if isinstance(level, int):
rv = level
elif str(level) == level:
- if level not in logging._levelNames:
+ try:
+ levelnames = logging._levelNames
+ except AttributeError:
+ levelnames = logging._nameToLevel
+ if level not in levelnames:
raise ValueError('Unknown level: %r' % level)
- rv = logging._levelNames[level]
+ rv = levelnames[level]
else:
raise TypeError('Level not an integer or a '
'valid string: %r' % level)
@@ -64,7 +68,7 @@ class ConvertingDict(dict):
result.parent = self
result.key = key
return result
-
+
def get(self, key, default=None):
value = dict.get(self, key, default)
result = self.configurator.convert(value)
@@ -76,7 +80,7 @@ class ConvertingDict(dict):
result.parent = self
result.key = key
return result
-
+
def pop(self, key, default=None):
value = dict.pop(self, key, default)
result = self.configurator.convert(value)
@@ -126,7 +130,7 @@ class BaseConfigurator(object):
"""
The configurator base class which defines some useful defaults.
"""
-
+
CONVERT_PATTERN = re.compile(r'^(?P<prefix>[a-z]+)://(?P<suffix>.*)$')
WORD_PATTERN = re.compile(r'^\s*(\w+)\s*')
@@ -177,7 +181,7 @@ class BaseConfigurator(object):
def ext_convert(self, value):
"""Default converter for the ext:// protocol."""
return self.resolve(value)
-
+
def cfg_convert(self, value):
"""Default converter for the cfg:// protocol."""
rest = value
@@ -239,7 +243,7 @@ class BaseConfigurator(object):
converter = getattr(self, converter)
value = converter(suffix)
return value
-
+
def configure_custom(self, config):
"""Configure an object with a user-supplied factory."""
c = config.pop('()')
@@ -269,7 +273,7 @@ def named_handlers_supported():
else:
result = (major > 3)
return result
-
+
class DictConfigurator(BaseConfigurator):
"""
Configure logging using a dictionary-like object to describe the
@@ -326,10 +330,10 @@ class DictConfigurator(BaseConfigurator):
'logger: %s' % e)
else:
disable_existing = config.pop('disable_existing_loggers', True)
-
+
logging._handlers.clear()
del logging._handlerList[:]
-
+
# Do formatters first - they don't refer to anything else
formatters = config.get('formatters', EMPTY_DICT)
for name in formatters:
@@ -364,7 +368,7 @@ class DictConfigurator(BaseConfigurator):
raise ValueError('Unable to configure handler '
'%r: %s' % (name, e))
# Next, do loggers - they refer to handlers and filters
-
+
#we don't want to lose the existing loggers,
#since other threads may have pointers to them.
#existing is set to contain all existing loggers,
@@ -402,7 +406,7 @@ class DictConfigurator(BaseConfigurator):
e = sys.exc_info()[1]
raise ValueError('Unable to configure logger '
'%r: %s' % (name, e))
-
+
#Disable any old loggers. There's no point deleting
#them as other threads may continue to hold references
#and by disabling them, you stop them doing any logging.
@@ -416,12 +420,12 @@ class DictConfigurator(BaseConfigurator):
logger.propagate = True
elif disable_existing:
logger.disabled = True
-
+
# And finally, do the root logger
root = config.get('root', None)
if root:
try:
- self.configure_root(root)
+ self.configure_root(root)
except StandardError:
e = sys.exc_info()[1]
raise ValueError('Unable to configure root '
@@ -451,7 +455,7 @@ class DictConfigurator(BaseConfigurator):
dfmt = config.get('datefmt', None)
result = logging.Formatter(fmt, dfmt)
return result
-
+
def configure_filter(self, config):
"""Configure a filter from a dictionary."""
if '()' in config:
@@ -552,7 +556,7 @@ class DictConfigurator(BaseConfigurator):
filters = config.get('filters', None)
if filters:
self.add_filters(logger, filters)
-
+
def configure_logger(self, name, config, incremental=False):
"""Configure a non-root logger from a dictionary."""
logger = logging.getLogger(name)
@@ -560,7 +564,7 @@ class DictConfigurator(BaseConfigurator):
propagate = config.get('propagate', None)
if propagate is not None:
logger.propagate = propagate
-
+
def configure_root(self, config, incremental=False):
"""Configure a root logger from a dictionary."""
root = logging.getLogger()