def find_handlers( obj: logging.Logger, hname: str = "", htype: logging.Handler = None, traverse: bool = True, ) -> Dict[str, List[logging.Handler]]: """Remove the NullHandler from a logger if found. Notes: * will remove handler if hname supplied and handler obj name matches * will remove handler if htype supplied and handler obj type matches Args: obj: logger obj to search for handler in traverse: traverse the logger obj supplied up to the root logger hname: name of handler to search for htype: type of handler to find """ handlers = {} for handler in obj.handlers: match_name = hname and handler.name == hname match_type = htype and isinstance(handler, htype) if match_name or match_type: handlers[obj.name] = handlers.get(obj.name, []) if handler not in handlers[obj.name]: handlers[obj.name].append(handler) if obj.parent and traverse: found = find_handlers(obj=obj.parent, hname=hname, htype=htype, traverse=traverse) handlers.update(found) return handlers
def configure_handlers(level): """ Configure (and install) handlers appropriate for the level. """ logging.info('Logging %s messages and higher.' % \ logging.getLevelName(level)) for hlevel in handlers.keys(): # avoid duplicate handlers by removing the handler first logger.removeHandler(handlers.get(hlevel)) if hlevel < level: logger.addHandler(null) continue handlers[hlevel].setLevel(hlevel) handlers[hlevel].setFormatter(get_formatter(hlevel)) logger.addHandler(handlers.get(hlevel)) # default to censoring the syslog handler if os.name == 'posix': syslog = handlers[1000] syslog.setLevel(logging.DEBUG) syslog.applyFormat() logger.addHandler(syslog)
def handle(self): handlers={'text':msg_action.text_handler,'image':msg_action.images_handler,'location':msg_action.location_handler} #将消息分发到对应的处理方法 action = handlers.get(self.MsgType,self.unknownMsgType()) log.debug('action :%s'%repr(action)) #return action(self) result='' try: result = action(self) if not isinstance(result,basestring): raise Exception("user action returns not string, but %s"%(str(result))) except: log.exception("exception on handle msg") return result
def handler_chooser(envvar_prefix, make_default=False): """Choose a logging handler based on some environment variable. The envvar_prefix must contain the prefix name of a handler, eg: syslog, streamlogging, nteventlog. It will be used to lookup the real handler based on: envar_prefix + "Handler" if make_default is set true then we auto insert this into the standard logger. """ import os, re, sys # Make a dict to search for our handler in handlers = dict([(name.lower(),name) \ for name in logging.handlers.__dict__.keys() \ if re.match(".*Handler", name)]) # Make the handler if we can if os.isatty(sys.stdout.fileno()): handler_name = os.getenv(envvar_prefix + "LOGGING_HANDLER", DEFAULT_LOGGING_HANDLER_NAME) else: handler_name = os.getenv(envvar_prefix + "LOGGING_HANDLER", 'SysLog') try: logginghandler = eval("logging.handlers.%s" % ( handlers.get("%shandler" % handler_name.lower()))) except Exception: # Hmmm... should we setup a default handler here? print >>sys.stderr, "setting up a handler with %s was impossible" % handler_name else: lh = logginghandler() formatter_spec = os.getenv(envvar_prefix + "LOGGING_SPEC", DEFAULT_LOGGING_SPEC) if formatter_spec: lh.setFormatter(logging.Formatter(formatter_spec)) if make_default: root_logger = logging.getLogger("") # root_logger always has a default sysloghandler defined by djangostart. # need to delete this else you get two logging statements per log request root_logger.handlers = [] root_logger.addHandler(lh) return lh
def find_handlers(obj, hname="", htype=None, traverse=True): """Remove the NullHandler from a logger if found. Notes: * will remove handler if hname supplied and handler obj name matches * will remove handler if htype supplied and handler obj type matches Args: obj (:obj:`logging.Logger`): logger obj to remove handler from hname (:obj:`str`, optional): default ``""`` - name of handler to find and remove htype (:class:`object`, optional): default ``None`` - type of handler to find and remove traverse (:obj:`bool`, optional): default ``True`` - * if ``True`` traverse the logger obj supplied up to the root logger to see if there are any matching attached handlers * if ``False`` only check the logger obj supplied for any matching attached handlers Returns: dict handler handler name->[handler objects] mapping of found handlers """ handlers = {} for handler in obj.handlers: match_name = hname and handler.name == hname match_type = htype and isinstance(handler, htype) if match_name or match_type: handlers[obj.name] = handlers.get(obj.name, []) if handler not in handlers[obj.name]: handlers[obj.name].append(handler) if obj.parent and traverse: found = find_handlers( obj=obj.parent, hname=hname, htype=htype, traverse=traverse ) handlers.update(found) return handlers
def find_handlers(obj, hname="", htype=None, traverse=True): """Find all handlers by traversing up the tree from obj.""" handlers = {} for handler in obj.handlers: match_name = hname and handler.name == hname match_type = htype and isinstance(handler, htype) if match_name or match_type: handlers[obj.name] = handlers.get(obj.name, []) if handler not in handlers[obj.name]: handlers[obj.name].append(handler) if obj.parent and traverse: found = find_handlers(obj=obj.parent, hname=hname, htype=htype, traverse=traverse) handlers.update(found) return handlers
def configure(self): """Do the configuration.""" config = self.config version = self.convert(config.get("version", None)) if version != 1: raise ValueError("Unsupported version: {}".format(config['version'])) incremental = self.convert(config.pop('incremental', False)) EMPTY_DICT = {} logging._acquireLock() try: if incremental: handlers = self.convert(config.get('handlers', EMPTY_DICT)) for name in handlers.keys(): if name not in logging._handlers: raise ValueError('No handler found with name {}'.format(name)) else: try: handler = logging._handlers[name] hconfig = self.convert(handlers[name]) level = self.convert(hconfig.get('level', None)) if level: handler.setLevel(logging._checkLevel(level)) except Exception as e: raise ValueError('Unable to configure handler ' '{}'.format(name)) from e loggers = self.convert(config.get('loggers', EMPTY_DICT)) for name in loggers.keys(): try: self.configure_logger(name, self.convert(loggers[name]), True) except Exception as e: raise ValueError('Unable to configure logger {}'.format(name)) from e root = self.convert(config.get('root', None)) if root: try: self.configure_root(root, True) except Exception as e: raise ValueError('Unable to configure root logger') from e else: disable_existing = config.pop('disable_existing_loggers', True) logging._handlers.clear() logging._handlerList[:] = [] # Do formatters first - they don't refer to anything else formatters = self.convert(config.get('formatters', EMPTY_DICT)) for name in formatters.keys(): try: fmtConfig = self.convert(formatters.get(name)) formatters[name] = self.configure_formatter(fmtConfig) except Exception as e: raise ValueError('Unable to configure formatter {}'.format(name)) from e # Next, do filters - they don't refer to anything else, either filters = self.convert(config.get('filters', EMPTY_DICT)) for name in filters.keys(): try: filtConfig = self.convert(filters.get(name)) filters[name] = self.configure_filter(filtConfig) except Exception as e: raise ValueError('Unable to configure filter {}'.format(name)) from e # Next, do handlers - they refer to formatters and filters # As handlers can refer to other handlers, sort the keys # to allow a deterministic order of configuration handlers = self.convert(config.get('handlers', EMPTY_DICT)) deferred = [] for name in sorted(handlers.keys()): try: handlerConfig = self.convert(handlers.get(name)) handler = self.configure_handler(handlerConfig) handler.name = name handlers[name] = handler except UnresolvableError as exc: raise exc except Exception as e: if 'target not configured yet' in str(e.__cause__): deferred.append(name) else: raise ValueError( 'Unable to config handler {}'.format(name) ) from e # Now do any that were deferred for name in deferred: try: handlerConfig = self.convert(handlers.get(name)) handler = self.configure_handler(handlerConfig) handler.name = name handlers[name] = handler except UnresolvableError as exc: raise exc except Exception as e: raise ValueError( 'Unable to configure handler {}'.format(name) ) from e # Next, do loggers - they refer to handlers and filters #we don't want to lose the existing loggers, #since other threads may have pointers to them. #existing is set to contain all existing loggers, #and as we go through the new configuration we #remove any which are configured. At the end, #what's left in existing is the set of loggers #which were in the previous configuration but #which are not in the new configuration. root = logging.root existing = list(root.manager.loggerDict.keys()) #The list needs to be sorted so that we can #avoid disabling child loggers of explicitly #named loggers. With a sorted list it is easier #to find the child loggers. existing.sort() #We'll keep the list of existing loggers #which are children of named loggers here... child_loggers = [] #now set up the new ones... loggers = self.convert(config.get('loggers', EMPTY_DICT)) for name in loggers.keys(): if name in existing: i = existing.index(name) + 1 # look after name prefixed = name + "." pflen = len(prefixed) num_existing = len(existing) while i < num_existing: if existing[i][:pflen] == prefixed: child_loggers.append(existing[i]) i += 1 existing.remove(name) try: loggerConfig = loggers.get(name) self.configure_logger(name, loggerConfig) except Exception as e: raise ValueError( 'Unable to configure logger {}'.format(name) )from e #Disable any old loggers. There's no point deleting #them as other threads may continue to hold references #and by disabling them, you stop them doing any logging. #However, don't disable children of named loggers, as that's #probably not what was intended by the user. #for log in existing: # logger = root.manager.loggerDict[log] # if log in child_loggers: # logger.level = logging.NOTSET # logger.handlers = [] # logger.propagate = True # elif disable_existing: # logger.disabled = True _handle_existing_loggers(existing, child_loggers, disable_existing) # And finally, do the root logger root = self.convert(config.get('root', None)) if root: try: self.configure_root(root) except Exception as e: raise ValueError('Unable to configure root ' 'logger') from e finally: logging._releaseLock()