Пример простого SYSLOG сервера


--- app.py
  • app.py
#!/usr/bin/env python3


import logging
import socketserver

from logging.handlers import RotatingFileHandler


LOG_FILE = 'youlogfile.log'
LONG_LOG_FORMAT = '%(asctime)s - [%(name)s.%(levelname)s] [%(threadName)s, %(module)s.%(funcName)s@%(lineno)d] %(message)s'
HOST, PORT = "0.0.0.0", 514

# Логирование
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = RotatingFileHandler(
    LOG_FILE,
    maxBytes=128*1024*1024,
    backupCount=1
)
formatter = logging.Formatter(LONG_LOG_FORMAT)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.addHandler( logging.StreamHandler() )


class SyslogUDPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        data = bytes.decode( self.request[0].strip() )
        logger.info( data )


if __name__ == "__main__":
    try:
        server = socketserver.UDPServer((HOST,PORT), SyslogUDPHandler)
        server.serve_forever(poll_interval=0.5)
    except (IOError, SystemExit):
        raise
    except KeyboardInterrupt:
        print ("Crtl+C Pressed. Shutting down.")
© RemiZOffAlex