def test_exception_logging_overridden(): log.enable_exception_logging() sys.excepthook = lambda etype, evalue, tb: None with pytest.raises(LoggingError) as e: log.disable_exception_logging() assert e.value.args[ 0] == 'Cannot disable exception logging: sys.excepthook was not set by this logger, or has been overridden'
def log(self): from astropy import log if log.exception_logging_enabled(): log.disable_exception_logging() return log
def _set_defaults(self, log_level=logging.INFO, redirect_stdout=False): """Reset logger to its initial state.""" # Disable astropy logging if present because it uses the same override # of showwarning than us and messes up things try: from astropy import log log.disable_warnings_logging() log.disable_exception_logging() except Exception: pass # Remove all previous handlers for handler in self.handlers[:]: self.removeHandler(handler) # Set levels self.setLevel(logging.DEBUG) # Set up the stdout handler self.fh = None self.sh = logging.StreamHandler() self.sh.emit = colored_formatter self.addHandler(self.sh) self.sh.setLevel(log_level) # Redirects all stdout to the logger if redirect_stdout: sys.stdout = LoggerStdout(self._print) # Catches exceptions sys.excepthook = self._catch_exceptions warnings.showwarning = self._showwarning
def log(self): if self._log is None: try: from astropy import log if log.exception_logging_enabled(): log.disable_exception_logging() except ImportError: log = NotAModule(self._name) self._log = log return self._log
def setup_function(function): # Reset modules to default importlib.reload(warnings) importlib.reload(sys) # Reset internal original hooks log._showwarning_orig = None log._excepthook_orig = None # Set up the logger log._set_defaults() # Reset hooks if log.warnings_logging_enabled(): log.disable_warnings_logging() if log.exception_logging_enabled(): log.disable_exception_logging()
def test_exception_logging(): # Without exception logging try: with log.log_to_list() as log_list: raise Exception("This is an Exception") except Exception as exc: sys.excepthook(*sys.exc_info()) assert exc.args[0] == "This is an Exception" else: assert False # exception should have been raised assert len(log_list) == 0 # With exception logging try: log.enable_exception_logging() with log.log_to_list() as log_list: raise Exception("This is an Exception") except Exception as exc: sys.excepthook(*sys.exc_info()) assert exc.args[0] == "This is an Exception" else: assert False # exception should have been raised assert len(log_list) == 1 assert log_list[0].levelname == 'ERROR' assert log_list[0].message.startswith('Exception: This is an Exception') assert log_list[0].origin == 'astropy.tests.test_logger' # Without exception logging log.disable_exception_logging() try: with log.log_to_list() as log_list: raise Exception("This is an Exception") except Exception as exc: sys.excepthook(*sys.exc_info()) assert exc.args[0] == "This is an Exception" else: assert False # exception should have been raised assert len(log_list) == 0
def test_exception_logging_overridden(): log.enable_exception_logging() sys.excepthook = lambda etype, evalue, tb: None with pytest.raises(LoggingError, match='Cannot disable exception logging: ' 'sys.excepthook was not set by this logger, or has been overridden'): log.disable_exception_logging()
def test_exception_logging_disable_no_enable(): with pytest.raises(LoggingError) as e: log.disable_exception_logging() assert e.value.args[0] == 'Exception logging has not been enabled'
def test_exception_logging_overridden(): log.enable_exception_logging() sys.excepthook = lambda etype, evalue, tb: None with pytest.raises(LoggingError) as e: log.disable_exception_logging() assert e.value.args[0] == 'Cannot disable exception logging: sys.excepthook was not set by this logger, or has been overridden'