def test_config_file_exception(self, config, path): """Test other errors (e.g. /dev/log permission).""" path.return_value.exists.return_value = True config.fileConfig.side_effect = OSError self._mock_logger() LogManager.load_config_file('existent_file') self._assert_string_in_logs('Using default Python logging config')
def test_config_file_exception(self, config, path): """Test other errors (e.g. /dev/log permission).""" path.return_value.exists.return_value = True config.fileConfig.side_effect = OSError self._mock_logger() LogManager.load_config_file('existent_file') self._assert_string_in_logs('Using default Python logging config')
def test_set_debug_mode_with_false(self, config, path, parser): """Test set_debug_mode with debug = False.""" path.return_value.exists.return_value = True config.fileConfig.side_effect = OSError self._mock_logger() LogManager.load_config_file('existent_file', debug=False) parser.set.assert_not_called()
def test_set_debug_mode_with_false(self, config, path, parser): """Test set_debug_mode with debug = False.""" path.return_value.exists.return_value = True config.fileConfig.side_effect = OSError self._mock_logger() LogManager.load_config_file('existent_file', debug=False) parser.set.assert_not_called()
def test_no_syslog(self, path, config, parser): """Must log when there's no syslog and try again without it.""" path.return_value.exists.return_value = True config.fileConfig.side_effect = [OSError, None] parser.__contains__.return_value = True # must have syslog section self._mock_logger() LogManager.load_config_file('existent_file') self._assert_string_in_logs('Trying to disable syslog') parser.remove_section.assert_called_once_with('handler_syslog') self._assert_string_in_logs('Logging config file "%s" loaded ' 'successfully.')
def test_no_syslog(self, path, config, parser): """Must log when there's no syslog and try again without it.""" path.return_value.exists.return_value = True config.fileConfig.side_effect = [OSError, None] parser.__contains__.return_value = True # must have syslog section self._mock_logger() LogManager.load_config_file('existent_file') self._assert_string_in_logs('Trying to disable syslog') parser.remove_section.assert_called_once_with('handler_syslog') self._assert_string_in_logs('Logging config file "%s" loaded ' 'successfully.')
def test_set_debug_mode(self, config, path, parser): """Test set_debug_mode with debug = True.""" path.return_value.exists.return_value = True config.fileConfig.side_effect = OSError self._mock_logger() LogManager.load_config_file('existent_file', debug=True) expected_message = 'Setting log configuration with debug mode.' self._assert_string_in_logs(expected_message) parser.set('logger_root', 'level', 'DEBUG') parser.set.assert_called_with('logger_root', 'level', 'DEBUG') parser.set('logger_api_server', 'level', 'DEBUG') parser.set.assert_called_with('logger_api_server', 'level', 'DEBUG')
def test_set_debug_mode(self, config, path, parser): """Test set_debug_mode with debug = True.""" path.return_value.exists.return_value = True config.fileConfig.side_effect = OSError self._mock_logger() LogManager.load_config_file('existent_file', debug=True) expected_message = 'Setting log configuration with debug mode.' self._assert_string_in_logs(expected_message) parser.set('logger_root', 'level', 'DEBUG') parser.set.assert_called_with('logger_root', 'level', 'DEBUG') parser.set('logger_api_server', 'level', 'DEBUG') parser.set.assert_called_with('logger_api_server', 'level', 'DEBUG')
def toggle_debug(self, name=None): """Enable/disable logging debug messages to a given logger name. If the name parameter is not specified the debug will be enabled/disabled following the initial config file. It will decide to enable/disable using the 'kytos' name to find the current logger level. Obs: To disable the debug the logging will be set to NOTSET Args: name(text): Full hierarchy Logger name. Ex: "kytos.core.controller" """ if name and name not in logging.root.manager.loggerDict: # A Logger name that is not declared in logging will raise an error # otherwise logging would create a new Logger. raise ValueError(f"Invalid logger name: {name}") if not name: # Logger name not specified. level = logging.getLogger('kytos').getEffectiveLevel() enable_debug = level != logging.DEBUG # Enable/disable default Loggers LogManager.load_config_file(self.options.logging, enable_debug) return # Get effective logger level for the name level = logging.getLogger(name).getEffectiveLevel() logger = logging.getLogger(name) if level == logging.DEBUG: # disable debug logger.setLevel(logging.NOTSET) else: # enable debug logger.setLevel(logging.DEBUG)
def test_non_existent_config_file(self, path): """If config file doesn't exist, warn instead of raising exception.""" self._mock_logger() path.return_value.exists.return_value = False LogManager.load_config_file('non_existent_file') self._assert_string_in_logs('Log config file "%s" does not exist.')
def enable_logs(self): """Register kytos log and enable the logs.""" LogManager.load_config_file(self.options.logging, self.options.debug) LogManager.enable_websocket(self.api_server.server) self.log = logging.getLogger(__name__)
def test_non_existent_config_file(self, path): """If config file doesn't exist, warn instead of raising exception.""" self._mock_logger() path.return_value.exists.return_value = False LogManager.load_config_file('non_existent_file') self._assert_string_in_logs('Log config file "%s" does not exist.')
def enable_logs(self): """Register kytos log and enable the logs.""" LogManager.load_config_file(self.options.logging, self.options.debug) LogManager.enable_websocket(self.api_server.server) self.log = logging.getLogger("controller")