コード例 #1
0
ファイル: test_logging.py プロジェクト: samholt/pysoa
    def test_emit_shorter_than_limit(self):
        handler = SyslogHandler()
        handler.maximum_length = 500
        handler.overflow = SyslogHandler.OVERFLOW_BEHAVIOR_FRAGMENT
        handler.formatter = Formatter(
            'foo_file: %(name)s %(levelname)s %(message)s')

        record = LogRecord(
            name='bar_service',
            level=WARNING,
            pathname='/path/to/file.py',
            lineno=122,
            msg='This is a fairly short message',
            args=(),
            exc_info=None,
        )

        with mock.patch.object(handler, '_send') as mock_send:
            handler.emit(record)

        priority = '<{:d}>'.format(
            handler.encodePriority(handler.facility,
                                   handler.mapPriority(
                                       record.levelname)), ).encode('utf-8')

        mock_send.assert_called_once_with([
            priority +
            b'foo_file: bar_service WARNING This is a fairly short message\000',
        ])
コード例 #2
0
    def test_emit_longer_than_limit_truncate_unicode_at_boundary(self):
        # b'\xf0\x9f\x98\xb1' = u'\U0001f631' = shocked face with hands to cheeks
        handler = SyslogHandler()
        handler.maximum_length = 100
        handler.overflow = SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
        handler.formatter = Formatter('foo_file: %(name)s %(levelname)s %(message)s')
        handler.ident = '5678'

        record = LogRecord(
            name='bar_service',
            level=WARNING,
            pathname='/path/to/file.py',
            lineno=122,
            msg='This is a much longer message that is going to exceed the \U0001f631 maximum byte count and will '
                'need truncating',
            args=(),
            exc_info=None,
        )

        with mock.patch.object(handler, '_send') as mock_send:
            handler.emit(record)

        priority = '<{:d}>'.format(
            handler.encodePriority(handler.facility, handler.mapPriority(record.levelname)),
        ).encode('utf-8')

        expected1 = (
            priority +
            b'5678foo_file: bar_service WARNING This is a much longer message that is going to exceed the \000'
        )
        assert len(expected1) == 97

        mock_send.assert_called_once_with([
            expected1,
        ])
コード例 #3
0
    def test_emit_longer_than_limit_fragment_unicode_at_boundary(self):
        # b'\xf0\x9f\x98\xb1' = u'\U0001f631' = shocked face with hands to cheeks
        handler = SyslogHandler()
        handler.maximum_length = 100
        handler.overflow = SyslogHandler.OVERFLOW_BEHAVIOR_FRAGMENT
        handler.formatter = Formatter(
            'foo_file: %(name)s %(levelname)s %(message)s')

        record = LogRecord(
            name='bar_service',
            level=WARNING,
            pathname='/path/to/file.py',
            lineno=122,
            msg=
            'This is a much longer message that yes is going to \U0001f631 exceed the maximum byte count and will '
            'need truncating',
            args=(),
            exc_info=None,
        )

        with mock.patch.object(handler, '_send') as mock_send:
            handler.emit(record)

        priority = '<{:d}>'.format(
            handler.encodePriority(handler.facility,
                                   handler.mapPriority(
                                       record.levelname)),  # type: ignore
        ).encode('utf-8')

        expected1 = (
            priority +
            b"foo_file: bar_service WARNING This is a much longer message that yes is going to ... (cont'd)\000"
        )
        assert len(expected1) == 98
        expected2 = (
            priority +
            b"foo_file: bar_service WARNING (cont'd #2) ...\xf0\x9f\x98\xb1 exceed the maximum byte count and"
            b"... (cont'd)\000")
        assert len(expected2) == 100
        expected3 = (
            priority +
            b"foo_file: bar_service WARNING (cont'd #3) ... will need truncating\000"
        )
        assert len(expected3) < 100

        mock_send.assert_called_once_with([
            expected1,
            expected2,
            expected3,
        ])