Exemple #1
0
 def test_find_handler(self):
     """Make sure find_handler() works as intended."""
     root, parent, child, grand_child = self.get_logger_tree()
     # Add some handlers to the tree.
     stream_handler = logging.StreamHandler()
     syslog_handler = logging.handlers.SysLogHandler()
     child.addHandler(stream_handler)
     parent.addHandler(syslog_handler)
     # Make sure the first matching handler is returned.
     matched_handler, matched_logger = find_handler(grand_child, lambda h: isinstance(h, logging.Handler))
     assert matched_handler is stream_handler
     # Make sure the first matching handler of the given type is returned.
     matched_handler, matched_logger = find_handler(child, lambda h: isinstance(h, logging.handlers.SysLogHandler))
     assert matched_handler is syslog_handler
Exemple #2
0
 def test_find_handler(self):
     """Make sure find_handler() works as intended."""
     root, parent, child, grand_child = self.get_logger_tree()
     # Add some handlers to the tree.
     stream_handler = logging.StreamHandler()
     syslog_handler = logging.handlers.SysLogHandler()
     child.addHandler(stream_handler)
     parent.addHandler(syslog_handler)
     # Make sure the first matching handler is returned.
     matched_handler, matched_logger = find_handler(grand_child, lambda h: isinstance(h, logging.Handler))
     assert matched_handler is stream_handler
     # Make sure the first matching handler of the given type is returned.
     matched_handler, matched_logger = find_handler(child, lambda h: isinstance(h, logging.handlers.SysLogHandler))
     assert matched_handler is syslog_handler
Exemple #3
0
def cleanup_handlers():
    """Context manager to cleanup output handlers."""
    # There's nothing to set up so we immediately yield control.
    yield
    # After the with block ends we cleanup any output handlers.
    for match_func in match_stream_handler, match_syslog_handler:
        handler, logger = find_handler(logging.getLogger(), match_func)
        if handler and logger:
            logger.removeHandler(handler)
Exemple #4
0
def cleanup_handlers():
    """Context manager to cleanup output handlers."""
    # There's nothing to set up so we immediately yield control.
    yield
    # After the with block ends we cleanup any output handlers.
    for match_func in match_stream_handler, match_syslog_handler:
        handler, logger = find_handler(logging.getLogger(), match_func)
        if handler and logger:
            logger.removeHandler(handler)
Exemple #5
0
 def test_colorama_missing(self):
     """Test that colorama is missing (through mocking)."""
     def init_function():
         raise ImportError
     with mocked_colorama_module(init_function):
         # Configure logging to the terminal. It is expected that internally
         # an ImportError is raised, but the exception is caught and colored
         # output is disabled.
         coloredlogs.install()
         # Find the handler that was created by coloredlogs.install().
         handler, logger = find_handler(logging.getLogger(), match_stream_handler)
         # Make sure that logging to the terminal was initialized.
         assert isinstance(handler.formatter, logging.Formatter)
         # Make sure colored logging is disabled.
         assert not isinstance(handler.formatter, ColoredFormatter)
 def test_colorama_missing(self):
     """Test that colorama is missing (through mocking)."""
     def init_function():
         raise ImportError
     with mocked_colorama_module(init_function):
         # Configure logging to the terminal. It is expected that internally
         # an ImportError is raised, but the exception is caught and colored
         # output is disabled.
         coloredlogs.install()
         # Find the handler that was created by coloredlogs.install().
         handler, logger = find_handler(logging.getLogger(), match_stream_handler)
         # Make sure that logging to the terminal was initialized.
         assert isinstance(handler.formatter, logging.Formatter)
         # Make sure colored logging is disabled.
         assert not isinstance(handler.formatter, ColoredFormatter)