1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """Creates new loggers from a python logging configuration file."""
23
24 import logging
25 import logging.config
26
28 """Add new loggers, handlers, and fomatters from a file.
29
30 The file should be in the standard Python log config format described here:
31 http://docs.python.org/library/logging.config.html#configuration-file-format
32
33 This code was copied from the Python 2.7 logging.config.fileConfig()
34 method, then altered to not require root or wipe existing loggers.
35 Unfortunately the standard option "disable_existing_loggers=False" would
36 still wipe out their settings and replace root, undoing Zope's log config.
37 """
38 import ConfigParser
39
40 cp = ConfigParser.ConfigParser(configDefaults)
41 if hasattr(fname, 'readline'):
42 cp.readfp(fname)
43 else:
44 cp.read(fname)
45
46 formatters = logging.config._create_formatters(cp)
47
48
49 logging._acquireLock()
50 try:
51 logging._handlers.clear()
52 del logging._handlerList[:]
53
54 handlers = logging.config._install_handlers(cp, formatters)
55 _zen_install_loggers(cp, handlers)
56 finally:
57 logging._releaseLock()
58
59
61 """Create and install loggers, without wiping existing ones."""
62
63 llist = cp.get("loggers", "keys")
64 llist = [log.strip() for log in llist.split(",")]
65 if 'root' in llist:
66 raise Exception('Zenoss logger config files should not have a root logger.')
67
68
69 existing_logger_names = logging.root.manager.loggerDict.keys()
70 for log in llist:
71 sectname = "logger_%s" % log
72 qn = cp.get(sectname, "qualname")
73 opts = cp.options(sectname)
74 if "propagate" in opts:
75 propagate = cp.getint(sectname, "propagate")
76 else:
77 propagate = 1
78 if qn in existing_logger_names:
79 raise Exception("Logger already exists: %s" % qn)
80 logger = logging.getLogger(qn)
81 if "level" in opts:
82 level = cp.get(sectname, "level")
83 logger.setLevel(logging._levelNames[level])
84 for h in logger.handlers[:]:
85 logger.removeHandler(h)
86 logger.propagate = propagate
87 logger.disabled = 0
88 hlist = cp.get(sectname, "handlers")
89 if len(hlist):
90 hlist = hlist.split(",")
91 hlist = logging.config._strip_spaces(hlist)
92 for hand in hlist:
93 logger.addHandler(handlers[hand])
94