rainbow_logging_handler icon indicating copy to clipboard operation
rainbow_logging_handler copied to clipboard

Logging when python interpreter exits fails when it cannot import sys

Open ankostis opened this issue 8 years ago • 1 comments

The first statement of RainbowLoggingHandler._emit() is: import sys statement. Importing libraries when python shutsdown raises the following exception:

--- Logging error ---
Traceback (most recent call last):
  File "/python/lib/site_packages/rainbow_logging_handler/__init__.py", line 194, in emit
    msg = self._encode(msg)
  File "/python/lib/site_packages/rainbow_logging_handler/__init__.py", line 210, in _encode
    import sys
ImportError: sys.meta_path is None, Python is likely shutting down
Call stack:
  File "/python/app/foo.py", line 65, in __del__
    self.close()
  File "/python/lib/logging/__init__.py", line 1294, in debug
    self._log(DEBUG, msg, args, **kwargs)
  File "/python/lib/logging/__init__.py", line 1442, in _log
    self.handle(record)
  File "/python/lib/logging/__init__.py", line 1452, in handle
    self.callHandlers(record)
  File "/python/lib/logging/__init__.py", line 1514, in callHandlers
    hdlr.handle(record)
  File "/python/lib/logging/__init__.py", line 863, in handle
    self.emit(record)
  File "/python/lib/site_packages/rainbow_logging_handler/__init__.py", line 200, in emit
    self.handleError(record)
Message: 'Closing somethid: %s'
Arguments: ('something')

Many libraries use logging statements just before python interpreter exits, e.g inside finalizers, and this causes an annoying cascade of log-failures at the exit of the application.

The solution is to move the import sys statement at the top of the rainbow_logging_handler/__init__.py file.

ankostis avatar Jun 27 '17 09:06 ankostis

Any chance for a new release with this bug fixed?

ankostis avatar Aug 06 '18 11:08 ankostis