def test_mixin(self): loggable = Loggable() self.assertTrue(not hasattr(loggable, "_logger")) loggable.log(mozlog.INFO, "This will instantiate the logger") self.assertTrue(hasattr(loggable, "_logger")) self.assertEqual(loggable._logger.name, "__main__.Loggable") self.assertRaises(ValueError, loggable.set_logger, "not a logger") logger = mozlog.MozLogger('test.mixin') handler = ListHandler() logger.addHandler(handler) loggable.set_logger(logger) self.assertTrue(isinstance(loggable._logger.handlers[0], ListHandler)) self.assertEqual(loggable._logger.name, "test.mixin") loggable.log(mozlog.WARN, 'message for "log" method') loggable.info('message for "info" method') loggable.error('message for "error" method') loggable.log_structured( 'test_message', params={'_message': 'message for ' + '"log_structured" method'}) expected_messages = [ 'message for "log" method', 'message for "info" method', 'message for "error" method', 'message for "log_structured" method' ] actual_messages = loggable._logger.handlers[0].messages self.assertEqual(expected_messages, actual_messages)
def setUp(self): self.handler = ListHandler() self.handler.setFormatter(mozlog.JSONFormatter()) self.logger = mozlog.MozLogger('test.Logger') self.logger.addHandler(self.handler) self.logger.setLevel(mozlog.DEBUG)