Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    def test_getlevelint(self):
        """Test the getLevelInt"""
        debug = fancylogger.getLevelInt('DEBUG')
        info = fancylogger.getLevelInt('INFO')
        warning = fancylogger.getLevelInt('WARNING')
        quiet = fancylogger.getLevelInt('QUIET')
        error = fancylogger.getLevelInt('ERROR')
        exception = fancylogger.getLevelInt('EXCEPTION')
        critical = fancylogger.getLevelInt('CRITICAL')
        fatal = fancylogger.getLevelInt('FATAL')
        apocalyptic = fancylogger.getLevelInt('APOCALYPTIC')

        for level in [
                debug, info, warning, quiet, error, exception, fatal, critical,
                fatal, apocalyptic
        ]:
            self.assertTrue(isinstance(level, int))

        self.assertEqual(logging.getLevelName(debug), 'DEBUG')
        self.assertEqual(logging.getLevelName(info), 'INFO')
        self.assertEqual(logging.getLevelName(warning), 'WARNING')
        self.assertEqual(logging.getLevelName(error), 'ERROR')
        self.assertEqual(logging.getLevelName(critical), 'CRITICAL')
        self.assertEqual(logging.getLevelName(apocalyptic), 'APOCALYPTIC')

        self.assertTrue(info > debug)
        self.assertTrue(warning > info)
        self.assertEqual(warning, quiet)
        self.assertTrue(error > warning)
        self.assertEqual(error, exception)
        self.assertTrue(critical > error)
        self.assertEqual(critical, fatal)
        self.assertTrue(apocalyptic > critical)
Пример #4
0
 def _loglevel(lvl, msg):
     lvl_int = topt.log.getEffectiveLevel()
     lvl_name = [
         k for k, v in logging._levelNames.items() if v == lvl_int
     ][0]
     self.assertEqual(lvl_int,
                      fancylogger.getLevelInt(lvl),
                      msg="%s (expected %s got %s)" %
                      (msg, lvl, lvl_name))
Пример #5
0
    def test_loglevel(self):
        """Test the loglevel default setting"""
        topt = TestOption1(go_args=['--ext-optional=REALVALUE'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), fancylogger.getLevelInt(topt.DEFAULT_LOGLEVEL.upper()))

        topt = TestOption1(go_args=['--debug'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), logging.DEBUG)

        topt = TestOption1(go_args=['--info'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), logging.INFO)

        topt = TestOption1(go_args=['--quiet'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), logging.WARNING)

        # last one wins
        topt = TestOption1(go_args=['--debug', '--info', '--quiet'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), logging.WARNING)

        CONFIGFILE1 = """
[base]
debug=1
"""
        tmp1 = NamedTemporaryFile()
        tmp1.write(CONFIGFILE1)
        tmp1.flush()  # flush, otherwise empty
        envvar = 'logactionoptiontest'.upper()
        topt = TestOption1(go_configfiles=[tmp1.name],
                           go_args=[],
                           go_nosystemexit=True,
                           envvar_prefix=envvar
                           )
        self.assertEqual(topt.log.getEffectiveLevel(), logging.DEBUG)

        # set via environment; environment wins over cfg file
        os.environ['%s_INFO' % envvar] = '1';
        topt = TestOption1(go_configfiles=[tmp1.name],
                           go_args=[],
                           go_nosystemexit=True,
                           envvar_prefix=envvar
                           )
        self.assertEqual(topt.log.getEffectiveLevel(), logging.INFO)

        # commandline always wins
        topt = TestOption1(go_configfiles=[tmp1.name],
                           go_args=['--quiet'],
                           go_nosystemexit=True,
                           envvar_prefix=envvar
                           )
        self.assertEqual(topt.log.getEffectiveLevel(), logging.WARNING)

        # remove tmp1
        del os.environ['%s_INFO' % envvar]
        tmp1.close()
Пример #6
0
    def test_loglevel(self):
        """Test the loglevel default setting"""
        topt = TestOption1(go_args=['--ext-optional=REALVALUE'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), fancylogger.getLevelInt(topt.DEFAULT_LOGLEVEL.upper()))

        topt = TestOption1(go_args=['--debug'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), logging.DEBUG)

        topt = TestOption1(go_args=['--info'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), logging.INFO)

        topt = TestOption1(go_args=['--quiet'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), logging.WARNING)

        # last one wins
        topt = TestOption1(go_args=['--debug', '--info', '--quiet'], go_nosystemexit=True,)
        self.assertEqual(topt.log.getEffectiveLevel(), logging.WARNING)

        CONFIGFILE1 = """
[base]
debug=1
"""
        tmp1 = NamedTemporaryFile()
        tmp1.write(CONFIGFILE1)
        tmp1.flush()  # flush, otherwise empty
        envvar = 'logactionoptiontest'.upper()
        topt = TestOption1(go_configfiles=[tmp1.name],
                           go_args=[],
                           go_nosystemexit=True,
                           envvar_prefix=envvar
                           )
        self.assertEqual(topt.log.getEffectiveLevel(), logging.DEBUG)

        # set via environment; environment wins over cfg file
        os.environ['%s_INFO' % envvar] = '1';
        topt = TestOption1(go_configfiles=[tmp1.name],
                           go_args=[],
                           go_nosystemexit=True,
                           envvar_prefix=envvar
                           )
        self.assertEqual(topt.log.getEffectiveLevel(), logging.INFO)

        # commandline always wins
        topt = TestOption1(go_configfiles=[tmp1.name],
                           go_args=['--quiet'],
                           go_nosystemexit=True,
                           envvar_prefix=envvar
                           )
        self.assertEqual(topt.log.getEffectiveLevel(), logging.WARNING)

        # remove tmp1
        del os.environ['%s_INFO' % envvar]
        tmp1.close()
Пример #7
0
def error_exit(logger, message):
    """
    Log silent error on INFO logger level or higher
    Log error traceback on DEBUG logger level or below
    - logger: (object) fancylogger object of the caller
    - message: (string) error message to be printed
    """
    debug_level = logger.getEffectiveLevel() <= fancylogger.getLevelInt(
        'DEBUG')

    # Log the error
    logger.error(message, exc_info=debug_level)

    # Exit
    sys.exit(1)
Пример #8
0
 def test_getlevelint(self):
     """Test the getLevelInt"""
     DEBUG = fancylogger.getLevelInt('DEBUG')
     INFO = fancylogger.getLevelInt('INFO')
     WARNING = fancylogger.getLevelInt('WARNING')
     ERROR = fancylogger.getLevelInt('ERROR')
     CRITICAL = fancylogger.getLevelInt('CRITICAL')
     APOCALYPTIC = fancylogger.getLevelInt('APOCALYPTIC')
     self.assertTrue(INFO > DEBUG)
     self.assertTrue(WARNING > INFO)
     self.assertTrue(ERROR > WARNING)
     self.assertTrue(CRITICAL > ERROR)
     self.assertTrue(APOCALYPTIC > CRITICAL)
Пример #9
0
 def test_getlevelint(self):
     """Test the getLevelInt"""
     DEBUG = fancylogger.getLevelInt("DEBUG")
     INFO = fancylogger.getLevelInt("INFO")
     WARNING = fancylogger.getLevelInt("WARNING")
     ERROR = fancylogger.getLevelInt("ERROR")
     CRITICAL = fancylogger.getLevelInt("CRITICAL")
     APOCALYPTIC = fancylogger.getLevelInt("APOCALYPTIC")
     self.assertTrue(INFO > DEBUG)
     self.assertTrue(WARNING > INFO)
     self.assertTrue(ERROR > WARNING)
     self.assertTrue(CRITICAL > ERROR)
     self.assertTrue(APOCALYPTIC > CRITICAL)
Пример #10
0
 def test_getlevelint(self):
     """Test the getLevelInt"""
     DEBUG = fancylogger.getLevelInt('DEBUG')
     INFO = fancylogger.getLevelInt('INFO')
     WARNING = fancylogger.getLevelInt('WARNING')
     ERROR = fancylogger.getLevelInt('ERROR')
     CRITICAL = fancylogger.getLevelInt('CRITICAL')
     APOCALYPTIC = fancylogger.getLevelInt('APOCALYPTIC')
     self.assertTrue(INFO > DEBUG)
     self.assertTrue(WARNING > INFO)
     self.assertTrue(ERROR > WARNING)
     self.assertTrue(CRITICAL > ERROR)
     self.assertTrue(APOCALYPTIC > CRITICAL)
Пример #11
0
 def _loglevel(lvl, msg):
     lvl_int = topt.log.getEffectiveLevel()
     lvl_name = [k for k,v in logging._levelNames.items() if v == lvl_int][0]
     self.assertEqual(lvl_int,
                      fancylogger.getLevelInt(lvl),
                      msg="%s (expected %s got %s)" % (msg, lvl, lvl_name))
Пример #12
0
 def test_loglevel(self):
     """Test the loglevel default setting"""
     topt = TestOption1(go_args=['--ext-optional=REALVALUE'], go_nosystemexit=True,)
     self.assertEqual(topt.log.getEffectiveLevel(), fancylogger.getLevelInt(topt.DEFAULT_LOGLEVEL.upper()))