[issue36704] logging.FileHandler currently hardcodes errors='strict'

Alan Jenkins report at bugs.python.org
Fri Apr 26 11:17:30 EDT 2019


Alan Jenkins <alan.christopher.jenkins at gmail.com> added the comment:

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).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36704>
_______________________________________


More information about the Python-bugs-list mailing list