def test_logging_configuration(self):

        # Test that logging is properly initialized when there is no logging configuration file

        rebase_environment('app_without_logging_configuration')

        self.assertIsInstance(environment.splunklib_logger, logging.Logger)
        self.assertIsNone(environment.logging_configuration)
        self.assertEqual(len(logging.root.handlers), 1)
        self.assertEqual(len(logging.Logger.manager.loggerDict), 1)
        self.assertIsInstance(logging.root.handlers[0], logging.StreamHandler)
        self.assertIs(environment.splunklib_logger, logging.getLogger('splunklib'))
        self.assertIsNone(environment.logging_configuration)

        command = StubbedSearchCommand()

        self.assertIs(command.logger, logging.getLogger('StubbedSearchCommand'))
        self.assertEqual(len(command.logger.handlers), 0)
        self.assertIsNone(command.logging_configuration)
        self.assertIs(command.logger.root, logging.root)
        self.assertEqual(len(logging.root.handlers), 1)

        root_handler = logging.root.handlers[0]

        self.assertIsInstance(root_handler, logging.StreamHandler)
        self.assertEqual(root_handler.stream, sys.stderr)

        self.assertEqual(command.logging_level, logging.getLevelName(logging.WARNING))
        root_handler.stream = StringIO()
        message = 'Test that output is directed to stderr without formatting'
        command.logger.warning(message)
        self.assertEqual(root_handler.stream.getvalue(), message + '\n')

        # A search command loads {local,default}/logging.conf when it is available

        rebase_environment('app_with_logging_configuration')

        command = StubbedSearchCommand()
        self.assertEqual(command.logging_configuration, os.path.join(environment.app_root, 'default', 'logging.conf'))
        self.assertIs(command.logger, logging.getLogger('StubbedSearchCommand'))

        # Setting logging_configuration loads a new logging configuration file relative to the app root

        command.logging_configuration = 'alternative-logging.conf'
        self.assertEqual(
            command.logging_configuration, os.path.join(environment.app_root, 'default', 'alternative-logging.conf'))
        self.assertIs(command.logger, logging.getLogger('StubbedSearchCommand'))

        # Setting logging_configuration loads a new logging configuration file on an absolute path

        app_root_logging_configuration = os.path.join(environment.app_root, 'logging.conf')
        command.logging_configuration = app_root_logging_configuration

        self.assertEqual(command.logging_configuration, app_root_logging_configuration)
        self.assertIs(command.logger, logging.getLogger('StubbedSearchCommand'))

        # logging_configuration raises a value error, if a non-existent logging configuration file is provided

        try:
            command.logging_configuration = 'foo'
        except __HOLE__:
            pass
        except BaseException as e:
            self.fail('Expected ValueError, but {} was raised'.format(type(e)))
        else:
            self.fail('Expected ValueError, but logging_configuration={}'.format(command.logging_configuration))

        try:
            command.logging_configuration = os.path.join(package_directory, 'non-existent.logging.conf')
        except ValueError:
            pass
        except BaseException as e:
            self.fail('Expected ValueError, but {} was raised'.format(type(e)))
        else:
            self.fail('Expected ValueError, but logging_configuration={}'.format(command.logging_configuration))
dataset/ETHPy150Open splunk/splunk-sdk-python/tests/searchcommands/test_builtin_options.py/TestBuiltinOptions.test_logging_configuration