我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用logging.PlaceHolder()。
def make_logger_node(name, logger, parent=None): if isinstance(logger, PlaceHolder): # some loggers are instances of logging.PlaceHolder level = parent.level else: level = humanize_level(logger.getEffectiveLevel()) node = Logger(name, level, logger, []) if parent is not None: parent.children.append(node) return node
def log_level(self, level): logger.debug("Setting config.log_level to %r", level) logging.getLogger().setLevel(level) logging._acquireLock() for name, _logger in iteritems(logging.Logger.manager.loggerDict): if isinstance(_logger, logging.PlaceHolder): continue _logger.setLevel(level) logging._releaseLock()
def configure(config_string=None, log_json=False, log_file=None): if not config_string: config_string = ":{}".format(DEFAULT_LOGLEVEL) if log_json: SLogger.manager.log_json = True log_format = JSON_FORMAT else: SLogger.manager.log_json = False log_format = PRINT_FORMAT if len(rootLogger.handlers) == 0: handler = StreamHandler() formatter = Formatter(log_format) handler.setFormatter(formatter) rootLogger.addHandler(handler) if log_file: if not any(isinstance(hndlr, FileHandler) for hndlr in rootLogger.handlers): handler = FileHandler(log_file) formatter = Formatter("{} {}".format(FILE_PREFIX, log_format)) handler.setFormatter(formatter) rootLogger.addHandler(handler) # Reset logging levels before applying new config below for name, logger in SLogger.manager.loggerDict.items(): if hasattr(logger, 'setLevel'): # Guard against `logging.PlaceHolder` instances logger.setLevel(logging.NOTSET) logger.propagate = True for name_levels in config_string.split(','): name, _, level = name_levels.partition(':') logger = getLogger(name) logger.setLevel(level.upper())