This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author sourcejedi
Recipients sourcejedi, vinay.sajip
Date 2019-04-26.15:17:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1556291850.2.0.520401244405.issue36704@roundup.psfhosted.org>
In-reply-to
Content
Oops. I assumed logging.raiseExceptions == True (the default) actually raises exceptions, but it doesn't.  It prints the exception to stderr and continues.

E.g. traditional UNIX daemons run with stderr connected to /dev/null.  Consider during development / debugging, or if you overlooked setting raiseExceptions altogether, or maybe you made the same mistake I did (because the reference-style documentation doesn't explain this behaviour).  My above proposal was to escape encoding errors only when raiseExceptions == False.  So when raiseExceptions == True, we might still lose log messages, and the UnicodeEncodeError might be printed to /dev/null i.e. also silently discarded.  This suggests my proposal was trying to be too clever.

I guess the simplest approach is for emit() to handle encode errors by using 'backslashreplace' to log the message AND calling handleError().  

Instead of the original code sketch, change StreamHandler.emit() to

        try:
            # issue 35046: merged two stream.writes into one.
            msg = self.format(record) + self.terminator
            stream = self.stream
            try:
                stream.write(msg)
                self.flush()
            except UnicodeEncodeError:
                # Try to log something, even pure mojibake might provide a clue
                encoding = getattr(stream, 'encoding', None)
                if encoding:
                    bytes = msg.encode(encoding, errors='backslashreplace')
                    msg = bytes.decode(encoding)
                    stream.write(msg)
                    self.flush()
                # Call handleError() as normal
                raise
        except RecursionError:  # See issue 36272
            raise
        except Exception:
            self.handleError(record)

(And I'd like a similar change for SyslogHandler at least).
History
Date User Action Args
2019-04-26 15:17:30sourcejedisetrecipients: + sourcejedi, vinay.sajip
2019-04-26 15:17:30sourcejedisetmessageid: <1556291850.2.0.520401244405.issue36704@roundup.psfhosted.org>
2019-04-26 15:17:30sourcejedilinkissue36704 messages
2019-04-26 15:17:29sourcejedicreate