def test_notify_context(self): with mock.patch('requests.post') as requests_post: version = platform.python_version() plat = platform.platform() environment = u"testing123" root_directory = u"/home/app/" ab = Airbrake(project_id=1234, api_key='fake', environment=environment, root_directory=root_directory) ab.log("this is a test") expected_context = { u'notifier': { u'name': u'airbrake-python', u'version': __version__, u'url': __url__ }, u'os': plat, u'hostname': socket.gethostname(), u'language': u'Python/%s' % version, u'environment': environment, u'rootDirectory': root_directory, u'severity': ErrorLevels.DEFAULT_LEVEL, } data = json.loads(requests_post.call_args[1]['data']) actual_context = data["context"] self.assertEqual(expected_context, actual_context)
def test_log_with_severity(self): with mock.patch('requests.post') as requests_post: ab = Airbrake(project_id=1234, api_key='fake') ab.log("this is a test", severity=ErrorLevels.CRITICAL) data = json.loads(requests_post.call_args[1]['data']) actual_context = data["context"] self.assertEqual(actual_context[u'severity'], ErrorLevels.CRITICAL)
class AirbrakeHandler(logging.Handler): """A handler class which ships logs to airbrake.io. Requires one: * `project_id` AND `api_key` * an instance of airbrake.Airbrake """ def __init__(self, airbrake=None, level=logging.ERROR, project_id=None, api_key=None, environment=None, base_url=None): """Initialize the Airbrake handler with a default logging level. Default level of logs handled by this class are >= ERROR, which includes ERROR and CRITICAL. To change this behavior supply a different argument for 'level'. """ logging.Handler.__init__(self, level=level) if isinstance(airbrake, Airbrake): self.airbrake = airbrake else: self.airbrake = Airbrake(project_id, api_key, environment, base_url) def emit(self, record): """Log the record airbrake.io style. To prevent method calls which invoke this handler from using the global exception context in sys.exc_info(), exc_info must be passed as False. E.g. To prevent AirbrakeHandler from reading the global exception context, (which it may do to find a traceback and error type), make logger method calls like this: LOG.error("Bad math.", exc_info=False) Otherwise, provide exception context directly, though the following contrived example would be a strange way to use the handler: exc_info = sys.exc_info() ... LOG.error("Bad math.", exc_info=exc_info) """ try: airbrakeerror = airbrake_error_from_logrecord(record) self.airbrake.log(**airbrakeerror) except (KeyboardInterrupt, SystemExit): raise except: # pylint: disable=bare-except self.handleError(record)