Create and install loggers
(cp, handlers, disable_existing)
| 193 | logger.disabled = disable_existing |
| 194 | |
| 195 | def _install_loggers(cp, handlers, disable_existing): |
| 196 | """Create and install loggers""" |
| 197 | |
| 198 | # configure the root first |
| 199 | llist = cp["loggers"]["keys"] |
| 200 | llist = llist.split(",") |
| 201 | llist = list(_strip_spaces(llist)) |
| 202 | llist.remove("root") |
| 203 | section = cp["logger_root"] |
| 204 | root = logging.root |
| 205 | log = root |
| 206 | if "level" in section: |
| 207 | level = section["level"] |
| 208 | log.setLevel(level) |
| 209 | for h in root.handlers[:]: |
| 210 | root.removeHandler(h) |
| 211 | hlist = section["handlers"] |
| 212 | if len(hlist): |
| 213 | hlist = hlist.split(",") |
| 214 | hlist = _strip_spaces(hlist) |
| 215 | for hand in hlist: |
| 216 | log.addHandler(handlers[hand]) |
| 217 | |
| 218 | #and now the others... |
| 219 | #we don't want to lose the existing loggers, |
| 220 | #since other threads may have pointers to them. |
| 221 | #existing is set to contain all existing loggers, |
| 222 | #and as we go through the new configuration we |
| 223 | #remove any which are configured. At the end, |
| 224 | #what's left in existing is the set of loggers |
| 225 | #which were in the previous configuration but |
| 226 | #which are not in the new configuration. |
| 227 | existing = list(root.manager.loggerDict.keys()) |
| 228 | #The list needs to be sorted so that we can |
| 229 | #avoid disabling child loggers of explicitly |
| 230 | #named loggers. With a sorted list it is easier |
| 231 | #to find the child loggers. |
| 232 | existing.sort() |
| 233 | #We'll keep the list of existing loggers |
| 234 | #which are children of named loggers here... |
| 235 | child_loggers = [] |
| 236 | #now set up the new ones... |
| 237 | for log in llist: |
| 238 | section = cp["logger_%s" % log] |
| 239 | qn = section["qualname"] |
| 240 | propagate = section.getint("propagate", fallback=1) |
| 241 | logger = logging.getLogger(qn) |
| 242 | if qn in existing: |
| 243 | i = existing.index(qn) + 1 # start with the entry after qn |
| 244 | prefixed = qn + "." |
| 245 | pflen = len(prefixed) |
| 246 | num_existing = len(existing) |
| 247 | while i < num_existing: |
| 248 | if existing[i][:pflen] == prefixed: |
| 249 | child_loggers.append(existing[i]) |
| 250 | i += 1 |
| 251 | existing.remove(qn) |
| 252 | if "level" in section: |
no test coverage detected