question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Rich loghandler raising exceptions when using pythonw

See original GitHub issue

After switching to using the Rich log handler in an application it started raising 'NoneType' object has no attribute 'write' exceptions, however, this would only happen when the debugger was not connected and running under pythonw (the special windows version that does not create a console window, used for GUI applications).

On a hunch I checked the implementation of Pythons builtin log handler (that we had been using previously) and found the following line in the Handler.handleError:

if raiseExceptions and sys.stderr: # see issue 13807

This confirmed my hunch, when using pythonw both sys.stdout and sys.stderr are None. The Rich logger doesn’t appear to have an exception handler catching all exceptions being generated when rending log output and deferring to the handleError method.

To Reproduce

Running this with Python will produce the expected log message, running with pythonw will generate the error.txt file with the missing attribute exception.

import logging
from rich.logging import RichHandler

logging.basicConfig(handlers=[RichHandler()], level=logging.INFO)

try:
    logging.info("My amazing info message")
except Exception as ex:
    with open("error.txt", "w") as f_out:
        print(str(ex), file=f_out)

Platform This is a Windows-specific error

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
willmcgugancommented, Nov 7, 2021

Fix is in 10.13.0

1reaction
timsavagecommented, Nov 6, 2021

The emit method of logging.StreamHandler (ref https://github.com/python/cpython/blob/main/Lib/logging/__init__.py#L1103 ) has a try/except block that catches Exception and passes the record to Handler.handleError. The docstring for handleError states

This method should be called from handlers when an exception is encountered during an emit() call.

This was the pattern I followed in the PR associated with this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Logging Guide - Best Practices and Hands-on Examples
This method is useful when using custom logging levels. Logger.exception(msg, *args, **kwargs) issues log requests with the logging level ERROR ...
Read more >
What happens if a python logging handler raises an exception?
If an exception is raised while a handler is asked to emit a record, all standard library implementations catch the exception and instead...
Read more >
Logging Handler — Rich 12.6.0 documentation
Rich supplies a logging handler which will format and colorize text written by Python's logging module. Here's an example of how to set...
Read more >
Exceptional Logging of Exceptions in Python - Loggly
In the event a specific exception type is raised, you log a message, deal with the situation, and move on. The key difference...
Read more >
Use Rich Loghandler in combination with other handlers #1582
So far, i've been using a regular StreamHandler - which looks pretty OK with the logformat selected - and the same format works...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found