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 from logging import FileHandler
27 from logging.handlers import RotatingFileHandler
28 from logging.handlers import TimedRotatingFileHandler
29
30 from .Utils import zenPath
31
32 log = logging.getLogger("zen.configlog")
33
34
36 """Returns the filename relative to ZENHOME/log/"""
37 if filename.startswith('/'):
38 return filename
39 return zenPath('log', filename)
40
41
43 """Like python's FileHandler but relative to ZENHOME/log/"""
44 - def __init__(self, filename, mode='a', encoding=None, delay=0):
47
48
50 """Like python's RotatingFileHandler but relative to ZENHOME/log/"""
51 - def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
54
55
57 """Like python's TimedFileHandler but relative to ZENHOME/log/"""
58 - def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False):
61
62
64 """Add new loggers, handlers, and fomatters from a file.
65
66 The file should be in the standard Python log config format described here:
67 http://docs.python.org/library/logging.config.html#configuration-file-format
68
69 This code was copied from the Python 2.7 logging.config.fileConfig()
70 method, then altered to not require root or wipe existing loggers.
71 Unfortunately the standard option "disable_existing_loggers=False" would
72 still wipe out their settings and replace root, undoing Zope's log config.
73 """
74 import ConfigParser
75
76 try:
77 cp = ConfigParser.ConfigParser(configDefaults)
78 if hasattr(fname, 'readline'):
79 cp.readfp(fname)
80 else:
81 cp.read(fname)
82
83 formatters = logging.config._create_formatters(cp)
84 except Exception:
85 log.exception('Problem with log configuration file: %s', fname)
86 return
87
88
89 logging._acquireLock()
90 try:
91 logging._handlers.clear()
92 del logging._handlerList[:]
93
94 handlers = logging.config._install_handlers(cp, formatters)
95 _zen_install_loggers(cp, handlers)
96 except Exception:
97 log.exception('Problem with log configuration file: %s', fname)
98 finally:
99 logging._releaseLock()
100
101
103 """Create and install loggers, without wiping existing ones."""
104
105 llist = cp.get("loggers", "keys")
106 llist = [log.strip() for log in llist.split(",")]
107 if 'root' in llist:
108 raise Exception('Zenoss logger config files should not have a root logger.')
109
110
111 existing_logger_names = logging.root.manager.loggerDict.keys()
112 for log in llist:
113 sectname = "logger_%s" % log
114 qn = cp.get(sectname, "qualname")
115 opts = cp.options(sectname)
116 if "propagate" in opts:
117 propagate = cp.getint(sectname, "propagate")
118 else:
119 propagate = 1
120 if qn in existing_logger_names:
121 raise Exception("Logger already exists: %s" % qn)
122 logger = logging.getLogger(qn)
123 if "level" in opts:
124 level = cp.get(sectname, "level")
125 logger.setLevel(logging._levelNames[level])
126 for h in logger.handlers[:]:
127 logger.removeHandler(h)
128 logger.propagate = propagate
129 logger.disabled = 0
130 hlist = cp.get(sectname, "handlers")
131 if len(hlist):
132 hlist = hlist.split(",")
133 hlist = logging.config._strip_spaces(hlist)
134 for hand in hlist:
135 logger.addHandler(handlers[hand])
136