Python logging.RootLogger() Examples

The following are 16 code examples for showing how to use logging.RootLogger(). 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: Nest   Author: ZhouYanzhao   File: cli.py    License: MIT License 6 votes vote down vote up
def hook_exceptions(self, logger: logging.RootLogger) -> None:
        """Format excetion traceback.
        
        Parameters:
            logger:
                The logger for logging exceptions.
        """

        def _hook(exc_type, value, exc_tb) -> None:
            nest_dir = os.path.dirname(os.path.abspath(__file__))
            traceback_str = ''
            idx = 0
            for file_name, line_number, func_name, text in traceback.extract_tb(exc_tb)[1:]:
                # skip Nest-related tracebacks to make it more readable
                if os.path.dirname(os.path.abspath(file_name)) == nest_dir:
                    continue
                idx += 1
                traceback_str += '\n  [%d] File "%s", line %d, in function "%s"\n    %s' % \
                    (idx, file_name, line_number, func_name, text)
            if traceback_str != '':
                traceback_str = 'Traceback: ' + traceback_str
            logger.critical('Exception occurred during resolving:\nType: %s\nMessage: %s\n%s' % \
                (exc_type.__name__, value, traceback_str))

        sys.excepthook = _hook 
Example 2
Project: OasisLMF   Author: OasisLMF   File: test_log.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_log_config_is_loaded___logger_is_updated(self, level):
        log_file_path = os.path.abspath(os.path.join("tmp", "log_file.txt"))
        with patch('logging.root', logging.RootLogger(logging.NOTSET)):
            read_log_config({
                'LOG_FILE': log_file_path,
                'LOG_LEVEL': level,
                'LOG_MAX_SIZE_IN_BYTES': 100,
                'LOG_BACKUP_COUNT': 10,
            })

            logger = logging.getLogger()

            self.assertEqual(level, logger.level)

            self.assertEqual(1, len(logger.handlers))
            handler = logger.handlers[0]
            self.assertIsInstance(handler, RotatingFileHandler)
            self.assertEqual(log_file_path, handler.baseFilename)
            self.assertEqual(100, handler.maxBytes)
            self.assertEqual(10, handler.backupCount) 
Example 3
Project: Transcrypt   Author: QQuick   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def resetLogging():
    """ Reset the handlers and loggers so that we
    can rerun the tests starting from a blank slate.
    """
    __pragma__("skip")
    logging._handlerList = []

    import weakref
    logging._handlers = weakref.WeakValueDictionary()

    logging.root = logging.RootLogger(logging.WARNING)
    logging.Logger.root = logging.root
    logging.Logger.manager = logging.Manager(logging.root)
    logging.root.manager = logging.Logger.manager
    __pragma__("noskip")

    if __envir__.executor_name == __envir__.transpiler_name:
        logging._resetLogging() 
Example 4
Project: puzzle   Author: robinandeer   File: log.py    License: MIT License 6 votes vote down vote up
def configure_stream(level='WARNING'):
    """Configure root logger using a standard stream handler.

    Args:
        level (string, optional): lowest level to log to the console

    Returns:
        logging.RootLogger: root logger instance with attached handler
    """
    # get the root logger
    root_logger = logging.getLogger()
    # set the logger level to the same as will be used by the handler
    root_logger.setLevel(level)

    # customize formatter, align each column
    template = "[%(asctime)s] %(name)-25s %(levelname)-8s %(message)s"
    formatter = logging.Formatter(template)

    # add a basic STDERR handler to the logger
    console = logging.StreamHandler()
    console.setLevel(level)
    console.setFormatter(formatter)

    root_logger.addHandler(console)
    return root_logger 
Example 5
Project: Nest   Author: ZhouYanzhao   File: logger.py    License: MIT License 5 votes vote down vote up
def setup_logger() -> logging.RootLogger:
    """Initialize logger.

    Returns:
        The global logger
    """

    # set up logger
    logger = logging.getLogger('Nest')
    logger.setLevel(logging.DEBUG)
    # create a formatter and add it to the handlers
    screen_formatter = logging.Formatter('%(message)s')
    # create a console handler
    screen_handler = logging.StreamHandler()
    screen_handler.setLevel(logging.INFO)
    screen_handler.setFormatter(screen_formatter)
    screen_handler.addFilter(ExceptionFilter())
    logger.addHandler(screen_handler)
    if settings['LOGGING_TO_FILE']:
        # create a file handler which logs warning and error messages
        file_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        file_handler = logging.FileHandler(settings['LOGGING_PATH'], encoding='utf8')
        file_handler.setLevel(logging.WARNING)
        file_handler.setFormatter(file_formatter)
        logger.addHandler(file_handler)

    return logger 
Example 6
Project: cookiecutter-aiohttp-sqlalchemy   Author: aalhour   File: logger.py    License: MIT License 5 votes vote down vote up
def get_logger():
    """
    Returns the application's logger instance by name.

    :rtype: logging.Logger.
    :return: logger instance.
    """
    _logger = logging.getLogger(APP_NAME)

    if isinstance(_logger, logging.RootLogger):
        _logger = make_logger()

    return _logger 
Example 7
Project: cookiecutter-aiohttp-sqlalchemy   Author: aalhour   File: logger.py    License: MIT License 5 votes vote down vote up
def get_logger():
    """
    Returns the application's logger instance by name.

    :rtype: logging.Logger.
    :return: logger instance.
    """
    _logger = logging.getLogger(APP_NAME)

    if isinstance(_logger, logging.RootLogger):
        _logger = make_logger()

    return _logger 
Example 8
Project: ubuntu-advantage-client   Author: canonical   File: conftest.py    License: GNU General Public License v3.0 5 votes vote down vote up
def logging_sandbox():
    # Monkeypatch a replacement root logger, so that our changes to logging
    # configuration don't persist outside of the test
    root_logger = logging.RootLogger(logging.WARNING)

    with mock.patch.object(logging, "root", root_logger):
        with mock.patch.object(logging.Logger, "root", root_logger):
            with mock.patch.object(
                logging.Logger, "manager", logging.Manager(root_logger)
            ):
                yield 
Example 9
Project: OasisLMF   Author: OasisLMF   File: test_log.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_rotating_log_config_is_loaded___logger_is_set(self, level):
        log_file_path = os.path.abspath(os.path.join("tmp", "log_file.txt"))
        with patch('logging.root', logging.RootLogger(logging.NOTSET)):
            set_rotating_logger(log_file_path=log_file_path, log_level=level, max_file_size=100, max_backups=10)

            logger = logging.getLogger()

            self.assertEqual(level, logger.level)

            self.assertEqual(1, len(logger.handlers))
            handler = logger.handlers[0]
            self.assertIsInstance(handler, RotatingFileHandler)
            self.assertEqual(log_file_path, handler.baseFilename)
            self.assertEqual(100, handler.maxBytes)
            self.assertEqual(10, handler.backupCount) 
Example 10
Project: OasisLMF   Author: OasisLMF   File: test_base.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        self._orig_root_logger = logging.root
        logging.root = logging.RootLogger(logging.WARNING) 
Example 11
Project: st2   Author: StackStorm   File: sensor.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, sensor_wrapper):
        self._sensor_wrapper = sensor_wrapper

        # Holds a mock logger instance
        # We use a Mock class so use can assert logger was called with particular arguments
        self._logger = Mock(spec=RootLogger)

        # Holds a list of triggers which were dispatched
        self.dispatched_triggers = []

        self._datastore_service = MockDatastoreService(logger=self._logger,
                                                       pack_name=self._sensor_wrapper._pack,
                                                       class_name=self._sensor_wrapper._class_name) 
Example 12
Project: st2   Author: StackStorm   File: action.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, action_wrapper):
        self._action_wrapper = action_wrapper

        # Holds a mock logger instance
        # We use a Mock class so use can assert logger was called with particular arguments
        self._logger = Mock(spec=RootLogger)

        self._datastore_service = MockDatastoreService(logger=self._logger,
                                                       pack_name=self._action_wrapper._pack,
                                                       class_name=self._action_wrapper._class_name) 
Example 13
Project: loguru   Author: Delgan   File: conftest.py    License: MIT License 5 votes vote down vote up
def reset_logger():
    def reset():
        loguru.logger.remove()
        loguru.logger.__init__(
            loguru._logger.Core(), None, 0, False, False, False, False, True, None, {}
        )
        loguru._logger.context.set({})
        logging.Logger.manager.loggerDict.clear()
        logging.root = logging.RootLogger("WARNING")

    reset()
    yield
    reset() 
Example 14
Project: video2mocap   Author: robertjoosten   File: logging.py    License: MIT License 5 votes vote down vote up
def get_logger(path):
    """
    Returns a logger object that writes a log file to disk ( provided path )
    and to the stderr output.

    :param str path:
    :return: Logger
    :rtype: logging.RootLogger
    """

    # get logger
    global logger
    logger = logging.getLogger()

    # create logging file
    if path:
        logging_path = os.path.join(path, "log.log")
        logging.basicConfig(
            filename=logging_path,
            level=logging.DEBUG,
            format="%(asctime)s | %(levelname)s | %(message)s",
            datefmt="%Y/%m/%d/ %I:%M:%S",
        )
    
    # create logging to stderr
    stderr_logger = logging.StreamHandler()
    stderr_logger.setLevel(logging.INFO)
    stderr_logger.setFormatter(
        logging.Formatter("%(levelname)s | %(message)s")
    )

    logger.addHandler(stderr_logger)

    return logger 
Example 15
Project: video2mocap   Author: robertjoosten   File: logging.py    License: MIT License 5 votes vote down vote up
def get_maya_logger(path):
    """
    Returns a logger object that writes a log file to disk ( provided path )
    and to the stderr output.

    :param str path:
    :return: Logger
    :rtype: logging.RootLogger
    """
    # get logger
    global logger
    logger = logging.getLogger()

    # remove existing handlers
    for handler in logger.handlers:
        logger.removeHandler(handler)

    # create logging file
    if path:
        logging_path = os.path.join(path, "log.log")
        file_logger = logging.FileHandler(logging_path)
        file_logger.setLevel(logging.DEBUG)
        file_logger.setFormatter(
            logging.Formatter(
                "%(asctime)s | %(levelname)s | %(message)s",
                "%Y/%m/%d/ %I:%M:%S"
            )
        )

        logger.addHandler(file_logger)

    # create logging to stderr
    stderr_logger = logging.StreamHandler()
    stderr_logger.setLevel(logging.INFO)
    stderr_logger.setFormatter(
        logging.Formatter("%(levelname)s | %(message)s")
    )

    logger.addHandler(stderr_logger)

    return logger 
Example 16
Project: fast-autocomplete   Author: seperman   File: loader.py    License: MIT License 5 votes vote down vote up
def get_data(filepath: str, compress: bool = False,
             redis_client: Optional[StrictRedis] = None,
             redis_key_prefix: Optional[str] = None,
             logger: Optional[logging.RootLogger] = None) -> Dict[str, List[str]]:
    data_json = None
    filename = os.path.basename(filepath)
    if redis_client and redis_key_prefix:
        key = redis_key_prefix.format(filename)
        try:
            data_json = redis_client.get(key)
        except Exception:
            if logger:
                logger.exception('Unable to get the search graph words from Redis.')
            else:
                print('Unable to get the search graph words from Redis.')
        if data_json:
            data_json = gzip.decompress(data_json).decode('utf-8')
    if not data_json:
        data_json = read_local_dump(filepath)
    data = json.loads(data_json)

    if compress:
        hash_to_val = {}

        for word, value in data.items():
            context, display, count = value
            display = _simple_compress(item=display, hash_to_val=hash_to_val)
            for key, val in context.items():
                context[key] = _simple_compress(
                    item=context[key], hash_to_val=hash_to_val
                )
            data[word] = WordValue(context=context, display=display, count=count)

    return data