def setUp(self): """ Set up the mocks and a new BoundLog. """ self.msg = mock.Mock() self.err = mock.Mock() self.log = BoundLog(self.msg, self.err)
class BoundLogTests(TestCase): """ Test the BoundLog utility. """ def setUp(self): """ Set up the mocks and a new BoundLog. """ self.msg = mock.Mock() self.err = mock.Mock() self.log = BoundLog(self.msg, self.err) def test_bind_msg(self): """ bind saves it's keyword arguments and passes them to msg when it is called. """ log = self.log.bind(system='hello') log.msg('Hi there') self.msg.assert_called_once_with('Hi there', system='hello') def test_bind_err(self): """ bind saves it's keyword arguments and passes them to err when it is called. """ exc = ValueError('uh oh') log = self.log.bind(system='hello') log.err(exc) self.err.assert_called_once_with(exc, system='hello')
def test_kwargs_order(self): """ kwargs bound in order, i.e. next bound overriding previous bound should retain the value """ log = BoundLog(lambda: None, lambda: None).bind(a=10, b=20).bind(a=3) self.assertIsNone(IsBoundWith(a=3, b=20).match(log))
def test_not_match_kwargs(self): """ Returns mismatch on non-matching kwargs """ log = BoundLog(lambda: None, lambda: None).bind(a=10, b=2) self.assertEqual( self.bound.match(log).describe(), 'Expected kwargs {} but got {} instead'.format(dict(a=10, b=20), dict(a=10, b=2)))
def mock_log(*args, **kwargs): """ Returns a BoundLog whose msg and err methods are mocks. Makes it easier to test logging, since instead of making a mock object and testing:: log.bind.return_value.msg.assert_called_with(...) This can be done instead:: log.msg.assert_called_with(mock.ANY, bound_value1="val", ...) Since in all likelyhood, testing that certain values are bound would be more important than testing the exact logged message. """ msg = mock.Mock(spec=[]) msg.return_value = None err = mock.Mock(spec=[]) err.return_value = None return BoundLog(msg, err)
def test_nested_match(self): """ works with Nested BoundLog """ log = BoundLog(lambda: None, lambda: None).bind(a=10, b=20).bind(c=3) self.assertIsNone(IsBoundWith(a=10, b=20, c=3).match(log))
def test_match_kwargs(self): """ Returns None on matching kwargs """ log = BoundLog(lambda: None, lambda: None).bind(a=10, b=20) self.assertIsNone(self.bound.match(log))
""" Package for all otter specific logging functionality. """ from otter.log.setup import observer_factory, observer_factory_debug from otter.log.bound import BoundLog from twisted.python.log import msg, err log = BoundLog(msg, err).bind(system='otter') def audit(log): """ Single method to ensure that the log object is an audit log (by binding the audit log param) :param log: a bound log object :returns: a bound log object with keyword that specifies it as an audit log already bound """ return log.bind(audit_log=True) __all__ = ['observer_factory', 'observer_factory_debug', 'log']