Exemple #1
0
class TestLogJSONFile(TestCase):
    def setUp(self):
        self.logger = LogObject(name='test', json=True,
                                dir='.', level='INFO', stdout=False,
                                file='test.log')
        self.test_file = './test'

    def test_log_file_json(self):
        self.logger._get_time = MagicMock(return_value='2015-11-12T10:11:12.0Z')
        self.logger.info("Test log")
        with open(self.test_file + '.log', 'r') as f:
            read_data = f.read()
            the_dict = json.loads(read_data)
            six.assertCountEqual(self, the_dict, {
                "message": "Test log",
                "level": "INFO",
                "logger":"test",
                "timestamp":"2015-11-12T10:11:12.0Z"})

    def test_preserve_extra(self):
        self.logger.log_level = 'DEBUG'
        before = {"some": "dict", "a": 1}
        preserve = copy.deepcopy(before)
        with LogCapture() as l:
            self.logger.debug("my message", extra=before)

        self.assertEqual(preserve, before)

    def tearDown(self):
        os.remove(self.test_file + '.log')
        os.remove(self.test_file + '.lock')
Exemple #2
0
 def setUp(self):
     self.logger = LogObject(name='test',
                             json=True,
                             dir='.',
                             level='INFO',
                             stdout=False,
                             file='test.log')
     self.test_file = './test'
 def setUp(self):
     self.logger = LogObject(
         name='test',
         json=True,
         dir='.',
         level='INFO',
         stdout=False,
     )
     self.logger.x = 1
Exemple #4
0
class TestLogJSONFile(TestCase):
    def setUp(self):
        self.logger = LogObject(name='test',
                                json=True,
                                dir='.',
                                level='INFO',
                                stdout=False,
                                file='test.log')
        self.test_file = './test'

    def test_log_file_json(self):
        self.logger._get_time = MagicMock(
            return_value='2015-11-12T10:11:12.0Z')
        self.logger.info("Test log")
        with open(self.test_file + '.log', 'r') as f:
            read_data = f.read()
            the_dict = json.loads(read_data)
            self.assertItemsEqual(
                the_dict, {
                    "message": "Test log",
                    "level": "INFO",
                    "logger": "test",
                    "timestamp": "2015-11-12T10:11:12.0Z"
                })

    def test_preserve_extra(self):
        self.logger.log_level = 'DEBUG'
        before = {"some": "dict", "a": 1}
        preserve = copy.deepcopy(before)
        with LogCapture() as l:
            self.logger.debug("my message", extra=before)

        self.assertEqual(preserve, before)

    def tearDown(self):
        os.remove(self.test_file + '.log')
        os.remove(self.test_file + '.lock')
Exemple #5
0
 def setUp(self):
     self.logger = LogObject(name='test', json=True,
                             dir='.', level='INFO', stdout=False,
                             file='test.log')
     self.test_file = './test'
Exemple #6
0
 def setUp(self):
     self.logger = LogObject(name='test', json=True,
                             dir='.', level='INFO', stdout=False,
                             )
     self.logger.x = 1
Exemple #7
0
class TestLogCallbacks(TestCase):
    def setUp(self):
        self.logger = LogObject(name='test', json=True,
                                dir='.', level='INFO', stdout=False,
                                )
        self.logger.x = 1

    def test_log_callbacks_integration(self):
        def add_1(log_message=None, log_extra=None):
            self.logger.x += 1

        def negate(log_message=None, log_extra=None):
            self.logger.x *= -1

        def multiply_5(log_message=None, log_extra=None):
            self.logger.x *= 5

        self.logger.register_callback('<=INFO', add_1, {'key': 'val1'})
        self.logger.register_callback('<=INFO', negate, {'key': 'val2'})
        self.logger.register_callback('<=INFO', multiply_5)

        self.logger.x = 1
        self.logger.log_level = 'INFO'
        self.logger.info('info message')
        self.assertEqual(5, self.logger.x)

        self.logger.x = 1
        self.logger.log_level = 'INFO'
        self.logger.info('info message', extra={'key': 'val1'})
        self.assertEqual(10, self.logger.x)

        self.logger.x = 1
        self.logger.log_level = 'INFO'
        self.logger.info('info message', extra={'key': 'val2'})
        self.assertEqual(-5, self.logger.x)

        # Callback shouldn't fire
        self.logger.x = 1
        self.logger.log_level = 'CRITICAL'
        self.logger.info('info message')
        self.assertEqual(1, self.logger.x)

        # Callback shouldn't fire
        self.logger.x = 1
        self.logger.log_level = 'INFO'
        self.logger.warning('warning message')
        self.assertEqual(1, self.logger.x)

    def test_parse_log_level(self):
        log_range = self.logger.parse_log_level("<=INFO")
        self.assertEqual([0,1], log_range)

        log_range = self.logger.parse_log_level("<INFO")
        self.assertEqual([0], log_range)

        log_range = self.logger.parse_log_level(">=WARNING")
        self.assertEqual([2,3,4], log_range)

        log_range = self.logger.parse_log_level(">WARN")
        self.assertEqual([3,4], log_range)

        log_range = self.logger.parse_log_level("=INFO")
        self.assertEqual([1], log_range)

        log_range = self.logger.parse_log_level("CRITICAL")
        self.assertEqual([4], log_range)

    def test_register_callback(self):
        def add_1(log_obj, log_message=None, log_extra=None):
            pass

        def add_2(log_obj, log_message=None, log_extra=None):
            pass

        def add_3(log_obj, log_message=None, log_extra=None):
            pass

        def add_4(log_obj, log_message=None, log_extra=None):
            pass

        self.logger.register_callback('>=INFO', add_1)
        self.logger.register_callback('<=WARN', add_2)
        self.logger.register_callback('ERROR', add_3)
        self.logger.register_callback('*', add_4)

        callbacks = [cb for cb,criteria in self.logger.callbacks['DEBUG']]
        self.assertEqual([add_2, add_4], callbacks)

        callbacks = [cb for cb,criteria in self.logger.callbacks['INFO']]
        self.assertEqual([add_1, add_2, add_4], callbacks)

        callbacks = [cb for cb,criteria in self.logger.callbacks['WARNING']]
        self.assertEqual([add_1, add_2, add_4], callbacks)

        callbacks = [cb for cb,criteria in self.logger.callbacks['ERROR']]
        self.assertEqual([add_1, add_3, add_4], callbacks)

        callbacks = [cb for cb,criteria in self.logger.callbacks['CRITICAL']]
        self.assertEqual([add_1, add_4], callbacks)

    def test_fire_callbacks_basic_1(self):
        def add_1(log_message=None, log_extra=None):
            self.logger.x += 1

        def negate(log_message=None, log_extra=None):
            self.logger.x *= -1

        def multiply_5(log_message=None, log_extra=None):
            self.logger.x *= 5

        self.logger.register_callback('<=INFO', add_1)
        self.logger.register_callback('INFO', negate)
        self.logger.register_callback('<CRITICAL', add_1)
        self.logger.register_callback('>DEBUG', multiply_5)

        self.logger.x = 0
        self.logger.log_level = 'DEBUG'
        self.logger.fire_callbacks('DEBUG')
        self.assertEqual(2, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'INFO'
        self.logger.fire_callbacks('INFO')
        self.assertEqual(0, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'WARNING'
        self.logger.fire_callbacks('WARNING')
        self.assertEqual(5, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'ERROR'
        self.logger.fire_callbacks('ERROR')
        self.assertEqual(5, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'CRITICAL'
        self.logger.fire_callbacks('CRITICAL')
        self.assertEqual(0, self.logger.x)

    def test_fire_callbacks_basic_2(self):
        def add_1(log_message=None, log_extra=None):
            self.logger.x += 1

        def negate(log_message=None, log_extra=None):
            self.logger.x *= -1

        def multiply_5(log_message=None, log_extra=None):
            self.logger.x *= 5

        self.logger.register_callback('>DEBUG', add_1)
        self.logger.register_callback('=WARNING', negate)
        self.logger.register_callback('<INFO', add_1)
        self.logger.register_callback('>=INFO', multiply_5)
        self.logger.register_callback('*', add_1)

        self.logger.x = 0
        self.logger.log_level = 'DEBUG'
        self.logger.fire_callbacks('DEBUG')
        self.assertEqual(2, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'INFO'
        self.logger.fire_callbacks('INFO')
        self.assertEqual(6, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'WARNING'
        self.logger.fire_callbacks('WARNING')
        self.assertEqual(-4, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'ERROR'
        self.logger.fire_callbacks('ERROR')
        self.assertEqual(6, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'CRITICAL'
        self.logger.fire_callbacks('CRITICAL')
        self.assertEqual(6, self.logger.x)

    def test_preserve_data(self):
        message = "THIS IS A TEST"
        extras = {"key": "value", 'a': [1, 2, 3]}

        def cb(log_message=None, log_extra=None):
            self.assertEquals(log_message, message)
            self.assertEquals(log_extra, extras)

        self.logger.register_callback('>DEBUG', cb)
        self.logger.log_level = 'INFO'
        self.logger.info(message, extras)

    def tearDown(self):
        os.remove('main.log')
        os.remove('main.lock')
class TestLogCallbacks(TestCase):
    def setUp(self):
        self.logger = LogObject(
            name='test',
            json=True,
            dir='.',
            level='INFO',
            stdout=False,
        )
        self.logger.x = 1

    def test_log_callbacks_integration(self):
        def add_1(log_message=None, log_extra=None):
            self.logger.x += 1

        def negate(log_message=None, log_extra=None):
            self.logger.x *= -1

        def multiply_5(log_message=None, log_extra=None):
            self.logger.x *= 5

        self.logger.register_callback('<=INFO', add_1, {'key': 'val1'})
        self.logger.register_callback('<=INFO', negate, {'key': 'val2'})
        self.logger.register_callback('<=INFO', multiply_5)

        self.logger.x = 1
        self.logger.log_level = 'INFO'
        self.logger.info('info message')
        self.assertEqual(5, self.logger.x)

        self.logger.x = 1
        self.logger.log_level = 'INFO'
        self.logger.info('info message', extra={'key': 'val1'})
        self.assertEqual(10, self.logger.x)

        self.logger.x = 1
        self.logger.log_level = 'INFO'
        self.logger.info('info message', extra={'key': 'val2'})
        self.assertEqual(-5, self.logger.x)

        # Callback shouldn't fire
        self.logger.x = 1
        self.logger.log_level = 'CRITICAL'
        self.logger.info('info message')
        self.assertEqual(1, self.logger.x)

        # Callback shouldn't fire
        self.logger.x = 1
        self.logger.log_level = 'INFO'
        self.logger.warning('warning message')
        self.assertEqual(1, self.logger.x)

    def test_parse_log_level(self):
        log_range = self.logger.parse_log_level("<=INFO")
        self.assertEqual([0, 1], log_range)

        log_range = self.logger.parse_log_level("<INFO")
        self.assertEqual([0], log_range)

        log_range = self.logger.parse_log_level(">=WARNING")
        self.assertEqual([2, 3, 4], log_range)

        log_range = self.logger.parse_log_level(">WARN")
        self.assertEqual([3, 4], log_range)

        log_range = self.logger.parse_log_level("=INFO")
        self.assertEqual([1], log_range)

        log_range = self.logger.parse_log_level("CRITICAL")
        self.assertEqual([4], log_range)

    def test_register_callback(self):
        def add_1(log_obj, log_message=None, log_extra=None):
            pass

        def add_2(log_obj, log_message=None, log_extra=None):
            pass

        def add_3(log_obj, log_message=None, log_extra=None):
            pass

        def add_4(log_obj, log_message=None, log_extra=None):
            pass

        self.logger.register_callback('>=INFO', add_1)
        self.logger.register_callback('<=WARN', add_2)
        self.logger.register_callback('ERROR', add_3)
        self.logger.register_callback('*', add_4)

        callbacks = [cb for cb, criteria in self.logger.callbacks['DEBUG']]
        self.assertEqual([add_2, add_4], callbacks)

        callbacks = [cb for cb, criteria in self.logger.callbacks['INFO']]
        self.assertEqual([add_1, add_2, add_4], callbacks)

        callbacks = [cb for cb, criteria in self.logger.callbacks['WARNING']]
        self.assertEqual([add_1, add_2, add_4], callbacks)

        callbacks = [cb for cb, criteria in self.logger.callbacks['ERROR']]
        self.assertEqual([add_1, add_3, add_4], callbacks)

        callbacks = [cb for cb, criteria in self.logger.callbacks['CRITICAL']]
        self.assertEqual([add_1, add_4], callbacks)

    def test_fire_callbacks_basic_1(self):
        def add_1(log_message=None, log_extra=None):
            self.logger.x += 1

        def negate(log_message=None, log_extra=None):
            self.logger.x *= -1

        def multiply_5(log_message=None, log_extra=None):
            self.logger.x *= 5

        self.logger.register_callback('<=INFO', add_1)
        self.logger.register_callback('INFO', negate)
        self.logger.register_callback('<CRITICAL', add_1)
        self.logger.register_callback('>DEBUG', multiply_5)

        self.logger.x = 0
        self.logger.log_level = 'DEBUG'
        self.logger.fire_callbacks('DEBUG')
        self.assertEqual(2, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'INFO'
        self.logger.fire_callbacks('INFO')
        self.assertEqual(0, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'WARNING'
        self.logger.fire_callbacks('WARNING')
        self.assertEqual(5, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'ERROR'
        self.logger.fire_callbacks('ERROR')
        self.assertEqual(5, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'CRITICAL'
        self.logger.fire_callbacks('CRITICAL')
        self.assertEqual(0, self.logger.x)

    def test_fire_callbacks_basic_2(self):
        def add_1(log_message=None, log_extra=None):
            self.logger.x += 1

        def negate(log_message=None, log_extra=None):
            self.logger.x *= -1

        def multiply_5(log_message=None, log_extra=None):
            self.logger.x *= 5

        self.logger.register_callback('>DEBUG', add_1)
        self.logger.register_callback('=WARNING', negate)
        self.logger.register_callback('<INFO', add_1)
        self.logger.register_callback('>=INFO', multiply_5)
        self.logger.register_callback('*', add_1)

        self.logger.x = 0
        self.logger.log_level = 'DEBUG'
        self.logger.fire_callbacks('DEBUG')
        self.assertEqual(2, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'INFO'
        self.logger.fire_callbacks('INFO')
        self.assertEqual(6, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'WARNING'
        self.logger.fire_callbacks('WARNING')
        self.assertEqual(-4, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'ERROR'
        self.logger.fire_callbacks('ERROR')
        self.assertEqual(6, self.logger.x)

        self.logger.x = 0
        self.logger.log_level = 'CRITICAL'
        self.logger.fire_callbacks('CRITICAL')
        self.assertEqual(6, self.logger.x)

    def test_preserve_data(self):
        message = "THIS IS A TEST"
        extras = {"key": "value", 'a': [1, 2, 3]}

        def cb(log_message=None, log_extra=None):
            self.assertEquals(log_message, message)
            self.assertEquals(log_extra, extras)

        self.logger.register_callback('>DEBUG', cb)
        self.logger.log_level = 'INFO'
        self.logger.info(message, extras)

    def tearDown(self):
        os.remove('main.log')
        os.remove('main.lock')