def Log(self, rows, logger=None, level=logging.INFO): """Logs the given rows to the given logger. Args: rows: list of tuples, The rows to log the formatted table for. logger: logging.Logger, The logger to do the logging. If None, the root logger will be used. level: logging level, An optional override for the logging level, INFO by default. """ if not logger: logger = log.getLogger() lines = self.GetLines(rows) for line in lines: logger.log(level, line)
def Log(self, rows, logger=None, level=logging.INFO): """Logs the given rows to the given logger. Args: rows: list of tuples, The rows to log the formatted table for. logger: logging.Logger, The logger to do the logging. If None, the root logger will be used. level: logging level, An optional override for the logging level, INFO by default. """ if not logger: logger = log.getLogger() lines = self.GetLines(rows) for line in lines: logger.log(level, line)
def SetUp(self): self._prev_log_level = log.getLogger().getEffectiveLevel() self.__root_dir = file_utils.TemporaryDirectory() self.root_path = self.__root_dir.path self.temp_path = self.CreateTempDir() self.global_config_path = self.CreateTempDir('config') encoding.SetEncodedValue(os.environ, config.CLOUDSDK_CONFIG, self.global_config_path, encoding='utf-8') # MONKEYPATCH: We'd like to StartObjectPatch but Popen is patched elsewhere. subprocess.Popen = self.__Popen # Redirect home to a temp directory. self.home_path = self.CreateTempDir() self.StartEnvPatch({'HOME': self.home_path}) self.mock_get_home_path = self.StartPatch( 'googlecloudsdk.core.util.files.GetHomeDir', return_value=self.home_path) self.mock_expandvars = self.StartPatch('os.path.expandvars', autospec=True, return_value=self.home_path) self.addCleanup(self._CloseDirs) self.addCleanup(resources.REGISTRY.Clear) # Make sure there is nothing in the environment before the tests starts. self.__CleanProperties(setup=True) self.addCleanup(self.__CleanProperties) # Turn these off for tests. properties.VALUES.GetInvocationStack()[:] = [{}] # This is not a real property but behaves like one. os.environ.pop('CLOUDSDK_ACTIVE_CONFIG_NAME', None) # pylint:disable=protected-access named_configs.FLAG_OVERRIDE_STACK._stack[:] = [] # Make sure certain things are restored between tests self.install_props = self._GetInstallPropsStats() self.addCleanup(self._VerifyInstallProps) properties.VALUES.core.interactive_ux_style.Set( properties.VALUES.core.InteractiveUXStyles.TESTING.name) # The mocking of stdout and stderr happen in the test_case module whenever # a test is using output capture. We need to reset the logger here to pick # up those settings. log.Reset()
def TearDown(self): # We need to reset the logger here because it depends on properties and # the config directory, so that needs to happen before we unmock that stuff. # We have to explicitly pass in the original stdout and stderr streams # because when this runs, test_case.WithOutputCapture, will not yet have # unmocked these streams. If we don't then log.Reset here would restore # log.out to the mocked sys.stdout and leave it in that state for subsequent # tests. This could result in "write to closed stream" exceptions for # subsequent log.out writes. # pylint:disable=protected-access log.Reset(sys.__stdout__, sys.__stderr__) self.assertEqual(self._prev_log_level, log.getLogger().getEffectiveLevel(), 'The test or the code that is tested has modified the ' 'logger level/verbosity and did not restore it.') self.root_path = None self.temp_path = None self.global_config_path = None os.environ.pop(config.CLOUDSDK_CONFIG, None) subprocess.Popen = self._REAL_POPEN