config.py 36 KB

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