Python logging.level() Examples

The following are 12 code examples for showing how to use logging.level(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module logging , or try the search function .

Example 1
Project: ansibullbot   Author: ansible   File: meta_component_dump.py    License: GNU General Public License v3.0 6 votes vote down vote up
def set_logger():
    logging.level = logging.DEBUG
    logFormatter = \
        logging.Formatter("%(asctime)s %(levelname)s %(message)s")
    rootLogger = logging.getLogger()
    rootLogger.setLevel(logging.DEBUG)
    logfile = '/tmp/ansibullbot.log'
    fileHandler = logging.FileHandler("{0}/{1}".format(
            os.path.dirname(logfile),
            os.path.basename(logfile))
    )
    fileHandler.setFormatter(logFormatter)
    rootLogger.addHandler(fileHandler)
    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(logFormatter)
    rootLogger.addHandler(consoleHandler) 
Example 2
Project: BAC0   Author: ChristianTremblay   File: notes.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def convert_level(level):
    if not level:
        return None
    _valid_levels = [
        logging.DEBUG,
        logging.INFO,
        logging.WARNING,
        logging.ERROR,
        logging.CRITICAL,
    ]
    if level in _valid_levels:
        return level
    if level.lower() == "info":
        return logging.INFO
    elif level.lower() == "debug":
        return logging.DEBUG
    elif level.lower() == "warning":
        return logging.WARNING
    elif level.lower() == "error":
        return logging.ERROR
    elif level.lower() == "critical":
        return logging.CRITICAL
    raise ValueError(
        "Wrong log level use one of the following : {}".format(_valid_levels)
    ) 
Example 3
Project: strawberryfields   Author: XanaduAI   File: logger.py    License: Apache License 2.0 5 votes vote down vote up
def create_logger(name, level=logging.INFO):
    """Get the Strawberry Fields module specific logger and configure it if needed.

    Configuration only takes place if no user configuration was applied to the
    logger. Therefore, the logger is configured if and only if the following
    are true:

    - the logger has WARNING as effective level,
    - the level of the logger was not explicitly set,
    - no handlers were added to the logger.

    As the root logger has a WARNING level by default, any module specific
    logger will inherit the same as effective level.

    The default handler that is used for configuration writes to the standard
    error stream and uses a datetime and level formatter.

    Args:
        name (str): the name of the module for which the logger is being created
        level (logging.level): the logging level to set for the logger
    """
    logger = logging.getLogger(name)

    effective_level_inherited = logger.getEffectiveLevel() == logging.WARNING
    level_not_set = not logger.level
    no_handlers = not logging_handler_defined(logger)

    if effective_level_inherited and level_not_set and no_handlers:
        logger.setLevel(level)
        logger.addHandler(default_handler)

    return logger 
Example 4
Project: vspk-python   Author: nuagenetworks   File: utils.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_log_level(level, handler=None):
    """ Set both vspk and Bambou log level to the given level

        Args:
            level (logging.level): a logging level
            handler (logging.Handler): a logging handler

        Notes:
            if no handler is provided, it will automatically create a new StreamHandler.

        Examples:
            >>> set_log_level(logging.INFO)
            >>> set_log_level(logging.DEBUG, logging.Streamhandler())
            >>> set_log_level(logging.ERROR)

    """

    from bambou import bambou_logger, pushcenter_logger

    if handler is None:
        handler = logging.StreamHandler()

    bambou_logger.setLevel(level)
    bambou_logger.addHandler(handler)

    pushcenter_logger.setLevel(level)
    pushcenter_logger.addHandler(handler)

    logger.setLevel(level)
    logger.addHandler(handler) 
Example 5
Project: ansibullbot   Author: ansible   File: logs.py    License: GNU General Public License v3.0 5 votes vote down vote up
def set_logger(debug=False, logfile=None):

    logFormatter = \
            logging.Formatter("%(asctime)s %(levelname)s %(process)d %(filename)s:%(funcName)s:%(lineno)d %(message)s")
    rootLogger = logging.getLogger()

    if debug:
        logging.level = logging.DEBUG
        rootLogger.setLevel(logging.DEBUG)
    else:
        logging.level = logging.INFO
        rootLogger.setLevel(logging.INFO)

    if logfile:
        try:
            logdir = os.path.dirname(logfile)
            if logdir and not os.path.isdir(logdir):
                os.makedirs(logdir)
            fileHandler = WatchedFileHandler(logfile)
            fileHandler.setFormatter(logFormatter)
            rootLogger.addHandler(fileHandler)
        except Exception as e:
            pass

    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(logFormatter)
    rootLogger.addHandler(consoleHandler) 
Example 6
Project: ansibullbot   Author: ansible   File: matching_test.py    License: GNU General Public License v3.0 5 votes vote down vote up
def set_logger():
    logging.level = logging.DEBUG

    logFormatter = \
        logging.Formatter("%(asctime)s %(levelname)s %(message)s")
    rootLogger = logging.getLogger()
    rootLogger.setLevel(logging.DEBUG)
    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(logFormatter)
    rootLogger.addHandler(consoleHandler) 
Example 7
Project: ansibullbot   Author: ansible   File: notifications_study.py    License: GNU General Public License v3.0 5 votes vote down vote up
def main():

    logging.level = logging.DEBUG
    logFormatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
    rootLogger = logging.getLogger()
    rootLogger.setLevel(logging.DEBUG)
    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(logFormatter)
    rootLogger.addHandler(consoleHandler)

    scraper = Scraper() 
Example 8
Project: PKURunningHelper   Author: fieryd   File: class_.py    License: MIT License 5 votes vote down vote up
def __init__(self, name=None):
        self.logger = logging.getLogger(name or self.Default_Name)
        self.level = logging.DEBUG if self.config.getboolean("Base", "debug") else logging.INFO
        self.logger.setLevel(self.level)
        self.add_handler(self.console_headler) 
Example 9
Project: PKURunningHelper   Author: fieryd   File: class_.py    License: MIT License 5 votes vote down vote up
def console_headler(self):
        console_headler = logging.StreamHandler(sys.stdout)
        console_headler.setLevel(self.level)
        console_headler.setFormatter(self.format)
        return console_headler 
Example 10
Project: shipyard   Author: airshipit   File: logging_config.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self,
                 level,
                 named_levels=None,
                 format_string=None,
                 additional_fields=None):
        self.level = level
        # Any false values passed for named_levels should instead be treated as
        # an empty dictionary i.e. no special log levels
        self.named_levels = named_levels or {}
        # Any false values for format string should use the default instead
        self.format_string = format_string or LoggingConfig._default_log_format
        self.additional_fields = additional_fields 
Example 11
Project: shipyard   Author: airshipit   File: logging_config.py    License: Apache License 2.0 5 votes vote down vote up
def setup_logging(self):
        """ Establishes the base logging using the appropriate filter
        attached to the console/stream handler.
        """
        console_handler = logging.StreamHandler()
        request_logging.assign_request_filter(console_handler,
                                              self.additional_fields)
        logging.basicConfig(level=self.level,
                            format=self.format_string,
                            handlers=[console_handler])
        for handler in logging.root.handlers:
            handler.setFormatter(RedactionFormatter(handler.formatter))
        logger = logging.getLogger(__name__)
        logger.info('Established logging defaults')
        self._setup_log_levels() 
Example 12
Project: shipyard   Author: airshipit   File: logging_config.py    License: Apache License 2.0 5 votes vote down vote up
def _setup_log_levels(self):
        """Sets up the logger levels for named loggers

        :param named_levels: dict to set each of the specified
            logging levels.
        """
        for logger_name, level in self.named_levels.items():
            logger = logging.getLogger(logger_name)
            logger.setLevel(level)
            LOG.info("Set %s to use logging level %s", logger_name, level)