Пример #1
0
    def test_fallback(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger, 10, 100)

        logger.info("hello world!")
        mock_logger.log.assert_called_with(
            logging.INFO,
            "hello world!",
        )
Пример #2
0
    def test_fallback(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger, 10, 100,
                                 structured_detail=False, with_prefix=False)

        logger.info("hello world!")
        mock_logger.log.assert_called_with(
            logging.INFO,
            "hello world!",
        )
Пример #3
0
    def test_verbosity(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 10,
                                 100,
                                 verbosity=swailing.PRIMARY,
                                 structured_detail=False,
                                 with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
        ]
        self.assertEqual(mock_logger.log.mock_calls, calls)

        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 10,
                                 100,
                                 verbosity=swailing.DETAIL,
                                 structured_detail=False,
                                 with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, "detail"),
        ]
        self.assertEqual(mock_logger.log.mock_calls, calls)

        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 10,
                                 100,
                                 verbosity=swailing.HINT,
                                 structured_detail=False,
                                 with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, "detail"),
            mock.call(logging.INFO, "hint"),
        ]
        self.assertEqual(mock_logger.log.mock_calls, calls)
Пример #4
0
    def test_verbosity(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 10,
                                 100,
                                 verbosity=swailing.PRIMARY,
                                 structured_detail=False,
                                 with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
        ]
        self.assertEqual(mock_logger.log.mock_calls, calls)

        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 10,
                                 100,
                                 verbosity=swailing.DETAIL,
                                 structured_detail=False,
                                 with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, "detail"),
        ]
        self.assertEqual(mock_logger.log.mock_calls, calls)

        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 10,
                                 100,
                                 verbosity=swailing.HINT,
                                 structured_detail=False,
                                 with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, "detail"),
            mock.call(logging.INFO, "hint"),
        ]
        self.assertEqual(mock_logger.log.mock_calls, calls)
Пример #5
0
    def test_fallback(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 10,
                                 100,
                                 structured_detail=False,
                                 with_prefix=False)

        logger.info("hello world!")
        mock_logger.log.assert_called_with(
            logging.INFO,
            "hello world!",
        )
Пример #6
0
    def test_context(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger, 10, 100)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, "detail"),
            mock.call(logging.INFO, "hint"),
        ]
        mock_logger.log.assert_has_calls(calls)
Пример #7
0
    def test_with_structured_detail(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger, 10, 100,
                                 structured_detail=True, with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail({"my":"dict"})
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, '{"my": "dict"}'),
            mock.call(logging.INFO, "hint"),
        ]
        mock_logger.log.assert_has_calls(calls)
Пример #8
0
    def test_with_prefix(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger, 10, 100,
                                 structured_detail=False, with_prefix=True)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, "DETAIL: detail"),
            mock.call(logging.INFO, "HINT: hint"),
        ]
        mock_logger.log.assert_has_calls(calls)
Пример #9
0
    def test_no_rate(self):
        """Simple test without rate limiting."""

        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, "detail"),
            mock.call(logging.INFO, "hint"),
        ]
        mock_logger.log.assert_has_calls(calls)
Пример #10
0
    def test_no_rate(self):
        """Simple test without rate limiting."""

        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 structured_detail=False,
                                 with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, "detail"),
            mock.call(logging.INFO, "hint"),
        ]
        mock_logger.log.assert_has_calls(calls)
Пример #11
0
    def test_context(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 10,
                                 100,
                                 structured_detail=False,
                                 with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail("detail")
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, "detail"),
            mock.call(logging.INFO, "hint"),
        ]
        mock_logger.log.assert_has_calls(calls)
Пример #12
0
    def test_with_structured_detail(self):
        mock_logger = mock.Mock()
        logger = swailing.Logger(mock_logger,
                                 10,
                                 100,
                                 structured_detail=True,
                                 with_prefix=False)
        with logger.info() as L:
            L.primary("primary")
            L.detail({"my": "dict"})
            L.hint("hint")

        calls = [
            mock.call(logging.INFO, "primary"),
            mock.call(logging.INFO, '{"my": "dict"}'),
            mock.call(logging.INFO, "hint"),
        ]
        mock_logger.log.assert_has_calls(calls)
Пример #13
0
    def test_throttle(self, clock):
        mock_logger = mock.Mock()
        clock.return_value = 1
        logger = swailing.Logger(mock_logger, 1, 2)

        # We'll write a buch of times, but since capacity is just 2,
        # we should expect the root_logger to only have been called
        # twice.
        for i in xrange(10):
            with logger.info() as L:
                L.primary("primary %d" % i)
                L.detail("detail %d" % i)
                L.hint("hint %d" % i)

        calls = [
            mock.call(logging.INFO, "primary 0"),
            mock.call(logging.INFO, "detail 0"),
            mock.call(logging.INFO, "hint 0"),

            mock.call(logging.INFO, "primary 1"),
            mock.call(logging.INFO, "detail 1"),
            mock.call(logging.INFO, "hint 1"),
        ]
        mock_logger.log.assert_has_calls(calls)


        # Now let's wait a second so we get one more token, then run test again.
        clock.return_value += 1
        mock_logger.reset_mock()

        for i in xrange(10):
            with logger.info() as L:
                L.primary("primary %d" % i)
                L.detail("detail %d" % i)
                L.hint("hint %d" % i)

        calls = [
            mock.call(logging.INFO, "primary 0"),
            mock.call(logging.INFO, "detail 0"),
            mock.call(logging.INFO, "hint 0"),
        ]
        mock_logger.log.assert_has_calls(calls)


        # Force a throttle message.
        clock.return_value += 100
        mock_logger = mock.Mock()
        logging.basicConfig()
        logger = swailing.Logger(mock_logger, 1, 1)
        for i in xrange(10):
            logger.error("foo")
        clock.return_value += 100
        logger.error("bar")

        calls = [
            mock.call(logging.ERROR, "foo"),
            mock.call(logging.ERROR, ""),
            mock.call(logging.ERROR, "(... throttled %d messages ...)", 9),
            mock.call(logging.ERROR, ""),
            mock.call(logging.ERROR, "bar"),
        ]
        mock_logger.log.assert_has_calls(calls)
Пример #14
0
    def test_throttle(self, clock):
        mock_logger = mock.Mock()
        clock.return_value = 1
        logger = swailing.Logger(mock_logger,
                                 1,
                                 2,
                                 structured_detail=False,
                                 with_prefix=False)

        # We'll write a buch of times, but since capacity is just 2,
        # we should expect the root_logger to only have been called
        # twice.
        for i in xrange(10):
            with logger.info() as L:
                L.primary("primary %d" % i)
                L.detail("detail %d" % i)
                L.hint("hint %d" % i)

        calls = [
            mock.call(logging.INFO, "primary 0"),
            mock.call(logging.INFO, "detail 0"),
            mock.call(logging.INFO, "hint 0"),
            mock.call(logging.INFO, "primary 1"),
            mock.call(logging.INFO, "detail 1"),
            mock.call(logging.INFO, "hint 1"),
        ]
        mock_logger.log.assert_has_calls(calls)

        # Now let's wait a second so we get one more token, then run test again.
        clock.return_value += 1
        mock_logger.reset_mock()

        for i in xrange(10):
            with logger.info() as L:
                L.primary("primary %d" % i)
                L.detail("detail %d" % i)
                L.hint("hint %d" % i)

        calls = [
            mock.call(logging.INFO, "primary 0"),
            mock.call(logging.INFO, "detail 0"),
            mock.call(logging.INFO, "hint 0"),
        ]
        mock_logger.log.assert_has_calls(calls)

        # Force a throttle message.
        clock.return_value += 100
        mock_logger = mock.Mock()
        logging.basicConfig()
        logger = swailing.Logger(mock_logger,
                                 1,
                                 1,
                                 structured_detail=False,
                                 with_prefix=False)
        for i in xrange(10):
            logger.error("foo")
        clock.return_value += 100
        logger.error("bar")

        calls = [
            mock.call(logging.ERROR, "foo"),
            mock.call(logging.ERROR, ""),
            mock.call(logging.ERROR, "(... throttled %d messages ...)", 9),
            mock.call(logging.ERROR, ""),
            mock.call(logging.ERROR, "bar"),
        ]
        mock_logger.log.assert_has_calls(calls)