config.py 35 KB

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