log: Simplify encoding in BetterFileHandler.

I don't think any these cases are useful anymore.

Let's keep the last one anyway, just in case I'm wrong.
This commit is contained in:
Valentin Lorentz 2021-10-14 23:45:28 +02:00
parent e972894b8a
commit 666acd9f1f

View File

@ -127,20 +127,14 @@ class StdoutStreamHandler(logging.StreamHandler):
class BetterFileHandler(logging.FileHandler):
def emit(self, record):
msg = self.format(record)
try:
self.stream.write(msg)
except (UnicodeError, TypeError):
try:
self.stream.write(msg.encode("utf8"))
super().emit(record)
except (UnicodeError, TypeError):
try:
self.stream.write(msg.encode("utf8").decode('ascii', 'replace'))
except (UnicodeError, TypeError):
self.stream.write(repr(msg))
self.stream.write(os.linesep)
try:
self.flush()
msg = self.format(record)
self.stream.write(repr(msg))
self.stream.write(os.linesep)
self.flush()
except OSError as e:
if e.args[0] == 28:
print('No space left on device, cannot flush log.')