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