logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

config.py (36463B)


  1. # Copyright 2001-2019 by Vinay Sajip. All Rights Reserved.
  2. #
  3. # Permission to use, copy, modify, and distribute this software and its
  4. # documentation for any purpose and without fee is hereby granted,
  5. # provided that the above copyright notice appear in all copies and that
  6. # both that copyright notice and this permission notice appear in
  7. # supporting documentation, and that the name of Vinay Sajip
  8. # not be used in advertising or publicity pertaining to distribution
  9. # of the software without specific, written prior permission.
  10. # VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  11. # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  12. # VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  13. # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  14. # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """
  17. Configuration functions for the logging package for Python. The core package
  18. is based on PEP 282 and comments thereto in comp.lang.python, and influenced
  19. by Apache's log4j system.
  20. Copyright (C) 2001-2019 Vinay Sajip. All Rights Reserved.
  21. To use, simply 'import logging' and log away!
  22. """
  23. import errno
  24. import io
  25. import logging
  26. import logging.handlers
  27. import re
  28. import struct
  29. import sys
  30. import threading
  31. import traceback
  32. from socketserver import ThreadingTCPServer, StreamRequestHandler
  33. DEFAULT_LOGGING_CONFIG_PORT = 9030
  34. RESET_ERROR = errno.ECONNRESET
  35. #
  36. # The following code implements a socket listener for on-the-fly
  37. # reconfiguration of logging.
  38. #
  39. # _listener holds the server object doing the listening
  40. _listener = None
  41. def fileConfig(fname, defaults=None, disable_existing_loggers=True, encoding=None):
  42. """
  43. Read the logging configuration from a ConfigParser-format file.
  44. This can be called several times from an application, allowing an end user
  45. the ability to select from various pre-canned configurations (if the
  46. developer provides a mechanism to present the choices and load the chosen
  47. configuration).
  48. """
  49. import configparser
  50. if isinstance(fname, configparser.RawConfigParser):
  51. cp = fname
  52. else:
  53. cp = configparser.ConfigParser(defaults)
  54. if hasattr(fname, 'readline'):
  55. cp.read_file(fname)
  56. else:
  57. encoding = io.text_encoding(encoding)
  58. cp.read(fname, encoding=encoding)
  59. formatters = _create_formatters(cp)
  60. # critical section
  61. logging._acquireLock()
  62. try:
  63. _clearExistingHandlers()
  64. # Handlers add themselves to logging._handlers
  65. handlers = _install_handlers(cp, formatters)
  66. _install_loggers(cp, handlers, disable_existing_loggers)
  67. finally:
  68. logging._releaseLock()
  69. def _resolve(name):
  70. """Resolve a dotted name to a global object."""
  71. name = name.split('.')
  72. used = name.pop(0)
  73. found = __import__(used)
  74. for n in name:
  75. used = used + '.' + n
  76. try:
  77. found = getattr(found, n)
  78. except AttributeError:
  79. __import__(used)
  80. found = getattr(found, n)
  81. return found
  82. def _strip_spaces(alist):
  83. return map(str.strip, alist)
  84. def _create_formatters(cp):
  85. """Create and return formatters"""
  86. flist = cp["formatters"]["keys"]
  87. if not len(flist):
  88. return {}
  89. flist = flist.split(",")
  90. flist = _strip_spaces(flist)
  91. formatters = {}
  92. for form in flist:
  93. sectname = "formatter_%s" % form
  94. fs = cp.get(sectname, "format", raw=True, fallback=None)
  95. dfs = cp.get(sectname, "datefmt", raw=True, fallback=None)
  96. stl = cp.get(sectname, "style", raw=True, fallback='%')
  97. c = logging.Formatter
  98. class_name = cp[sectname].get("class")
  99. if class_name:
  100. c = _resolve(class_name)
  101. f = c(fs, dfs, stl)
  102. formatters[form] = f
  103. return formatters
  104. def _install_handlers(cp, formatters):
  105. """Install and return handlers"""
  106. hlist = cp["handlers"]["keys"]
  107. if not len(hlist):
  108. return {}
  109. hlist = hlist.split(",")
  110. hlist = _strip_spaces(hlist)
  111. handlers = {}
  112. fixups = [] #for inter-handler references
  113. for hand in hlist:
  114. section = cp["handler_%s" % hand]
  115. klass = section["class"]
  116. fmt = section.get("formatter", "")
  117. try:
  118. klass = eval(klass, vars(logging))
  119. except (AttributeError, NameError):
  120. klass = _resolve(klass)
  121. args = section.get("args", '()')
  122. args = eval(args, vars(logging))
  123. kwargs = section.get("kwargs", '{}')
  124. kwargs = eval(kwargs, vars(logging))
  125. h = klass(*args, **kwargs)
  126. h.name = hand
  127. if "level" in section:
  128. level = section["level"]
  129. h.setLevel(level)
  130. if len(fmt):
  131. h.setFormatter(formatters[fmt])
  132. if issubclass(klass, logging.handlers.MemoryHandler):
  133. target = section.get("target", "")
  134. if len(target): #the target handler may not be loaded yet, so keep for later...
  135. fixups.append((h, target))
  136. handlers[hand] = h
  137. #now all handlers are loaded, fixup inter-handler references...
  138. for h, t in fixups:
  139. h.setTarget(handlers[t])
  140. return handlers
  141. def _handle_existing_loggers(existing, child_loggers, disable_existing):
  142. """
  143. When (re)configuring logging, handle loggers which were in the previous
  144. configuration but are not in the new configuration. There's no point
  145. deleting them as other threads may continue to hold references to them;
  146. and by disabling them, you stop them doing any logging.
  147. However, don't disable children of named loggers, as that's probably not
  148. what was intended by the user. Also, allow existing loggers to NOT be
  149. disabled if disable_existing is false.
  150. """
  151. root = logging.root
  152. for log in existing:
  153. logger = root.manager.loggerDict[log]
  154. if log in child_loggers:
  155. if not isinstance(logger, logging.PlaceHolder):
  156. logger.setLevel(logging.NOTSET)
  157. logger.handlers = []
  158. logger.propagate = True
  159. else:
  160. logger.disabled = disable_existing
  161. def _install_loggers(cp, handlers, disable_existing):
  162. """Create and install loggers"""
  163. # configure the root first
  164. llist = cp["loggers"]["keys"]
  165. llist = llist.split(",")
  166. llist = list(_strip_spaces(llist))
  167. llist.remove("root")
  168. section = cp["logger_root"]
  169. root = logging.root
  170. log = root
  171. if "level" in section:
  172. level = section["level"]
  173. log.setLevel(level)
  174. for h in root.handlers[:]:
  175. root.removeHandler(h)
  176. hlist = section["handlers"]
  177. if len(hlist):
  178. hlist = hlist.split(",")
  179. hlist = _strip_spaces(hlist)
  180. for hand in hlist:
  181. log.addHandler(handlers[hand])
  182. #and now the others...
  183. #we don't want to lose the existing loggers,
  184. #since other threads may have pointers to them.
  185. #existing is set to contain all existing loggers,
  186. #and as we go through the new configuration we
  187. #remove any which are configured. At the end,
  188. #what's left in existing is the set of loggers
  189. #which were in the previous configuration but
  190. #which are not in the new configuration.
  191. existing = list(root.manager.loggerDict.keys())
  192. #The list needs to be sorted so that we can
  193. #avoid disabling child loggers of explicitly
  194. #named loggers. With a sorted list it is easier
  195. #to find the child loggers.
  196. existing.sort()
  197. #We'll keep the list of existing loggers
  198. #which are children of named loggers here...
  199. child_loggers = []
  200. #now set up the new ones...
  201. for log in llist:
  202. section = cp["logger_%s" % log]
  203. qn = section["qualname"]
  204. propagate = section.getint("propagate", fallback=1)
  205. logger = logging.getLogger(qn)
  206. if qn in existing:
  207. i = existing.index(qn) + 1 # start with the entry after qn
  208. prefixed = qn + "."
  209. pflen = len(prefixed)
  210. num_existing = len(existing)
  211. while i < num_existing:
  212. if existing[i][:pflen] == prefixed:
  213. child_loggers.append(existing[i])
  214. i += 1
  215. existing.remove(qn)
  216. if "level" in section:
  217. level = section["level"]
  218. logger.setLevel(level)
  219. for h in logger.handlers[:]:
  220. logger.removeHandler(h)
  221. logger.propagate = propagate
  222. logger.disabled = 0
  223. hlist = section["handlers"]
  224. if len(hlist):
  225. hlist = hlist.split(",")
  226. hlist = _strip_spaces(hlist)
  227. for hand in hlist:
  228. logger.addHandler(handlers[hand])
  229. #Disable any old loggers. There's no point deleting
  230. #them as other threads may continue to hold references
  231. #and by disabling them, you stop them doing any logging.
  232. #However, don't disable children of named loggers, as that's
  233. #probably not what was intended by the user.
  234. #for log in existing:
  235. # logger = root.manager.loggerDict[log]
  236. # if log in child_loggers:
  237. # logger.level = logging.NOTSET
  238. # logger.handlers = []
  239. # logger.propagate = 1
  240. # elif disable_existing_loggers:
  241. # logger.disabled = 1
  242. _handle_existing_loggers(existing, child_loggers, disable_existing)
  243. def _clearExistingHandlers():
  244. """Clear and close existing handlers"""
  245. logging._handlers.clear()
  246. logging.shutdown(logging._handlerList[:])
  247. del logging._handlerList[:]
  248. IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)
  249. def valid_ident(s):
  250. m = IDENTIFIER.match(s)
  251. if not m:
  252. raise ValueError('Not a valid Python identifier: %r' % s)
  253. return True
  254. class ConvertingMixin(object):
  255. """For ConvertingXXX's, this mixin class provides common functions"""
  256. def convert_with_key(self, key, value, replace=True):
  257. result = self.configurator.convert(value)
  258. #If the converted value is different, save for next time
  259. if value is not result:
  260. if replace:
  261. self[key] = result
  262. if type(result) in (ConvertingDict, ConvertingList,
  263. ConvertingTuple):
  264. result.parent = self
  265. result.key = key
  266. return result
  267. def convert(self, value):
  268. result = self.configurator.convert(value)
  269. if value is not result:
  270. if type(result) in (ConvertingDict, ConvertingList,
  271. ConvertingTuple):
  272. result.parent = self
  273. return result
  274. # The ConvertingXXX classes are wrappers around standard Python containers,
  275. # and they serve to convert any suitable values in the container. The
  276. # conversion converts base dicts, lists and tuples to their wrapped
  277. # equivalents, whereas strings which match a conversion format are converted
  278. # appropriately.
  279. #
  280. # Each wrapper should have a configurator attribute holding the actual
  281. # configurator to use for conversion.
  282. class ConvertingDict(dict, ConvertingMixin):
  283. """A converting dictionary wrapper."""
  284. def __getitem__(self, key):
  285. value = dict.__getitem__(self, key)
  286. return self.convert_with_key(key, value)
  287. def get(self, key, default=None):
  288. value = dict.get(self, key, default)
  289. return self.convert_with_key(key, value)
  290. def pop(self, key, default=None):
  291. value = dict.pop(self, key, default)
  292. return self.convert_with_key(key, value, replace=False)
  293. class ConvertingList(list, ConvertingMixin):
  294. """A converting list wrapper."""
  295. def __getitem__(self, key):
  296. value = list.__getitem__(self, key)
  297. return self.convert_with_key(key, value)
  298. def pop(self, idx=-1):
  299. value = list.pop(self, idx)
  300. return self.convert(value)
  301. class ConvertingTuple(tuple, ConvertingMixin):
  302. """A converting tuple wrapper."""
  303. def __getitem__(self, key):
  304. value = tuple.__getitem__(self, key)
  305. # Can't replace a tuple entry.
  306. return self.convert_with_key(key, value, replace=False)
  307. class BaseConfigurator(object):
  308. """
  309. The configurator base class which defines some useful defaults.
  310. """
  311. CONVERT_PATTERN = re.compile(r'^(?P<prefix>[a-z]+)://(?P<suffix>.*)$')
  312. WORD_PATTERN = re.compile(r'^\s*(\w+)\s*')
  313. DOT_PATTERN = re.compile(r'^\.\s*(\w+)\s*')
  314. INDEX_PATTERN = re.compile(r'^\[\s*(\w+)\s*\]\s*')
  315. DIGIT_PATTERN = re.compile(r'^\d+$')
  316. value_converters = {
  317. 'ext' : 'ext_convert',
  318. 'cfg' : 'cfg_convert',
  319. }
  320. # We might want to use a different one, e.g. importlib
  321. importer = staticmethod(__import__)
  322. def __init__(self, config):
  323. self.config = ConvertingDict(config)
  324. self.config.configurator = self
  325. def resolve(self, s):
  326. """
  327. Resolve strings to objects using standard import and attribute
  328. syntax.
  329. """
  330. name = s.split('.')
  331. used = name.pop(0)
  332. try:
  333. found = self.importer(used)
  334. for frag in name:
  335. used += '.' + frag
  336. try:
  337. found = getattr(found, frag)
  338. except AttributeError:
  339. self.importer(used)
  340. found = getattr(found, frag)
  341. return found
  342. except ImportError:
  343. e, tb = sys.exc_info()[1:]
  344. v = ValueError('Cannot resolve %r: %s' % (s, e))
  345. v.__cause__, v.__traceback__ = e, tb
  346. raise v
  347. def ext_convert(self, value):
  348. """Default converter for the ext:// protocol."""
  349. return self.resolve(value)
  350. def cfg_convert(self, value):
  351. """Default converter for the cfg:// protocol."""
  352. rest = value
  353. m = self.WORD_PATTERN.match(rest)
  354. if m is None:
  355. raise ValueError("Unable to convert %r" % value)
  356. else:
  357. rest = rest[m.end():]
  358. d = self.config[m.groups()[0]]
  359. #print d, rest
  360. while rest:
  361. m = self.DOT_PATTERN.match(rest)
  362. if m:
  363. d = d[m.groups()[0]]
  364. else:
  365. m = self.INDEX_PATTERN.match(rest)
  366. if m:
  367. idx = m.groups()[0]
  368. if not self.DIGIT_PATTERN.match(idx):
  369. d = d[idx]
  370. else:
  371. try:
  372. n = int(idx) # try as number first (most likely)
  373. d = d[n]
  374. except TypeError:
  375. d = d[idx]
  376. if m:
  377. rest = rest[m.end():]
  378. else:
  379. raise ValueError('Unable to convert '
  380. '%r at %r' % (value, rest))
  381. #rest should be empty
  382. return d
  383. def convert(self, value):
  384. """
  385. Convert values to an appropriate type. dicts, lists and tuples are
  386. replaced by their converting alternatives. Strings are checked to
  387. see if they have a conversion format and are converted if they do.
  388. """
  389. if not isinstance(value, ConvertingDict) and isinstance(value, dict):
  390. value = ConvertingDict(value)
  391. value.configurator = self
  392. elif not isinstance(value, ConvertingList) and isinstance(value, list):
  393. value = ConvertingList(value)
  394. value.configurator = self
  395. elif not isinstance(value, ConvertingTuple) and\
  396. isinstance(value, tuple) and not hasattr(value, '_fields'):
  397. value = ConvertingTuple(value)
  398. value.configurator = self
  399. elif isinstance(value, str): # str for py3k
  400. m = self.CONVERT_PATTERN.match(value)
  401. if m:
  402. d = m.groupdict()
  403. prefix = d['prefix']
  404. converter = self.value_converters.get(prefix, None)
  405. if converter:
  406. suffix = d['suffix']
  407. converter = getattr(self, converter)
  408. value = converter(suffix)
  409. return value
  410. def configure_custom(self, config):
  411. """Configure an object with a user-supplied factory."""
  412. c = config.pop('()')
  413. if not callable(c):
  414. c = self.resolve(c)
  415. props = config.pop('.', None)
  416. # Check for valid identifiers
  417. kwargs = {k: config[k] for k in config if valid_ident(k)}
  418. result = c(**kwargs)
  419. if props:
  420. for name, value in props.items():
  421. setattr(result, name, value)
  422. return result
  423. def as_tuple(self, value):
  424. """Utility function which converts lists to tuples."""
  425. if isinstance(value, list):
  426. value = tuple(value)
  427. return value
  428. class DictConfigurator(BaseConfigurator):
  429. """
  430. Configure logging using a dictionary-like object to describe the
  431. configuration.
  432. """
  433. def configure(self):
  434. """Do the configuration."""
  435. config = self.config
  436. if 'version' not in config:
  437. raise ValueError("dictionary doesn't specify a version")
  438. if config['version'] != 1:
  439. raise ValueError("Unsupported version: %s" % config['version'])
  440. incremental = config.pop('incremental', False)
  441. EMPTY_DICT = {}
  442. logging._acquireLock()
  443. try:
  444. if incremental:
  445. handlers = config.get('handlers', EMPTY_DICT)
  446. for name in handlers:
  447. if name not in logging._handlers:
  448. raise ValueError('No handler found with '
  449. 'name %r' % name)
  450. else:
  451. try:
  452. handler = logging._handlers[name]
  453. handler_config = handlers[name]
  454. level = handler_config.get('level', None)
  455. if level:
  456. handler.setLevel(logging._checkLevel(level))
  457. except Exception as e:
  458. raise ValueError('Unable to configure handler '
  459. '%r' % name) from e
  460. loggers = config.get('loggers', EMPTY_DICT)
  461. for name in loggers:
  462. try:
  463. self.configure_logger(name, loggers[name], True)
  464. except Exception as e:
  465. raise ValueError('Unable to configure logger '
  466. '%r' % name) from e
  467. root = config.get('root', None)
  468. if root:
  469. try:
  470. self.configure_root(root, True)
  471. except Exception as e:
  472. raise ValueError('Unable to configure root '
  473. 'logger') from e
  474. else:
  475. disable_existing = config.pop('disable_existing_loggers', True)
  476. _clearExistingHandlers()
  477. # Do formatters first - they don't refer to anything else
  478. formatters = config.get('formatters', EMPTY_DICT)
  479. for name in formatters:
  480. try:
  481. formatters[name] = self.configure_formatter(
  482. formatters[name])
  483. except Exception as e:
  484. raise ValueError('Unable to configure '
  485. 'formatter %r' % name) from e
  486. # Next, do filters - they don't refer to anything else, either
  487. filters = config.get('filters', EMPTY_DICT)
  488. for name in filters:
  489. try:
  490. filters[name] = self.configure_filter(filters[name])
  491. except Exception as e:
  492. raise ValueError('Unable to configure '
  493. 'filter %r' % name) from e
  494. # Next, do handlers - they refer to formatters and filters
  495. # As handlers can refer to other handlers, sort the keys
  496. # to allow a deterministic order of configuration
  497. handlers = config.get('handlers', EMPTY_DICT)
  498. deferred = []
  499. for name in sorted(handlers):
  500. try:
  501. handler = self.configure_handler(handlers[name])
  502. handler.name = name
  503. handlers[name] = handler
  504. except Exception as e:
  505. if 'target not configured yet' in str(e.__cause__):
  506. deferred.append(name)
  507. else:
  508. raise ValueError('Unable to configure handler '
  509. '%r' % name) from e
  510. # Now do any that were deferred
  511. for name in deferred:
  512. try:
  513. handler = self.configure_handler(handlers[name])
  514. handler.name = name
  515. handlers[name] = handler
  516. except Exception as e:
  517. raise ValueError('Unable to configure handler '
  518. '%r' % name) from e
  519. # Next, do loggers - they refer to handlers and filters
  520. #we don't want to lose the existing loggers,
  521. #since other threads may have pointers to them.
  522. #existing is set to contain all existing loggers,
  523. #and as we go through the new configuration we
  524. #remove any which are configured. At the end,
  525. #what's left in existing is the set of loggers
  526. #which were in the previous configuration but
  527. #which are not in the new configuration.
  528. root = logging.root
  529. existing = list(root.manager.loggerDict.keys())
  530. #The list needs to be sorted so that we can
  531. #avoid disabling child loggers of explicitly
  532. #named loggers. With a sorted list it is easier
  533. #to find the child loggers.
  534. existing.sort()
  535. #We'll keep the list of existing loggers
  536. #which are children of named loggers here...
  537. child_loggers = []
  538. #now set up the new ones...
  539. loggers = config.get('loggers', EMPTY_DICT)
  540. for name in loggers:
  541. if name in existing:
  542. i = existing.index(name) + 1 # look after name
  543. prefixed = name + "."
  544. pflen = len(prefixed)
  545. num_existing = len(existing)
  546. while i < num_existing:
  547. if existing[i][:pflen] == prefixed:
  548. child_loggers.append(existing[i])
  549. i += 1
  550. existing.remove(name)
  551. try:
  552. self.configure_logger(name, loggers[name])
  553. except Exception as e:
  554. raise ValueError('Unable to configure logger '
  555. '%r' % name) from e
  556. #Disable any old loggers. There's no point deleting
  557. #them as other threads may continue to hold references
  558. #and by disabling them, you stop them doing any logging.
  559. #However, don't disable children of named loggers, as that's
  560. #probably not what was intended by the user.
  561. #for log in existing:
  562. # logger = root.manager.loggerDict[log]
  563. # if log in child_loggers:
  564. # logger.level = logging.NOTSET
  565. # logger.handlers = []
  566. # logger.propagate = True
  567. # elif disable_existing:
  568. # logger.disabled = True
  569. _handle_existing_loggers(existing, child_loggers,
  570. disable_existing)
  571. # And finally, do the root logger
  572. root = config.get('root', None)
  573. if root:
  574. try:
  575. self.configure_root(root)
  576. except Exception as e:
  577. raise ValueError('Unable to configure root '
  578. 'logger') from e
  579. finally:
  580. logging._releaseLock()
  581. def configure_formatter(self, config):
  582. """Configure a formatter from a dictionary."""
  583. if '()' in config:
  584. factory = config['()'] # for use in exception handler
  585. try:
  586. result = self.configure_custom(config)
  587. except TypeError as te:
  588. if "'format'" not in str(te):
  589. raise
  590. #Name of parameter changed from fmt to format.
  591. #Retry with old name.
  592. #This is so that code can be used with older Python versions
  593. #(e.g. by Django)
  594. config['fmt'] = config.pop('format')
  595. config['()'] = factory
  596. result = self.configure_custom(config)
  597. else:
  598. fmt = config.get('format', None)
  599. dfmt = config.get('datefmt', None)
  600. style = config.get('style', '%')
  601. cname = config.get('class', None)
  602. if not cname:
  603. c = logging.Formatter
  604. else:
  605. c = _resolve(cname)
  606. # A TypeError would be raised if "validate" key is passed in with a formatter callable
  607. # that does not accept "validate" as a parameter
  608. if 'validate' in config: # if user hasn't mentioned it, the default will be fine
  609. result = c(fmt, dfmt, style, config['validate'])
  610. else:
  611. result = c(fmt, dfmt, style)
  612. return result
  613. def configure_filter(self, config):
  614. """Configure a filter from a dictionary."""
  615. if '()' in config:
  616. result = self.configure_custom(config)
  617. else:
  618. name = config.get('name', '')
  619. result = logging.Filter(name)
  620. return result
  621. def add_filters(self, filterer, filters):
  622. """Add filters to a filterer from a list of names."""
  623. for f in filters:
  624. try:
  625. filterer.addFilter(self.config['filters'][f])
  626. except Exception as e:
  627. raise ValueError('Unable to add filter %r' % f) from e
  628. def configure_handler(self, config):
  629. """Configure a handler from a dictionary."""
  630. config_copy = dict(config) # for restoring in case of error
  631. formatter = config.pop('formatter', None)
  632. if formatter:
  633. try:
  634. formatter = self.config['formatters'][formatter]
  635. except Exception as e:
  636. raise ValueError('Unable to set formatter '
  637. '%r' % formatter) from e
  638. level = config.pop('level', None)
  639. filters = config.pop('filters', None)
  640. if '()' in config:
  641. c = config.pop('()')
  642. if not callable(c):
  643. c = self.resolve(c)
  644. factory = c
  645. else:
  646. cname = config.pop('class')
  647. klass = self.resolve(cname)
  648. #Special case for handler which refers to another handler
  649. if issubclass(klass, logging.handlers.MemoryHandler) and\
  650. 'target' in config:
  651. try:
  652. th = self.config['handlers'][config['target']]
  653. if not isinstance(th, logging.Handler):
  654. config.update(config_copy) # restore for deferred cfg
  655. raise TypeError('target not configured yet')
  656. config['target'] = th
  657. except Exception as e:
  658. raise ValueError('Unable to set target handler '
  659. '%r' % config['target']) from e
  660. elif issubclass(klass, logging.handlers.SMTPHandler) and\
  661. 'mailhost' in config:
  662. config['mailhost'] = self.as_tuple(config['mailhost'])
  663. elif issubclass(klass, logging.handlers.SysLogHandler) and\
  664. 'address' in config:
  665. config['address'] = self.as_tuple(config['address'])
  666. factory = klass
  667. props = config.pop('.', None)
  668. kwargs = {k: config[k] for k in config if valid_ident(k)}
  669. try:
  670. result = factory(**kwargs)
  671. except TypeError as te:
  672. if "'stream'" not in str(te):
  673. raise
  674. #The argument name changed from strm to stream
  675. #Retry with old name.
  676. #This is so that code can be used with older Python versions
  677. #(e.g. by Django)
  678. kwargs['strm'] = kwargs.pop('stream')
  679. result = factory(**kwargs)
  680. if formatter:
  681. result.setFormatter(formatter)
  682. if level is not None:
  683. result.setLevel(logging._checkLevel(level))
  684. if filters:
  685. self.add_filters(result, filters)
  686. if props:
  687. for name, value in props.items():
  688. setattr(result, name, value)
  689. return result
  690. def add_handlers(self, logger, handlers):
  691. """Add handlers to a logger from a list of names."""
  692. for h in handlers:
  693. try:
  694. logger.addHandler(self.config['handlers'][h])
  695. except Exception as e:
  696. raise ValueError('Unable to add handler %r' % h) from e
  697. def common_logger_config(self, logger, config, incremental=False):
  698. """
  699. Perform configuration which is common to root and non-root loggers.
  700. """
  701. level = config.get('level', None)
  702. if level is not None:
  703. logger.setLevel(logging._checkLevel(level))
  704. if not incremental:
  705. #Remove any existing handlers
  706. for h in logger.handlers[:]:
  707. logger.removeHandler(h)
  708. handlers = config.get('handlers', None)
  709. if handlers:
  710. self.add_handlers(logger, handlers)
  711. filters = config.get('filters', None)
  712. if filters:
  713. self.add_filters(logger, filters)
  714. def configure_logger(self, name, config, incremental=False):
  715. """Configure a non-root logger from a dictionary."""
  716. logger = logging.getLogger(name)
  717. self.common_logger_config(logger, config, incremental)
  718. propagate = config.get('propagate', None)
  719. if propagate is not None:
  720. logger.propagate = propagate
  721. def configure_root(self, config, incremental=False):
  722. """Configure a root logger from a dictionary."""
  723. root = logging.getLogger()
  724. self.common_logger_config(root, config, incremental)
  725. dictConfigClass = DictConfigurator
  726. def dictConfig(config):
  727. """Configure logging using a dictionary."""
  728. dictConfigClass(config).configure()
  729. def listen(port=DEFAULT_LOGGING_CONFIG_PORT, verify=None):
  730. """
  731. Start up a socket server on the specified port, and listen for new
  732. configurations.
  733. These will be sent as a file suitable for processing by fileConfig().
  734. Returns a Thread object on which you can call start() to start the server,
  735. and which you can join() when appropriate. To stop the server, call
  736. stopListening().
  737. Use the ``verify`` argument to verify any bytes received across the wire
  738. from a client. If specified, it should be a callable which receives a
  739. single argument - the bytes of configuration data received across the
  740. network - and it should return either ``None``, to indicate that the
  741. passed in bytes could not be verified and should be discarded, or a
  742. byte string which is then passed to the configuration machinery as
  743. normal. Note that you can return transformed bytes, e.g. by decrypting
  744. the bytes passed in.
  745. """
  746. class ConfigStreamHandler(StreamRequestHandler):
  747. """
  748. Handler for a logging configuration request.
  749. It expects a completely new logging configuration and uses fileConfig
  750. to install it.
  751. """
  752. def handle(self):
  753. """
  754. Handle a request.
  755. Each request is expected to be a 4-byte length, packed using
  756. struct.pack(">L", n), followed by the config file.
  757. Uses fileConfig() to do the grunt work.
  758. """
  759. try:
  760. conn = self.connection
  761. chunk = conn.recv(4)
  762. if len(chunk) == 4:
  763. slen = struct.unpack(">L", chunk)[0]
  764. chunk = self.connection.recv(slen)
  765. while len(chunk) < slen:
  766. chunk = chunk + conn.recv(slen - len(chunk))
  767. if self.server.verify is not None:
  768. chunk = self.server.verify(chunk)
  769. if chunk is not None: # verified, can process
  770. chunk = chunk.decode("utf-8")
  771. try:
  772. import json
  773. d =json.loads(chunk)
  774. assert isinstance(d, dict)
  775. dictConfig(d)
  776. except Exception:
  777. #Apply new configuration.
  778. file = io.StringIO(chunk)
  779. try:
  780. fileConfig(file)
  781. except Exception:
  782. traceback.print_exc()
  783. if self.server.ready:
  784. self.server.ready.set()
  785. except OSError as e:
  786. if e.errno != RESET_ERROR:
  787. raise
  788. class ConfigSocketReceiver(ThreadingTCPServer):
  789. """
  790. A simple TCP socket-based logging config receiver.
  791. """
  792. allow_reuse_address = 1
  793. def __init__(self, host='localhost', port=DEFAULT_LOGGING_CONFIG_PORT,
  794. handler=None, ready=None, verify=None):
  795. ThreadingTCPServer.__init__(self, (host, port), handler)
  796. logging._acquireLock()
  797. self.abort = 0
  798. logging._releaseLock()
  799. self.timeout = 1
  800. self.ready = ready
  801. self.verify = verify
  802. def serve_until_stopped(self):
  803. import select
  804. abort = 0
  805. while not abort:
  806. rd, wr, ex = select.select([self.socket.fileno()],
  807. [], [],
  808. self.timeout)
  809. if rd:
  810. self.handle_request()
  811. logging._acquireLock()
  812. abort = self.abort
  813. logging._releaseLock()
  814. self.server_close()
  815. class Server(threading.Thread):
  816. def __init__(self, rcvr, hdlr, port, verify):
  817. super(Server, self).__init__()
  818. self.rcvr = rcvr
  819. self.hdlr = hdlr
  820. self.port = port
  821. self.verify = verify
  822. self.ready = threading.Event()
  823. def run(self):
  824. server = self.rcvr(port=self.port, handler=self.hdlr,
  825. ready=self.ready,
  826. verify=self.verify)
  827. if self.port == 0:
  828. self.port = server.server_address[1]
  829. self.ready.set()
  830. global _listener
  831. logging._acquireLock()
  832. _listener = server
  833. logging._releaseLock()
  834. server.serve_until_stopped()
  835. return Server(ConfigSocketReceiver, ConfigStreamHandler, port, verify)
  836. def stopListening():
  837. """
  838. Stop the listening server which was created with a call to listen().
  839. """
  840. global _listener
  841. logging._acquireLock()
  842. try:
  843. if _listener:
  844. _listener.abort = 1
  845. _listener = None
  846. finally:
  847. logging._releaseLock()