Catch more unicode-related exceptions when trying to write logs to a file.

This commit is contained in:
Valentin Lorentz 2016-02-09 18:39:55 +01:00
parent 6d4ea07b72
commit a538920df7

View File

@ -127,16 +127,14 @@ class StdoutStreamHandler(logging.StreamHandler):
class BetterFileHandler(logging.FileHandler):
def emit(self, record):
msg = self.format(record)
if not hasattr(types, "UnicodeType"): #if no unicode support...
try:
self.stream.write(msg)
self.stream.write(os.linesep)
else:
except (UnicodeError, TypeError):
try:
self.stream.write(msg)
self.stream.write(os.linesep)
except UnicodeError:
self.stream.write(msg.encode("utf8"))
self.stream.write(os.linesep)
except (UnicodeError, TypeError):
self.stream.write(msg.encode("utf8").decode('ascii', 'replace'))
self.stream.write(os.linesep)
try:
self.flush()
except OSError as e: