Package Products :: Package ZenUtils :: Module configlog
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenUtils.configlog

 1  ###################################################################### 
 2  # 
 3  # Copyright 2011 Zenoss, Inc.  All Rights Reserved. 
 4  # 
 5  # Copyright 2001-2010 by Vinay Sajip. All Rights Reserved. 
 6  # 
 7  # Permission to use, copy, modify, and distribute this software and its 
 8  # documentation for any purpose and without fee is hereby granted, 
 9  # provided that the above copyright notice appear in all copies and that 
10  # both that copyright notice and this permission notice appear in 
11  # supporting documentation, and that the name of Vinay Sajip 
12  # not be used in advertising or publicity pertaining to distribution 
13  # of the software without specific, written prior permission. 
14  # VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
15  # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL 
16  # VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 
17  # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 
18  # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
19  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
20  ###################################################################### 
21   
22  """Creates new loggers from a python logging configuration file.""" 
23   
24  import logging 
25  import logging.config 
26   
27 -def addLogsFromConfigFile(fname, configDefaults=None):
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 # critical section 49 logging._acquireLock() 50 try: 51 logging._handlers.clear() 52 del logging._handlerList[:] 53 # Handlers add themselves to logging._handlers 54 handlers = logging.config._install_handlers(cp, formatters) 55 _zen_install_loggers(cp, handlers) 56 finally: 57 logging._releaseLock()
58 59
60 -def _zen_install_loggers(cp, handlers):
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 #now set up the new ones... 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