def setUp(self): self.logpath = self.mktemp() self.logfile = DummyLogFile(None, self.logpath, None, None) self.obs = JunebugLogObserver(self.logfile, 'worker-1')
class TestSentryLogObserver(JunebugTestBase): def setUp(self): self.logpath = self.mktemp() self.logfile = DummyLogFile(None, self.logpath, None, None) self.obs = JunebugLogObserver(self.logfile, 'worker-1') def assert_log(self, log, expected): '''Assert that a log matches what is expected.''' log = json.loads(log) timestamp = log.pop('timestamp') self.assertTrue(isinstance(timestamp, float)) self.assertEqual(log, expected) def test_level_for_event(self): '''The correct logging level is returned by `level_for_event`.''' for expected_level, event in [ (logging.WARN, {'logLevel': logging.WARN}), (logging.ERROR, {'isError': 1}), (logging.INFO, {}), ]: self.assertEqual(self.obs.level_for_event(event), expected_level) def test_logger_for_event(self): '''The correct logger name is returned by `logger_for_event`.''' self.assertEqual(self.obs.logger_for_event( {'system': 'foo,bar'}), 'foo.bar') def test_log_failure(self): '''A failure should be logged with the correct format.''' e = ValueError("foo error") f = Failure(e) self.obs({ 'failure': f, 'system': 'worker-1', 'isError': 1, 'message': [e.message]}) [log] = self.logfile.logs self.assert_log(log, { 'level': JunebugLogObserver.DEFAULT_ERROR_LEVEL, 'message': 'foo error', 'logger': 'worker-1', 'exception': { 'class': repr(ValueError), 'instance': repr(e), 'stack': [], }, }) def test_log_traceback(self): '''Logging a log with a traceback should place the traceback in the logfile.''' try: raise ValueError("foo") except ValueError: f = Failure(*sys.exc_info()) self.obs({ 'failure': f, 'isError': 1, 'message': ['foo'], 'system': 'worker-1'}) [log] = self.logfile.logs self.assert_log(log, { 'message': 'foo', 'logger': 'worker-1', 'level': logging.ERROR, 'exception': { 'class': repr(f.type), 'instance': repr(ValueError), # json encoding changes all tuples to lists 'stack': json.loads(json.dumps(f.stack)), }, }) def test_log_warning(self): '''Logging an warning level log should generate the correct level log message''' self.obs({ 'message': ["a"], 'system': 'foo', 'logLevel': logging.WARN, 'system': 'worker-1'}) [log] = self.logfile.logs self.assert_log(log, { 'level': logging.WARN, 'logger': 'worker-1', 'message': 'a', }) def test_log_info(self): '''Logging an info level log should generate the correct level log message''' self.obs({'message': ["a"], 'system': 'worker-1'}) [log] = self.logfile.logs self.assert_log(log, { 'logger': 'worker-1', 'message': 'a', 'level': logging.INFO }) def test_log_debug(self): '''Logging a debug level log should not generate a log, since it is below the minimum log level.''' self.obs({'message': ["a"], 'system': 'worker-1', 'logLevel': logging.DEBUG}) self.assertEqual(len(self.logfile.logs), 0) def test_log_with_context_sentinel(self): '''If the context sentinel has been set for a log, it should not be logged again.''' event = {'message': ["a"], 'system': 'worker-1'} event.update(self.obs.log_context) self.obs(event) self.assertEqual(len(self.logfile.logs), 0) def test_log_only_worker_id(self): '''A log should only be created when the worker id is in the system id of the log.''' self.obs({'message': ["a"], 'system': 'worker-1,bar'}) self.assertEqual(len(self.logfile.logs), 1) self.obs({'message': ["a"], 'system': 'worker-2,foo'}) self.assertEqual(len(self.logfile.logs), 1) self.obs({'message': ["a"], 'system': 'worker-1foo,bar'}) self.assertEqual(len(self.logfile.logs), 1) self.obs({'message': ["a"], 'system': None}) self.assertEqual(len(self.logfile.logs), 1)