Python logging.PlaceHolder() Examples

The following are 12 code examples of logging.PlaceHolder(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module logging , or try the search function .
Example #1
Source File: logger.py    From streamalert with Apache License 2.0 6 votes vote down vote up
def set_logger_levels(debug=False):
    """Set all of the logger levels

    Args:
        debug (bool): True to enable debug logging, False otherwise
    """
    for name, logger in logging.Logger.manager.loggerDict.items():
        if isinstance(logger, logging.PlaceHolder):
            continue

        if debug and name.startswith('streamalert'):
            logger.setLevel(logging.DEBUG if debug else logging.INFO)
        elif name.startswith(__package__):
            logger.setLevel(logging.INFO)
        else:
            logger.disabled = True  # disable this logger if it's not one of ours 
Example #2
Source File: config.py    From odoo13-x64 with GNU General Public License v3.0 6 votes vote down vote up
def _handle_existing_loggers(existing, child_loggers, disable_existing):
    """
    When (re)configuring logging, handle loggers which were in the previous
    configuration but are not in the new configuration. There's no point
    deleting them as other threads may continue to hold references to them;
    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. Also, allow existing loggers to NOT be
    disabled if disable_existing is false.
    """
    root = logging.root
    for log in existing:
        logger = root.manager.loggerDict[log]
        if log in child_loggers:
            if not isinstance(logger, logging.PlaceHolder):
                logger.setLevel(logging.NOTSET)
                logger.handlers = []
                logger.propagate = True
        else:
            logger.disabled = disable_existing 
Example #3
Source File: logging.py    From core with Apache License 2.0 6 votes vote down vote up
def setOverrideLogLevel(lvl):
    """
    Override all logger filter levels to include lvl and above.


    - Set root logger level
    - iterates all existing loggers and sets their log level to ``NOTSET``.

    Args:
        lvl (string): Log level name.
    """
    if lvl is None:
        return
    logging.info('Overriding log level globally to %s', lvl)
    lvl = getLevelName(lvl)
    global _overrideLogLevel # pylint: disable=global-statement
    _overrideLogLevel = lvl
    logging.getLogger('').setLevel(lvl)
    for loggerName in logging.Logger.manager.loggerDict:
        logger = logging.Logger.manager.loggerDict[loggerName]
        if isinstance(logger, logging.PlaceHolder):
            continue
        logger.setLevel(logging.NOTSET) 
Example #4
Source File: infra_logopts.py    From RackHD with Apache License 2.0 6 votes vote down vote up
def __build_ref_lists(self):
        """
        Build up easy-to-use lists of loggers and handlers.
        """
        ll = {}
        hl = {}
        loggers = dict(logging.Logger.manager.loggerDict)
        loggers[''] = logging.getLogger('')
        loggers['root'] = logging.getLogger('')
        for lg_name, lg_obj in loggers.items():
            if not isinstance(lg_obj, logging.PlaceHolder):
                ll[lg_name] = lg_obj
                for handler in lg_obj.handlers:
                    hl[handler.get_name()] = handler
        self.__loggers_by_name = ll
        self.__handlers_by_name = hl 
Example #5
Source File: util.py    From cf-mendix-buildpack with Apache License 2.0 5 votes vote down vote up
def print_all_logging_handlers():
    for k, v in logging.Logger.manager.loggerDict.items():
        print("+ [%s] {%s} " % (str.ljust(k, 20), str(v.__class__)[8:-2]))
        if not isinstance(v, logging.PlaceHolder):
            for h in v.handlers:
                print("     +++", str(h.__class__)[8:-2]) 
Example #6
Source File: slogging.py    From pyquarkchain with MIT License 5 votes vote down vote up
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()
        handler = StreamHandler(sys.stdout)
        #formatter = Formatter(log_format)
        formatter = QKCLogFormatter()
        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)
            if config_string == ":{}".format(DEFAULT_LOGLEVEL):
                logger.propagate = True
            else:
                logger.propagate = True

    for name_levels in config_string.split(','):
        name, _, level = name_levels.partition(':')
        logger = getLogger(name)
        logger.setLevel(level.upper())
        logger.propagate = True 
Example #7
Source File: rate_limit.py    From oslo.log with Apache License 2.0 5 votes vote down vote up
def _iter_loggers():
    """Iterate on existing loggers."""

    # Sadly, Logger.manager and Manager.loggerDict are not documented,
    # but there is no logging public function to iterate on all loggers.

    # The root logger is not part of loggerDict.
    yield logging.getLogger()

    manager = logging.Logger.manager
    for logger in manager.loggerDict.values():
        if isinstance(logger, logging.PlaceHolder):
            continue
        yield logger 
Example #8
Source File: log.py    From oslo.log with Apache License 2.0 5 votes vote down vote up
def _iter_loggers():
    """Iterate on existing loggers."""

    # Sadly, Logger.manager and Manager.loggerDict are not documented,
    # but there is no logging public function to iterate on all loggers.

    # The root logger is not part of loggerDict.
    yield logging.getLogger()

    manager = logging.Logger.manager
    for logger in manager.loggerDict.values():
        if isinstance(logger, logging.PlaceHolder):
            continue
        yield logger 
Example #9
Source File: log.py    From expressvpn_leak_testing with MIT License 5 votes vote down vote up
def terminate():
        for _, logger in logging.Logger.manager.loggerDict.items():
            if isinstance(logger, logging.PlaceHolder):
                continue
            for handler in logger.handlers:
                if isinstance(handler, logging.FileHandler):
                    handler.close()

    # TODO: These are suboptimal as they getLogger each time. Any real performance hit here?
    # TODO: Consider finding all newlines in the msg and ensuring the log prefix is added for each
    # newline. Would improve formatting for certain stuff. 
Example #10
Source File: log.py    From patroni with MIT License 5 votes vote down vote up
def update_loggers(self):
        loggers = deepcopy(self._config.get('loggers') or {})
        for name, logger in self._root_logger.manager.loggerDict.items():
            if not isinstance(logger, logging.PlaceHolder):
                level = loggers.pop(name, logging.NOTSET)
                logger.setLevel(level)

        for name, level in loggers.items():
            logger = self._root_logger.manager.getLogger(name)
            logger.setLevel(level) 
Example #11
Source File: infra_logopts.py    From RackHD with Apache License 2.0 5 votes vote down vote up
def __recurse_list(self, indent, logger, detail):
        """
        Print the passed in logger, its handlers, and then recurse on other
        loggers under its space.
        """
        indent_txt = ' ' * indent
        print >>sys.stderr, 'L{0}{1}: level={2}'.format(indent_txt, logger.name, logger.level)
        for handler in logger.handlers:
            self.__display_handler(indent + 1, handler, detail)
        for lg_name, lg_obj in logging.Logger.manager.loggerDict.items():
            if isinstance(lg_obj, logging.PlaceHolder):
                continue
            if lg_obj.parent == logger:
                self.__recurse_list(indent + 3, lg_obj, detail) 
Example #12
Source File: logSetup.py    From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def initLogging(logLevel=logging.INFO):

	global LOGGING_INITIALIZED
	if LOGGING_INITIALIZED:

		print("ERROR - Logging initialized twice!")
		try:
			print(traceback.format_exc())
			return
		except Exception:
			pass

	LOGGING_INITIALIZED = True

	print("Setting up loggers....")

	if not os.path.exists(os.path.join("./logs")):
		os.mkdir(os.path.join("./logs"))

	mainLogger = logging.getLogger()			# Main logger
	mainLogger.setLevel(logLevel)

	ch = ColourHandler()
	mainLogger.addHandler(ch)

	logName	= "log - %s.txt" % (time.strftime("%Y-%m-%d %H;%M;%S", time.gmtime()))

	errLogHandler = RobustFileHandler(os.path.join("./logs", logName))
	errLogHandler.setLevel(logging.INFO)
	# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
	# errLogHandler.setFormatter(formatter)

	# mainLogger.addHandler(errLogHandler)

	# Install override for excepthook, to catch all errors
	sys.excepthook = exceptHook

	logtst = logging.getLogger("Main.Test")
	logtst.info("Logging Active")

	print("done")


	# print("Enumerating loggers:")
	# for k,v in  logging.Logger.manager.loggerDict.items()  :
	# 	print('+ [%s] {%s} ' % (str.ljust( k, 20)  , str(v.__class__)[8:-2]) )
	# 	if not isinstance(v, logging.PlaceHolder):
	# 		for h in v.handlers:
	# 			print('     +++',str(h.__class__)[8:-2] )
	# print("Done listing")