Previously if there were any bytes that needed escaping, the newlines
would get escaped as well, ruining the message formatting.
Closes #675 (again)
if not record.exc_text:
record.exc_text = self.formatException(record.exc_info)
if record.exc_text:
- formatted = (formatted.rstrip() + "\n" +
- safe_unicode(record.exc_text))
+ # exc_text contains multiple lines. We need to safe_unicode
+ # each line separately so that non-utf8 bytes don't cause
+ # all the newlines to turn into '\n'.
+ lines = [formatted.rstrip()]
+ lines.extend(safe_unicode(ln) for ln in record.exc_text.split('\n'))
+ formatted = '\n'.join(lines)
return formatted.replace("\n", "\n ")
self.logger.exception('caught exception')
# This will be "Exception: \xe9" on python 2 or
# "Exception: b'\xe9'" on python 3.
- self.assertRegexpMatches(self.get_output(), br'Exception.*\\xe9')
+ output = self.get_output()
+ self.assertRegexpMatches(output, br'Exception.*\\xe9')
+ # The traceback contains newlines, which should not have been escaped.
+ self.assertNotIn(br'\n', output)
class UnicodeLogFormatterTest(LogFormatterTest):