1 # This is a copy of the Python logging.config.dictconfig module.
2 # It is provided here for backwards compatibility for Python versions
5 # Copyright 2009-2010 by Vinay Sajip. All Rights Reserved.
7 # Permission to use, copy, modify, and distribute this software and its
8 # documentation for any purpose and without fee is hereby granted,
9 # provided that the above copyright notice appear in all copies and that
10 # both that copyright notice and this permission notice appear in
11 # supporting documentation, and that the name of Vinay Sajip
12 # not be used in advertising or publicity pertaining to distribution
13 # of the software without specific, written prior permission.
14 # VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
16 # VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17 # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
18 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 import logging.handlers
26 IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)
29 m = IDENTIFIER.match(s)
31 raise ValueError('Not a valid Python identifier: %r' % s)
35 # This function is defined in logging only in recent versions of Python
38 from logging import _checkLevel
40 def _checkLevel(level):
41 if isinstance(level, int):
43 elif str(level) == level:
44 if level not in logging._levelNames:
45 raise ValueError('Unknown level: %r' % level)
46 rv = logging._levelNames[level]
48 raise TypeError('Level not an integer or a '
49 'valid string: %r' % level)
52 # The ConvertingXXX classes are wrappers around standard Python containers,
53 # and they serve to convert any suitable values in the container. The
54 # conversion converts base dicts, lists and tuples to their wrapped
55 # equivalents, whereas strings which match a conversion format are converted
58 # Each wrapper should have a configurator attribute holding the actual
59 # configurator to use for conversion.
61 class ConvertingDict(dict):
62 """A converting dictionary wrapper."""
64 def __getitem__(self, key):
65 value = dict.__getitem__(self, key)
66 result = self.configurator.convert(value)
67 #If the converted value is different, save for next time
68 if value is not result:
70 if type(result) in (ConvertingDict, ConvertingList,
76 def get(self, key, default=None):
77 value = dict.get(self, key, default)
78 result = self.configurator.convert(value)
79 #If the converted value is different, save for next time
80 if value is not result:
82 if type(result) in (ConvertingDict, ConvertingList,
88 def pop(self, key, default=None):
89 value = dict.pop(self, key, default)
90 result = self.configurator.convert(value)
91 if value is not result:
92 if type(result) in (ConvertingDict, ConvertingList,
98 class ConvertingList(list):
99 """A converting list wrapper."""
100 def __getitem__(self, key):
101 value = list.__getitem__(self, key)
102 result = self.configurator.convert(value)
103 #If the converted value is different, save for next time
104 if value is not result:
106 if type(result) in (ConvertingDict, ConvertingList,
112 def pop(self, idx=-1):
113 value = list.pop(self, idx)
114 result = self.configurator.convert(value)
115 if value is not result:
116 if type(result) in (ConvertingDict, ConvertingList,
121 class ConvertingTuple(tuple):
122 """A converting tuple wrapper."""
123 def __getitem__(self, key):
124 value = tuple.__getitem__(self, key)
125 result = self.configurator.convert(value)
126 if value is not result:
127 if type(result) in (ConvertingDict, ConvertingList,
133 class BaseConfigurator(object):
135 The configurator base class which defines some useful defaults.
138 CONVERT_PATTERN = re.compile(r'^(?P<prefix>[a-z]+)://(?P<suffix>.*)$')
140 WORD_PATTERN = re.compile(r'^\s*(\w+)\s*')
141 DOT_PATTERN = re.compile(r'^\.\s*(\w+)\s*')
142 INDEX_PATTERN = re.compile(r'^\[\s*(\w+)\s*\]\s*')
143 DIGIT_PATTERN = re.compile(r'^\d+$')
146 'ext' : 'ext_convert',
147 'cfg' : 'cfg_convert',
150 # We might want to use a different one, e.g. importlib
151 importer = __import__
153 def __init__(self, config):
154 self.config = ConvertingDict(config)
155 self.config.configurator = self
157 def resolve(self, s):
159 Resolve strings to objects using standard import and attribute
165 found = self.importer(used)
169 found = getattr(found, frag)
170 except AttributeError:
172 found = getattr(found, frag)
175 e, tb = sys.exc_info()[1:]
176 v = ValueError('Cannot resolve %r: %s' % (s, e))
177 v.__cause__, v.__traceback__ = e, tb
180 def ext_convert(self, value):
181 """Default converter for the ext:// protocol."""
182 return self.resolve(value)
184 def cfg_convert(self, value):
185 """Default converter for the cfg:// protocol."""
187 m = self.WORD_PATTERN.match(rest)
189 raise ValueError("Unable to convert %r" % value)
191 rest = rest[m.end():]
192 d = self.config[m.groups()[0]]
195 m = self.DOT_PATTERN.match(rest)
199 m = self.INDEX_PATTERN.match(rest)
202 if not self.DIGIT_PATTERN.match(idx):
206 n = int(idx) # try as number first (most likely)
211 rest = rest[m.end():]
213 raise ValueError('Unable to convert '
214 '%r at %r' % (value, rest))
215 #rest should be empty
218 def convert(self, value):
220 Convert values to an appropriate type. dicts, lists and tuples are
221 replaced by their converting alternatives. Strings are checked to
222 see if they have a conversion format and are converted if they do.
224 if not isinstance(value, ConvertingDict) and isinstance(value, dict):
225 value = ConvertingDict(value)
226 value.configurator = self
227 elif not isinstance(value, ConvertingList) and isinstance(value, list):
228 value = ConvertingList(value)
229 value.configurator = self
230 elif not isinstance(value, ConvertingTuple) and\
231 isinstance(value, tuple):
232 value = ConvertingTuple(value)
233 value.configurator = self
234 elif isinstance(value, basestring): # str for py3k
235 m = self.CONVERT_PATTERN.match(value)
239 converter = self.value_converters.get(prefix, None)
242 converter = getattr(self, converter)
243 value = converter(suffix)
246 def configure_custom(self, config):
247 """Configure an object with a user-supplied factory."""
249 if not hasattr(c, '__call__') and hasattr(types, 'ClassType') and type(c) != types.ClassType:
251 props = config.pop('.', None)
252 # Check for valid identifiers
253 kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
256 for name, value in props.items():
257 setattr(result, name, value)
260 def as_tuple(self, value):
261 """Utility function which converts lists to tuples."""
262 if isinstance(value, list):
266 class DictConfigurator(BaseConfigurator):
268 Configure logging using a dictionary-like object to describe the
273 """Do the configuration."""
276 if 'version' not in config:
277 raise ValueError("dictionary doesn't specify a version")
278 if config['version'] != 1:
279 raise ValueError("Unsupported version: %s" % config['version'])
280 incremental = config.pop('incremental', False)
282 logging._acquireLock()
285 handlers = config.get('handlers', EMPTY_DICT)
286 # incremental handler config only if handler name
287 # ties in to logging._handlers (Python 2.7)
288 if sys.version_info[:2] == (2, 7):
289 for name in handlers:
290 if name not in logging._handlers:
291 raise ValueError('No handler found with '
295 handler = logging._handlers[name]
296 handler_config = handlers[name]
297 level = handler_config.get('level', None)
299 handler.setLevel(_checkLevel(level))
300 except StandardError, e:
301 raise ValueError('Unable to configure handler '
302 '%r: %s' % (name, e))
303 loggers = config.get('loggers', EMPTY_DICT)
306 self.configure_logger(name, loggers[name], True)
307 except StandardError, e:
308 raise ValueError('Unable to configure logger '
309 '%r: %s' % (name, e))
310 root = config.get('root', None)
313 self.configure_root(root, True)
314 except StandardError, e:
315 raise ValueError('Unable to configure root '
318 disable_existing = config.pop('disable_existing_loggers', True)
320 logging._handlers.clear()
321 del logging._handlerList[:]
323 # Do formatters first - they don't refer to anything else
324 formatters = config.get('formatters', EMPTY_DICT)
325 for name in formatters:
327 formatters[name] = self.configure_formatter(
329 except StandardError, e:
330 raise ValueError('Unable to configure '
331 'formatter %r: %s' % (name, e))
332 # Next, do filters - they don't refer to anything else, either
333 filters = config.get('filters', EMPTY_DICT)
336 filters[name] = self.configure_filter(filters[name])
337 except StandardError, e:
338 raise ValueError('Unable to configure '
339 'filter %r: %s' % (name, e))
341 # Next, do handlers - they refer to formatters and filters
342 # As handlers can refer to other handlers, sort the keys
343 # to allow a deterministic order of configuration
344 handlers = config.get('handlers', EMPTY_DICT)
345 for name in sorted(handlers):
347 handler = self.configure_handler(handlers[name])
349 handlers[name] = handler
350 except StandardError, e:
351 raise ValueError('Unable to configure handler '
352 '%r: %s' % (name, e))
353 # Next, do loggers - they refer to handlers and filters
355 #we don't want to lose the existing loggers,
356 #since other threads may have pointers to them.
357 #existing is set to contain all existing loggers,
358 #and as we go through the new configuration we
359 #remove any which are configured. At the end,
360 #what's left in existing is the set of loggers
361 #which were in the previous configuration but
362 #which are not in the new configuration.
364 existing = root.manager.loggerDict.keys()
365 #The list needs to be sorted so that we can
366 #avoid disabling child loggers of explicitly
367 #named loggers. With a sorted list it is easier
368 #to find the child loggers.
370 #We'll keep the list of existing loggers
371 #which are children of named loggers here...
373 #now set up the new ones...
374 loggers = config.get('loggers', EMPTY_DICT)
377 i = existing.index(name)
378 prefixed = name + "."
379 pflen = len(prefixed)
380 num_existing = len(existing)
381 i = i + 1 # look at the entry after name
382 while (i < num_existing) and\
383 (existing[i][:pflen] == prefixed):
384 child_loggers.append(existing[i])
386 existing.remove(name)
388 self.configure_logger(name, loggers[name])
389 except StandardError, e:
390 raise ValueError('Unable to configure logger '
391 '%r: %s' % (name, e))
393 #Disable any old loggers. There's no point deleting
394 #them as other threads may continue to hold references
395 #and by disabling them, you stop them doing any logging.
396 #However, don't disable children of named loggers, as that's
397 #probably not what was intended by the user.
399 logger = root.manager.loggerDict[log]
400 if log in child_loggers:
401 logger.level = logging.NOTSET
403 logger.propagate = True
404 elif disable_existing:
405 logger.disabled = True
407 # And finally, do the root logger
408 root = config.get('root', None)
411 self.configure_root(root)
412 except StandardError, e:
413 raise ValueError('Unable to configure root '
416 logging._releaseLock()
418 def configure_formatter(self, config):
419 """Configure a formatter from a dictionary."""
421 factory = config['()'] # for use in exception handler
423 result = self.configure_custom(config)
424 except TypeError, te:
425 if "'format'" not in str(te):
427 #Name of parameter changed from fmt to format.
428 #Retry with old name.
429 #This is so that code can be used with older Python versions
431 config['fmt'] = config.pop('format')
432 config['()'] = factory
433 result = self.configure_custom(config)
435 fmt = config.get('format', None)
436 dfmt = config.get('datefmt', None)
437 result = logging.Formatter(fmt, dfmt)
440 def configure_filter(self, config):
441 """Configure a filter from a dictionary."""
443 result = self.configure_custom(config)
445 name = config.get('name', '')
446 result = logging.Filter(name)
449 def add_filters(self, filterer, filters):
450 """Add filters to a filterer from a list of names."""
453 filterer.addFilter(self.config['filters'][f])
454 except StandardError, e:
455 raise ValueError('Unable to add filter %r: %s' % (f, e))
457 def configure_handler(self, config):
458 """Configure a handler from a dictionary."""
459 formatter = config.pop('formatter', None)
462 formatter = self.config['formatters'][formatter]
463 except StandardError, e:
464 raise ValueError('Unable to set formatter '
465 '%r: %s' % (formatter, e))
466 level = config.pop('level', None)
467 filters = config.pop('filters', None)
470 if not hasattr(c, '__call__') and hasattr(types, 'ClassType') and type(c) != types.ClassType:
474 klass = self.resolve(config.pop('class'))
475 #Special case for handler which refers to another handler
476 if issubclass(klass, logging.handlers.MemoryHandler) and\
479 config['target'] = self.config['handlers'][config['target']]
480 except StandardError, e:
481 raise ValueError('Unable to set target handler '
482 '%r: %s' % (config['target'], e))
483 elif issubclass(klass, logging.handlers.SMTPHandler) and\
484 'mailhost' in config:
485 config['mailhost'] = self.as_tuple(config['mailhost'])
486 elif issubclass(klass, logging.handlers.SysLogHandler) and\
488 config['address'] = self.as_tuple(config['address'])
490 kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
492 result = factory(**kwargs)
493 except TypeError, te:
494 if "'stream'" not in str(te):
496 #The argument name changed from strm to stream
497 #Retry with old name.
498 #This is so that code can be used with older Python versions
500 kwargs['strm'] = kwargs.pop('stream')
501 result = factory(**kwargs)
503 result.setFormatter(formatter)
504 if level is not None:
505 result.setLevel(_checkLevel(level))
507 self.add_filters(result, filters)
510 def add_handlers(self, logger, handlers):
511 """Add handlers to a logger from a list of names."""
514 logger.addHandler(self.config['handlers'][h])
515 except StandardError, e:
516 raise ValueError('Unable to add handler %r: %s' % (h, e))
518 def common_logger_config(self, logger, config, incremental=False):
520 Perform configuration which is common to root and non-root loggers.
522 level = config.get('level', None)
523 if level is not None:
524 logger.setLevel(_checkLevel(level))
526 #Remove any existing handlers
527 for h in logger.handlers[:]:
528 logger.removeHandler(h)
529 handlers = config.get('handlers', None)
531 self.add_handlers(logger, handlers)
532 filters = config.get('filters', None)
534 self.add_filters(logger, filters)
536 def configure_logger(self, name, config, incremental=False):
537 """Configure a non-root logger from a dictionary."""
538 logger = logging.getLogger(name)
539 self.common_logger_config(logger, config, incremental)
540 propagate = config.get('propagate', None)
541 if propagate is not None:
542 logger.propagate = propagate
544 def configure_root(self, config, incremental=False):
545 """Configure a root logger from a dictionary."""
546 root = logging.getLogger()
547 self.common_logger_config(root, config, incremental)
549 dictConfigClass = DictConfigurator
551 def dictConfig(config):
552 """Configure logging using a dictionary."""
553 dictConfigClass(config).configure()