def setUp(self): # We add handlers that store root- and base-logger output self.rootBuffer = StringIO() rootLogger = logging.getLogger() self.prevRootLogLevel = rootLogger.getEffectiveLevel() self.rootLogHandler = logging.StreamHandler(self.rootBuffer) rootLogger.addHandler(self.rootLogHandler) self.baseBuffer = StringIO() baseLogger = logging.getLogger(BASE_LOGGER_NAME) self.prevBaseLogLevel = baseLogger.getEffectiveLevel() self.baseLogHandler = logging.StreamHandler(self.baseBuffer) baseLogger.addHandler(self.baseLogHandler)
class LoggerTest(unittest.TestCase): """Test configurable logging.""" def setUp(self): # We add handlers that store root- and base-logger output self.rootBuffer = StringIO() rootLogger = logging.getLogger() self.prevRootLogLevel = rootLogger.getEffectiveLevel() self.rootLogHandler = logging.StreamHandler(self.rootBuffer) rootLogger.addHandler(self.rootLogHandler) self.baseBuffer = StringIO() baseLogger = logging.getLogger(BASE_LOGGER_NAME) self.prevBaseLogLevel = baseLogger.getEffectiveLevel() self.baseLogHandler = logging.StreamHandler(self.baseBuffer) baseLogger.addHandler(self.baseLogHandler) def tearDown(self): rootLogger = logging.getLogger() self.rootLogHandler.close() rootLogger.setLevel(self.prevRootLogLevel) rootLogger.removeHandler(self.rootLogHandler) baseLogger = logging.getLogger(BASE_LOGGER_NAME) self.baseLogHandler.close() baseLogger.setLevel(self.prevBaseLogLevel) baseLogger.removeHandler(self.baseLogHandler) def getLogOutput(self): self.rootLogHandler.flush() self.baseLogHandler.flush() return (self.rootBuffer.getvalue(), self.baseBuffer.getvalue()) def testDefault(self): """By default, there should be no logging.""" _baseLogger = logging.getLogger(BASE_LOGGER_NAME) _baseLogger.debug("_baseLogger.debug") _baseLogger.info("_baseLogger.info") _baseLogger.warning("_baseLogger.warning") _baseLogger.error("_baseLogger.error") rootOutput, baseOutput = self.getLogOutput() # Printed for debugging, when test fails: print("ROOT OUTPUT:\n'{}'\nBASE OUTPUT:\n'{}'".format( rootOutput, baseOutput)) # No output should be generated in the root logger assert rootOutput == "" # The library logger should default to INFO level # (this output will not be visble, because the base logger only has a NullHandler) assert ".debug" not in baseOutput assert ".info" in baseOutput assert ".warning" in baseOutput assert ".error" in baseOutput def testEnablePropagation(self): """Users can enable logging by propagating to root logger.""" _baseLogger = logging.getLogger(BASE_LOGGER_NAME) _baseLogger.propagate = True _baseLogger.debug("_baseLogger.debug") _baseLogger.info("_baseLogger.info") _baseLogger.warning("_baseLogger.warning") _baseLogger.error("_baseLogger.error") rootOutput, baseOutput = self.getLogOutput() # Printed for debugging, when test fails: print("ROOT OUTPUT:\n'{}'\nBASE OUTPUT:\n'{}'".format( rootOutput, baseOutput)) # Now output we should see output in the root logger assert rootOutput == baseOutput # The library logger should default to INFO level # (this output will not be visble, because the base logger only has a NullHandler) assert ".debug" not in baseOutput assert ".info" in baseOutput assert ".warning" in baseOutput assert ".error" in baseOutput def testCliLogging(self): """CLI initializes logging.""" config = {"verbose": 3, "enable_loggers": ["test"]} init_logging(config) _baseLogger = logging.getLogger(BASE_LOGGER_NAME) _enabledLogger = get_module_logger("test") _disabledLogger = get_module_logger("test2") _baseLogger.debug("_baseLogger.debug") _baseLogger.info("_baseLogger.info") _baseLogger.warning("_baseLogger.warning") _baseLogger.error("_baseLogger.error") _enabledLogger.debug("_enabledLogger.debug") _enabledLogger.info("_enabledLogger.info") _enabledLogger.warning("_enabledLogger.warning") _enabledLogger.error("_enabledLogger.error") _disabledLogger.debug("_disabledLogger.debug") _disabledLogger.info("_disabledLogger.info") _disabledLogger.warning("_disabledLogger.warning") _disabledLogger.error("_disabledLogger.error") rootOutput, baseOutput = self.getLogOutput() # Printed for debugging, when test fails: print("ROOT OUTPUT:\n'{}'\nBASE OUTPUT:\n'{}'".format( rootOutput, baseOutput)) # init_logging() removes all other handlers assert rootOutput == "" assert baseOutput == ""
class LoggerTest(unittest.TestCase): """Test configurable logging.""" def setUp(self): # We add handlers that store root- and base-logger output self.rootBuffer = StringIO() rootLogger = logging.getLogger() self.prevRootLogLevel = rootLogger.getEffectiveLevel() self.rootLogHandler = logging.StreamHandler(self.rootBuffer) rootLogger.addHandler(self.rootLogHandler) self.baseBuffer = StringIO() baseLogger = logging.getLogger(BASE_LOGGER_NAME) self.prevBaseLogLevel = baseLogger.getEffectiveLevel() self.baseLogHandler = logging.StreamHandler(self.baseBuffer) baseLogger.addHandler(self.baseLogHandler) def tearDown(self): rootLogger = logging.getLogger() self.rootLogHandler.close() rootLogger.setLevel(self.prevRootLogLevel) rootLogger.removeHandler(self.rootLogHandler) baseLogger = logging.getLogger(BASE_LOGGER_NAME) self.baseLogHandler.close() baseLogger.setLevel(self.prevBaseLogLevel) baseLogger.removeHandler(self.baseLogHandler) def getLogOutput(self): self.rootLogHandler.flush() self.baseLogHandler.flush() return (self.rootBuffer.getvalue(), self.baseBuffer.getvalue()) def testDefault(self): """By default, there should be no logging.""" _baseLogger = logging.getLogger(BASE_LOGGER_NAME) _baseLogger.debug("_baseLogger.debug") _baseLogger.info("_baseLogger.info") _baseLogger.warning("_baseLogger.warning") _baseLogger.error("_baseLogger.error") rootOutput, baseOutput = self.getLogOutput() # Printed for debugging, when test fails: print("ROOT OUTPUT:\n'{}'\nBASE OUTPUT:\n'{}'".format(rootOutput, baseOutput)) # No output should be generated in the root logger assert rootOutput == "" # The library logger should default to INFO level # (this output will not be visble, because the base logger only has a NullHandler) assert ".debug" not in baseOutput assert ".info" in baseOutput assert ".warning" in baseOutput assert ".error" in baseOutput def testEnablePropagation(self): """Users can enable logging by propagating to root logger.""" _baseLogger = logging.getLogger(BASE_LOGGER_NAME) _baseLogger.propagate = True _baseLogger.debug("_baseLogger.debug") _baseLogger.info("_baseLogger.info") _baseLogger.warning("_baseLogger.warning") _baseLogger.error("_baseLogger.error") rootOutput, baseOutput = self.getLogOutput() # Printed for debugging, when test fails: print("ROOT OUTPUT:\n'{}'\nBASE OUTPUT:\n'{}'".format(rootOutput, baseOutput)) # Now output we should see output in the root logger assert rootOutput == baseOutput # The library logger should default to INFO level # (this output will not be visble, because the base logger only has a NullHandler) assert ".debug" not in baseOutput assert ".info" in baseOutput assert ".warning" in baseOutput assert ".error" in baseOutput def testCliLogging(self): """CLI initializes logging.""" config = {"verbose": 3, "enable_loggers": ["test"]} init_logging(config) _baseLogger = logging.getLogger(BASE_LOGGER_NAME) _enabledLogger = get_module_logger("test") _disabledLogger = get_module_logger("test2") _baseLogger.debug("_baseLogger.debug") _baseLogger.info("_baseLogger.info") _baseLogger.warning("_baseLogger.warning") _baseLogger.error("_baseLogger.error") _enabledLogger.debug("_enabledLogger.debug") _enabledLogger.info("_enabledLogger.info") _enabledLogger.warning("_enabledLogger.warning") _enabledLogger.error("_enabledLogger.error") _disabledLogger.debug("_disabledLogger.debug") _disabledLogger.info("_disabledLogger.info") _disabledLogger.warning("_disabledLogger.warning") _disabledLogger.error("_disabledLogger.error") rootOutput, baseOutput = self.getLogOutput() # Printed for debugging, when test fails: print("ROOT OUTPUT:\n'{}'\nBASE OUTPUT:\n'{}'".format(rootOutput, baseOutput)) # init_logging() removes all other handlers assert rootOutput == "" assert baseOutput == ""