b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname my_appname 111'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'appname': 'my_loooooooooooooooooooooooooooooooooooooooooong_appname'},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname my_loooooooooooooooooooooooooooooooooooooooooong 111'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'appname': 1234},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname 1234 111'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'appname': 'my_Δapp \nname'},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname my_appname 111'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'appname': SomeClass()},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname MyClassObject 111'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ),
])
def test_appname(logger, handler_kwargs, expected):
    sh = Rfc5424SysLogHandler(**handler_kwargs)
    logger.addHandler(sh)
    with patch.object(sh, 'socket') as syslog_socket:
        logger.info(message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()

        logger.log(logging.INFO, message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()
     ),
    ({
        'extra': {
            'msgid': 1234
        }
    }, b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111'
     b' 1234 - \xef\xbb\xbfThis is an interesting message'),
    ({
        'extra': {
            'msgid': 'my_Δmsg \nid'
        }
    }, b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111'
     b' my_msgid - \xef\xbb\xbfThis is an interesting message'),
    ({
        'extra': {
            'msgid': SomeClass()
        }
    }, b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111'
     b' MyClassObject - \xef\xbb\xbfThis is an interesting message'),
])
def test_msgid(logger, logger_kwargs, expected):
    sh = Rfc5424SysLogHandler(address=address)
    logger.addHandler(sh)
    with patch.object(sh.transport, 'socket') as syslog_socket:
        logger.info(message, **logger_kwargs)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()

        logger.log(logging.INFO, message, **logger_kwargs)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()
                                         'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
                                         'ooooooooooooooooooooooooong-hostname'},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 my-looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
        b'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
        b'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong root 111'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'hostname': 1234},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 1234 root 111'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'hostname': 'my-Δhost \nname'},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 my-hostname root 111'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'hostname': SomeClass()},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 MyClassObject root 111'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ),
])
def test_hostname_at_init(logger, handler_kwargs, expected):
    sh = Rfc5424SysLogHandler(**handler_kwargs)
    logger.addHandler(sh)
    with patch.object(sh, 'socket') as syslog_socket:
        logger.info(message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()

        logger.log(logging.INFO, message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()
     b'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
     b'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong root 111'
     b' - - \xef\xbb\xbfThis is an interesting message'),
    ({
        'address': address,
        'hostname': 1234
    }, b'<14>1 2000-01-01T17:11:11.111111+06:00 1234 root 111'
     b' - - \xef\xbb\xbfThis is an interesting message'),
    ({
        'address': address,
        'hostname': 'my-Δhost \nname'
    }, b'<14>1 2000-01-01T17:11:11.111111+06:00 my-hostname root 111'
     b' - - \xef\xbb\xbfThis is an interesting message'),
    ({
        'address': address,
        'hostname': SomeClass()
    }, b'<14>1 2000-01-01T17:11:11.111111+06:00 MyClassObject root 111'
     b' - - \xef\xbb\xbfThis is an interesting message'),
])
def test_hostname_at_init(logger, handler_kwargs, expected):
    sh = Rfc5424SysLogHandler(**handler_kwargs)
    logger.addHandler(sh)
    with patch.object(sh.transport, 'socket') as syslog_socket:
        logger.info(message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()

        logger.log(logging.INFO, message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()
    ), (
        {'address': address, 'procid': '1234123412341234123412341234123412341234123412341234123412341234123412341234'
                                       '1234123412341234123412341234123412341234123412341234aaa'},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 123412341234123412341234123412341234123412341234'
        b'12341234123412341234123412341234123412341234123412341234123412341234123412341234'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'procid': 1234},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 1234'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'procid': '12 \nΔ34'},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 1234'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ), (
        {'address': address, 'procid': SomeClass()},
        b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root MyClassObject'
        b' - - \xef\xbb\xbfThis is an interesting message'
    ),
])
def test_procid(logger, handler_kwargs, expected):
    sh = Rfc5424SysLogHandler(**handler_kwargs)
    logger.addHandler(sh)
    with patch.object(sh, 'socket') as syslog_socket:
        logger.info(message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()

        logger.log(logging.INFO, message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()
Example #6
0
     b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 123412341234123412341234123412341234123412341234'
     b'12341234123412341234123412341234123412341234123412341234123412341234123412341234'
     b' - - \xef\xbb\xbfThis is an interesting message'),
    ({
        'address': address,
        'procid': 1234
    }, b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 1234'
     b' - - \xef\xbb\xbfThis is an interesting message'),
    ({
        'address': address,
        'procid': '12 \nΔ34'
    }, b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 1234'
     b' - - \xef\xbb\xbfThis is an interesting message'),
    ({
        'address': address,
        'procid': SomeClass()
    },
     b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root MyClassObject'
     b' - - \xef\xbb\xbfThis is an interesting message'),
])
def test_procid(logger, handler_kwargs, expected):
    sh = Rfc5424SysLogHandler(**handler_kwargs)
    logger.addHandler(sh)
    with patch.object(sh.transport, 'socket') as syslog_socket:
        logger.info(message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()

        logger.log(logging.INFO, message)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()