def test_fancylogger_as_rootlogger_logging(self): """ Test if just using import logging, logging with logging uses fancylogger after setting the root logger """ # test logging.root is loggin root logger # this is an assumption made to make the fancyrootlogger code work orig_root = logging.getLogger() self.assertEqual(logging.root, orig_root, msg='logging.root is the root logger') self.assertFalse(isinstance(logging.root, fancylogger.FancyLogger), msg='logging.root is not a FancyLogger') stringfile = StringIO() sys.stderr = stringfile handler = fancylogger.logToScreen() fancylogger.setLogLevelDebug() logger = fancylogger.getLogger() self.assertEqual(logger.handlers, [self.handler, handler], msg='active handler for root fancylogger') self.assertEqual(logger.level, fancylogger.getLevelInt('DEBUG'), msg='debug level set') msg = 'this is my string' logging.debug(msg) self.assertEqual(stringfile.getvalue(), '', msg="logging.debug reports nothing when fancylogger loglevel is debug") fancylogger.setroot() self.assertTrue(isinstance(logging.root, fancylogger.FancyLogger), msg='logging.root is a FancyLogger after setRootLogger') self.assertEqual(logging.root.level, fancylogger.getLevelInt('DEBUG'), msg='debug level set for root') self.assertEqual(logger.level, logging.NOTSET, msg='original root fancylogger level set to NOTSET') self.assertEqual(logging.root.handlers, [self.handler, handler], msg='active handler for root logger from previous root fancylogger') self.assertEqual(logger.handlers, [], msg='no active handlers on previous root fancylogger') root_logger = logging.getLogger('') self.assertEqual(root_logger, logging.root, msg='logging.getLogger() returns logging.root FancyLogger') frl = fancylogger.getLogger() self.assertEqual(frl, logging.root, msg='fancylogger.getLogger() returns logging.root FancyLogger') logging.debug(msg) self.assertTrue(msg in stringfile.getvalue(), msg="logging.debug reports when fancylogger loglevel is debug") fancylogger.resetroot() self.assertEqual(logging.root, orig_root, msg='logging.root is the original root logger after resetroot') # restore fancylogger.logToScreen(enable=False, handler=handler)
def _reset_fancylogger(self): fancylogger.resetroot() # delete all fancyloggers loggers = logging.Logger.manager.loggerDict for name, lgr in list(loggers.items()): if isinstance(lgr, fancylogger.FancyLogger): del loggers[name] # reset root handlers; mimic clean import logging logging.root.handlers = []
def _reset_fancylogger(self): fancylogger.resetroot() # delete all fancyloggers loggers = logging.Logger.manager.loggerDict for name, lgr in loggers.items(): if isinstance(lgr, fancylogger.FancyLogger): del loggers[name] # reset root handlers; mimic clean import logging logging.root.handlers = []
def tearDown(self): """Clean up after running test.""" super(GeneralOptionTest, self).tearDown() fancylogger.resetroot() os.environ = self.orig_environ
def setUp(self): """Prepare for running test.""" super(GeneralOptionTest, self).setUp() fancylogger.resetroot() self.setup = vsc_setup() self.orig_environ = copy.deepcopy(os.environ)