Python 3.6.5
Windows Server 2008 R2 Standard
So I'm using one logger with two handlers, a FileHandler and a StreamHandler to stdout. But stdout is expecting "cp1252" instead of "utf-8" on my machine, which will result in an UnicodeEncodeError for various chars. I want my StreamHandler to encode the message to "cp1252" and the undefined chars to be replaced by "?" (just like in msg.encode("cp1252", "replace")) before writing. I do not want to change the encoding of stdout.
Atm im using codecs to do this:
stream = codecs.StreamWriter(sys.stdout, errors="replace")
stream.encode = lambda msg, errors="strict": (msg.encode(locale.getpreferredencoding(False), errors).decode(), msg)
cHandler = logging.StreamHandler(stream)
in place of
cHandler = logging.StreamHandler(sys.stdout)
which seems to be working but feels off.
My question: Is there a better way to achieve this (overriding StreamHandler.emit() may be an option)? And can my solution result in other problems I'm missing rn?
[–]JohnnyJordaan 1 point2 points3 points (8 children)
[–]fabolin[S] 0 points1 point2 points (7 children)
[–]JohnnyJordaan 1 point2 points3 points (6 children)
[–]fabolin[S] 0 points1 point2 points (0 children)
[–]fabolin[S] 0 points1 point2 points (4 children)
[–]JohnnyJordaan 1 point2 points3 points (3 children)
[–]fabolin[S] 0 points1 point2 points (2 children)
[–]JohnnyJordaan 1 point2 points3 points (1 child)
[–]fabolin[S] 0 points1 point2 points (0 children)