-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Description
Describe the issue:
Importing numpy.distutils silently replaces the global logger returned by logging.getLogger() with the custom class from numpy.distutils.log. This is surprising in of itself, but additionally the signature of numpy.distutils.log.Log._log() is outdated/incorrect and does not support the kwargs exc_info, stack_info, stacklevel or extra. In particular, this means the exception() method of the custom logger does not work.
Reproduce the code example:
>>> import logging
>>> type(logging.getLogger())
<class 'logging.RootLogger'>
>>> import numpy.distutils
>>> type(logging.getLogger())
<class 'numpy.distutils.log.Log'>
>>> logging.getLogger().exception("Oh No")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/oliver/anaconda3/envs/python3.10.12/lib/python3.10/logging/__init__.py", line 1512, in exception
self.error(msg, *args, exc_info=exc_info, **kwargs)
File "/home/oliver/anaconda3/envs/python3.10.12/lib/python3.10/logging/__init__.py", line 1506, in error
self._log(ERROR, msg, args, **kwargs)
TypeError: Log._log() got an unexpected keyword argument 'exc_info'Error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/oliver/anaconda3/envs/python3.10.12/lib/python3.10/logging/__init__.py", line 1512, in exception
self.error(msg, *args, exc_info=exc_info, **kwargs)
File "/home/oliver/anaconda3/envs/python3.10.12/lib/python3.10/logging/__init__.py", line 1506, in error
self._log(ERROR, msg, args, **kwargs)
TypeError: Log._log() got an unexpected keyword argument 'exc_info'Runtime information:
>>> import sys, numpy; print(numpy.__version__); print(sys.version)
1.22.3
3.10.8 (main, Nov 24 2022, 14:13:03) [GCC 11.2.0]
Context for the issue:
Any package that imports anything from numpy.distutils will accidentally break the root logger for the whole program/script. In addition, because it's not obvious that the root logger is replaced in this way, this problem can be (and sadly was) challenging to diagnose.
For a real world example, importing pyscf also recreates this behaviour:
>>> import logging
>>> type(logging.getLogger())
<class 'logging.RootLogger'>
>>> import pyscf
>>> type(logging.getLogger())
<class 'numpy.distutils.log.Log'>
>>> try:
... raise Exception("Bugger")
... except Exception:
... logging.getLogger().exception("Oh No")
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
Exception: Bugger
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
File "/home/oliver/anaconda3/envs/python3.10.12/lib/python3.10/logging/__init__.py", line 1512, in exception
self.error(msg, *args, exc_info=exc_info, **kwargs)
File "/home/oliver/anaconda3/envs/python3.10.12/lib/python3.10/logging/__init__.py", line 1506, in error
self._log(ERROR, msg, args, **kwargs)
TypeError: Log._log() got an unexpected keyword argument 'exc_info'