예제 #1
0
    def test_extra_object_serialization(self):
        class MyClass1(object):
            def __repr__(self):
                return 'repr'

        class MyClass2(object):
            def to_dict(self):
                return 'to_dict'

        class MyClass3(object):
            def to_serializable_dict(self, mask_secrets=False):
                return 'to_serializable_dict'

        formatter = GelfLogFormatter()

        record = MockRecord()
        record.msg = 'message'
        record._obj1 = MyClass1()
        record._obj2 = MyClass2()
        record._obj3 = MyClass3()

        message = formatter.format(record=record)
        parsed = json.loads(message)
        self.assertEqual(parsed['_obj1'], 'repr')
        self.assertEqual(parsed['_obj2'], 'to_dict')
        self.assertEqual(parsed['_obj3'], 'to_serializable_dict')
예제 #2
0
    def test_extra_object_serialization(self):
        class MyClass1(object):
            def __repr__(self):
                return 'repr'

        class MyClass2(object):
            def to_dict(self):
                return 'to_dict'

        class MyClass3(object):
            def to_serializable_dict(self):
                return 'to_serializable_dict'

        formatter = GelfLogFormatter()

        record = MockRecord()
        record.msg = 'message'
        record._obj1 = MyClass1()
        record._obj2 = MyClass2()
        record._obj3 = MyClass3()

        message = formatter.format(record=record)
        parsed = json.loads(message)
        self.assertEqual(parsed['_obj1'], 'repr')
        self.assertEqual(parsed['_obj2'], 'to_dict')
        self.assertEqual(parsed['_obj3'], 'to_serializable_dict')
예제 #3
0
파일: test_logger.py 프로젝트: nzlosh/st2
    def test_format_blacklisted_attributes_are_masked(self):
        formatter = GelfLogFormatter()

        # Some extra attributes
        mock_message = 'test message 1'

        record = MockRecord()
        record.msg = mock_message

        # Add "extra" attributes
        record._blacklisted_1 = 'test value 1'
        record._blacklisted_2 = 'test value 2'
        record._blacklisted_3 = {'key1': 'val1', 'blacklisted_1': 'val2', 'key3': 'val3'}
        record._foo1 = 'bar'

        message = formatter.format(record=record)
        parsed = json.loads(message)

        self.assertEqual(parsed['_blacklisted_1'], MASKED_ATTRIBUTE_VALUE)
        self.assertEqual(parsed['_blacklisted_2'], MASKED_ATTRIBUTE_VALUE)
        self.assertEqual(parsed['_blacklisted_3']['key1'], 'val1')
        self.assertEqual(parsed['_blacklisted_3']['blacklisted_1'], MASKED_ATTRIBUTE_VALUE)
        self.assertEqual(parsed['_blacklisted_3']['key3'], 'val3')
        self.assertEqual(parsed['_foo1'], 'bar')

        # Assert that the original dict is left unmodified
        self.assertEqual(record._blacklisted_1, 'test value 1')
        self.assertEqual(record._blacklisted_2, 'test value 2')
        self.assertEqual(record._blacklisted_3['key1'], 'val1')
        self.assertEqual(record._blacklisted_3['blacklisted_1'], 'val2')
        self.assertEqual(record._blacklisted_3['key3'], 'val3')
    def test_format_blacklisted_attributes_are_masked(self):
        formatter = GelfLogFormatter()

        # Some extra attributes
        mock_message = 'test message 1'

        record = MockRecord()
        record.msg = mock_message

        # Add "extra" attributes
        record._blacklisted_1 = 'test value 1'
        record._blacklisted_2 = 'test value 2'
        record._blacklisted_3 = {'key1': 'val1', 'blacklisted_1': 'val2', 'key3': 'val3'}
        record._foo1 = 'bar'

        message = formatter.format(record=record)
        parsed = json.loads(message)

        self.assertEqual(parsed['_blacklisted_1'], MASKED_ATTRIBUTE_VALUE)
        self.assertEqual(parsed['_blacklisted_2'], MASKED_ATTRIBUTE_VALUE)
        self.assertEqual(parsed['_blacklisted_3']['key1'], 'val1')
        self.assertEqual(parsed['_blacklisted_3']['blacklisted_1'], MASKED_ATTRIBUTE_VALUE)
        self.assertEqual(parsed['_blacklisted_3']['key3'], 'val3')
        self.assertEqual(parsed['_foo1'], 'bar')

        # Assert that the original dict is left unmodified
        self.assertEqual(record._blacklisted_1, 'test value 1')
        self.assertEqual(record._blacklisted_2, 'test value 2')
        self.assertEqual(record._blacklisted_3['key1'], 'val1')
        self.assertEqual(record._blacklisted_3['blacklisted_1'], 'val2')
        self.assertEqual(record._blacklisted_3['key3'], 'val3')
예제 #5
0
    def test_format(self):
        formatter = GelfLogFormatter()

        expected_keys = [
            'version', 'host', 'short_message', 'full_message', 'timestamp',
            'timestamp_f', 'level'
        ]

        # No extra attributes
        mock_message = 'test message 1'

        record = MockRecord()
        record.msg = mock_message

        message = formatter.format(record=record)
        parsed = json.loads(message)

        for key in expected_keys:
            self.assertTrue(key in parsed)

        self.assertEqual(parsed['short_message'], mock_message)
        self.assertEqual(parsed['full_message'], mock_message)

        # Some extra attributes
        mock_message = 'test message 2'

        record = MockRecord()
        record.msg = mock_message

        # Add "extra" attributes
        record._user_id = 1
        record._value = 'bar'
        record.ignored = 'foo'  # this one is ignored since it doesnt have a prefix
        record.created = 1234.5678

        message = formatter.format(record=record)
        parsed = json.loads(message)

        for key in expected_keys:
            self.assertTrue(key in parsed)

        self.assertEqual(parsed['short_message'], mock_message)
        self.assertEqual(parsed['full_message'], mock_message)
        self.assertEqual(parsed['_user_id'], 1)
        self.assertEqual(parsed['_value'], 'bar')
        self.assertEqual(parsed['timestamp'], 1234)
        self.assertEqual(parsed['timestamp_f'], 1234.5678)
        self.assertTrue('ignored' not in parsed)

        # Record with an exception
        mock_exception = Exception('mock exception bar')

        try:
            raise mock_exception
        except Exception:
            mock_exc_info = sys.exc_info()

        # Some extra attributes
        mock_message = 'test message 3'

        record = MockRecord()
        record.msg = mock_message
        record.exc_info = mock_exc_info

        message = formatter.format(record=record)
        parsed = json.loads(message)

        for key in expected_keys:
            self.assertTrue(key in parsed)

        self.assertEqual(parsed['short_message'], mock_message)
        self.assertTrue(mock_message in parsed['full_message'])
        self.assertTrue('Traceback' in parsed['full_message'])
        self.assertTrue('_exception' in parsed)
        self.assertTrue('_traceback' in parsed)
예제 #6
0
    def test_format(self):
        formatter = GelfLogFormatter()

        expected_keys = ['version', 'host', 'short_message', 'full_message',
                         'timestamp', 'level']

        # No extra attributes
        mock_message = 'test message 1'

        record = MockRecord()
        record.msg = mock_message

        message = formatter.format(record=record)
        parsed = json.loads(message)

        for key in expected_keys:
            self.assertTrue(key in parsed)

        self.assertEqual(parsed['short_message'], mock_message)
        self.assertEqual(parsed['full_message'], mock_message)

        # Some extra attributes
        mock_message = 'test message 2'

        record = MockRecord()
        record.msg = mock_message

        # Add "extra" attributes
        record._user_id = 1
        record._value = 'bar'
        record.ignored = 'foo'  # this one is ignored since it doesnt have a prefix

        message = formatter.format(record=record)
        parsed = json.loads(message)

        for key in expected_keys:
            self.assertTrue(key in parsed)

        self.assertEqual(parsed['short_message'], mock_message)
        self.assertEqual(parsed['full_message'], mock_message)
        self.assertEqual(parsed['_user_id'], 1)
        self.assertEqual(parsed['_value'], 'bar')
        self.assertTrue('ignored' not in parsed)

        # Record with an exception
        mock_exception = Exception('mock exception bar')

        try:
            raise mock_exception
        except Exception:
            mock_exc_info = sys.exc_info()

        # Some extra attributes
        mock_message = 'test message 3'

        record = MockRecord()
        record.msg = mock_message
        record.exc_info = mock_exc_info

        message = formatter.format(record=record)
        parsed = json.loads(message)

        for key in expected_keys:
            self.assertTrue(key in parsed)

        self.assertEqual(parsed['short_message'], mock_message)
        self.assertTrue(mock_message in parsed['full_message'])
        self.assertTrue('Traceback' in parsed['full_message'])
        self.assertTrue('_exception' in parsed)
        self.assertTrue('_traceback' in parsed)