def test_custom_formatter(path, parser): """Should use a custom formatter instead of Python's default.""" path.return_value.exists.return_value = False # Make sure we have the custome formatter section parser.__contains__.return_value = True handler = Mock() LogManager.add_handler(handler) handler.setFormatter.assert_called_once()
def test_parent_handler_usage(): """Existent logger should use a new handler.""" # First, get a child logger. old_logger = logging.getLogger('non-root logger') # Then, add a new handler. new_handler = Mock(level=logging.DEBUG) LogManager.add_handler(new_handler) old_logger.setLevel(logging.DEBUG) old_logger.debug('Handler should receive me.') new_handler.handle.assert_called_once()
def test_handler_filter(self): """Should not log harmless werkzeug "session is disconnected" msg.""" logging.root.handlers = [] handler = Mock(level=logging.WARNING) LogManager.add_handler(handler) # Message based on the log output that ends with traceback plaintext as # seen in site-packages/werkzeug/serving.py:225 of Werkzeug==0.12.1 msg = "lorem ipsum KeyError: 'Session is disconnected'" logger = logging.getLogger('werkzeug') logger.setLevel(logging.ERROR) with patch.object(handler, 'emit'): logger.error('lorem ipsum %s', msg) self.assertEqual(0, handler.emit.call_count)
def test_handler_filter(self): """Should not log harmless werkzeug "session is disconnected" msg.""" logging.root.handlers = [] handler = Mock(level=logging.WARNING) LogManager.add_handler(handler) # Message based on the log output that ends with traceback plaintext as # seen in lib/python3.6/site-packages/werkzeug/serving.py:225 of # Werkzeug==0.12.1 msg = "lorem ipsum KeyError: 'Session is disconnected'" logger = logging.getLogger('werkzeug') logger.setLevel(logging.ERROR) with patch.object(handler, 'emit'): logger.error('lorem ipsum %s', msg) self.assertEqual(0, handler.emit.call_count)
def test_custom_formatter(path, parser): """Should use a custom formatter instead of Python's default.""" path.return_value.exists.return_value = False # Make sure we have the custome formatter section parser.__contains__.return_value = True # Make 'parser' behave as a dict, this is necessary because the _PARSER # is being patched returning a MagicMock (inside add_handler) and in # 'logging' module (python 3.8) the 'Formatter' class includes a # 'validate' method that breaks when receives a MagicMock object. format_dict = {'formatter_console': {'format': None}} parser.__getitem__.side_effect = format_dict.__getitem__ handler = Mock() LogManager.add_handler(handler) handler.setFormatter.assert_called_once()
def test_add_handler_to_root(self): """Handler should be added to root logger.""" handler = Mock() LogManager.add_handler(handler) self.assertIn(handler, logging.root.handlers)