AdminZulipHandler: Support passing a record without a request.

This commit is contained in:
Tim Abbott 2017-03-25 21:54:56 -07:00
parent efa151b488
commit 267346f5fb
2 changed files with 24 additions and 1 deletions

View File

@ -78,7 +78,8 @@ class AdminZulipHandler(logging.Handler):
message = record.getMessage(),
stack_trace = stack_trace,
)
add_request_metadata(report, record.request) # type: ignore # record.request is added dynamically
if hasattr(record, "request"):
add_request_metadata(report, record.request) # type: ignore # record.request is added dynamically
except Exception:
traceback.print_exc()
report = dict(

View File

@ -55,6 +55,28 @@ class AdminZulipHandlerTest(ZulipTestCase):
# type: () -> None
settings.LOGGING_NOT_DISABLED = True
def get_admin_zulip_handler(self, logger):
# type: (logging.Logger) -> Any
# Ensure that AdminEmailHandler does not get filtered out
# even with DEBUG=True.
admin_email_handler = [
h for h in logger.handlers
if h.__class__.__name__ == "AdminZulipHandler"
][0]
return admin_email_handler
def test_basic(self):
# type: () -> None
"""A random exception passes happily through AdminZulipHandler"""
handler = self.get_admin_zulip_handler(self.logger)
try:
raise Exception("Testing Error!")
except Exception:
exc_info = sys.exc_info()
record = self.logger.makeRecord('name', logging.ERROR, 'function', 16, 'message', None, exc_info) # type: ignore # https://github.com/python/typeshed/pull/1100
handler.emit(record)
def run_handler(self, record):
# type: (logging.LogRecord) -> Dict[str, Any]
with patch('zerver.logging_handlers.queue_json_publish') as patched_publish: