-
-
Save adiroiban/7906603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _patched_emit(self, record): | |
""" | |
Emit a record. | |
If a formatter is specified, it is used to format the record. | |
The record is then written to the stream with a trailing newline. If | |
exception information is present, it is formatted using | |
traceback.print_exception and appended to the stream. If the stream | |
has an 'encoding' attribute, it is used to determine how to do the | |
output to the stream. | |
""" | |
try: | |
msg = self.format(record) | |
stream = self.stream | |
if not logging._unicode: | |
#if no unicode support... | |
stream.write(self.FS % msg) | |
else: | |
try: | |
if (isinstance(msg, unicode) and | |
getattr(stream, 'encoding', None)): | |
ufs = self.FS.decode(stream.encoding) | |
try: | |
stream.write(ufs % msg) | |
except UnicodeEncodeError: | |
#Printing to terminals sometimes fails. For | |
#example, with an encoding of 'cp1251', the above | |
#write will work if written to a stream opened or | |
#wrapped by the codecs module, but fail when | |
#writing to a terminal even when the codepage is | |
#set to cp1251. An extra encoding step seems to | |
#be needed. | |
stream.write((ufs % msg).encode(stream.encoding)) | |
else: | |
stream.write(self.FS % msg) | |
except UnicodeError: | |
stream.write(self.FS % msg.encode("UTF-8")) | |
self.flush() | |
except (KeyboardInterrupt, SystemExit): | |
raise | |
except: | |
self.handleError(record) | |
# Patch the based file hander. | |
logging.FileHandler.FS = "%s" + os.linesep | |
logging.FileHandler.emit = _patched_emit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment