def main():
    """
    Main application loop.
    """

    env = os.environ

    try:
        host = env['SYSLOG_SERVER']
        port = int(env['SYSLOG_PORT'])
        socktype = socket.SOCK_DGRAM if env['SYSLOG_PROTO'] == 'udp' \
            else socket.SOCK_STREAM
    except KeyError:
        sys.exit("SYSLOG_SERVER, SYSLOG_PORT and SYSLOG_PROTO are required.")
    facility = env.get('SYSLOG_FACILITY', 1)
    handler = SysLogHandler(address=(host, port),
                            socktype=socktype,
                            facility=facility)
    handler.setFormatter(PalletFormatter())

    for event_headers, event_data in supervisor_events(sys.stdin, sys.stdout):
        event = logging.LogRecord(
            name=event_headers['processname'],
            level=logging.INFO,
            pathname=None,
            lineno=0,
            msg=event_data,
            args=(),
            exc_info=None,
        )
        event.process = int(event_headers['pid'])
        handler.handle(event)
Example #2
0
def main():
    """
    Main application loop.
    """

    env = os.environ

    try:
        host = env['SYSLOG_SERVER']
        port = int(env['SYSLOG_PORT'])
        socktype = socket.SOCK_DGRAM if env['SYSLOG_PROTO'] == 'udp' \
            else socket.SOCK_STREAM
    except KeyError:
        sys.exit("SYSLOG_SERVER, SYSLOG_PORT and SYSLOG_PROTO are required.")

    handler = SysLogHandler(
        address=(host, port),
        socktype=socktype,
    )
    handler.setFormatter(PalletFormatter())

    for event_headers, event_data in supervisor_events(sys.stdin, sys.stdout):
        event = logging.LogRecord(
            name=event_headers['processname'],
            level=logging.INFO,
            pathname=None,
            lineno=0,
            msg=event_data,
            args=(),
            exc_info=None,
        )
        event.process = int(event_headers['pid'])
        handler.handle(event)
    def test_stdout(self):
        """
        Test printing to stdout with the fallback Syslog provider.
        """

        with tempfile.NamedTemporaryFile() as tmpfile:
            with redirect_stream(tmpfile.file.fileno()):
                syslog = forklift.services.Syslog.stdout('fake_app')
                self.assertTrue(syslog.available())
                env = syslog.environment()

                import logging
                from logging.handlers import SysLogHandler

                handler = SysLogHandler(
                    address=(env['SYSLOG_SERVER'], int(env['SYSLOG_PORT'])),
                    socktype=socket.SOCK_DGRAM
                    if env['SYSLOG_PROTO'] == 'udp' else socket.SOCK_STREAM,
                )

                handler.handle(
                    logging.LogRecord(
                        name='logname',
                        level=logging.INFO,
                        pathname='/fake/file',
                        lineno=314,
                        msg="Logging %s",
                        args="message",
                        exc_info=None,
                    ))
                handler.close()

                # Give the server a chance to process the message
                sleep(1)

            with open(tmpfile.name) as saved_output:
                log = saved_output.read()
                self.assertEqual("<14>Logging message\x00\n", log)
    def test_stdout(self):
        """
        Test printing to stdout with the fallback Syslog provider.
        """

        with tempfile.NamedTemporaryFile() as tmpfile:
            with redirect_stream(tmpfile.file.fileno()):
                syslog = forklift.services.Syslog.stdout('fake_app')
                self.assertTrue(syslog.available())
                env = syslog.environment()

                import logging
                from logging.handlers import SysLogHandler

                handler = SysLogHandler(
                    address=(env['SYSLOG_SERVER'], int(env['SYSLOG_PORT'])),
                    socktype=socket.SOCK_DGRAM
                    if env['SYSLOG_PROTO'] == 'udp'
                    else socket.SOCK_STREAM,
                )

                handler.handle(logging.LogRecord(
                    name='logname',
                    level=logging.INFO,
                    pathname='/fake/file',
                    lineno=314,
                    msg="Logging %s",
                    args="message",
                    exc_info=None,
                ))
                handler.close()

                # Give the server a chance to process the message
                sleep(1)

            with open(tmpfile.name) as saved_output:
                log = saved_output.read()
                self.assertEqual("<14>Logging message\x00\n", log)