Create a gist now

Instantly share code, notes, and snippets.

Embed
Tiny Python Syslog Server
#!/usr/bin/env python
## Tiny Syslog Server in Python.
##
## This is a tiny syslog server that is able to receive UDP based syslog
## entries on a specified port and save them to a file.
## That's it... it does nothing else...
## There are a few configuration parameters.
LOG_FILE = 'youlogfile.log'
HOST, PORT = "0.0.0.0", 514
#
# NO USER SERVICEABLE PARTS BELOW HERE...
#
import logging
import SocketServer
logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=LOG_FILE, filemode='a')
class SyslogUDPHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = bytes.decode(self.request[0].strip())
socket = self.request[1]
print( "%s : " % self.client_address[0], str(data))
logging.info(str(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.")
@iobear-zz

This comment has been minimized.

Show comment
Hide comment
@iobear-zz

iobear-zz Mar 27, 2013

Thanks, I've used it as a syslog frontend to redis.

https://github.com/iobear/beewatch/blob/master/bin/psyslog.py

Thanks, I've used it as a syslog frontend to redis.

https://github.com/iobear/beewatch/blob/master/bin/psyslog.py

@seefor

This comment has been minimized.

Show comment
Hide comment
@seefor

seefor Jan 29, 2015

Great Script BTW
Question, instead of Crtl+C to end, how would you have it end in 30 minutes?

seefor commented Jan 29, 2015

Great Script BTW
Question, instead of Crtl+C to end, how would you have it end in 30 minutes?

@somma

This comment has been minimized.

Show comment
Hide comment
@somma

somma Apr 20, 2015

Thank's for sharing nice code. 😃

somma commented Apr 20, 2015

Thank's for sharing nice code. 😃

@ilmarh

This comment has been minimized.

Show comment
Hide comment

ilmarh commented Jun 5, 2015

Thanks!

@pickeld

This comment has been minimized.

Show comment
Hide comment

pickeld commented Jul 18, 2015

Thanks!!

@lidaling

This comment has been minimized.

Show comment
Hide comment
@lidaling

lidaling Sep 8, 2015

I am using rsyslog on centos as a client .
But I can't get message with your code.
Please help me.
: )

lidaling commented Sep 8, 2015

I am using rsyslog on centos as a client .
But I can't get message with your code.
Please help me.
: )

@netravler

This comment has been minimized.

Show comment
Hide comment
@netravler

netravler Mar 30, 2016

Excellent! just what I needed...

Thank you:)

Excellent! just what I needed...

Thank you:)

@pakru

This comment has been minimized.

Show comment
Hide comment
@pakru

pakru May 18, 2017

Nice one!

Thanks!

pakru commented May 18, 2017

Nice one!

Thanks!

@rkreddy46

This comment has been minimized.

Show comment
Hide comment
@rkreddy46

rkreddy46 Jun 2, 2017

Thanks for the initiative. How do we add TLS support to this?

Thanks for the initiative. How do we add TLS support to this?

@sagism

This comment has been minimized.

Show comment
Hide comment
@sagism

sagism Sep 10, 2017

Can you explain why would I use this instead of the real syslog facility?

sagism commented Sep 10, 2017

Can you explain why would I use this instead of the real syslog facility?

@kmombergZF

This comment has been minimized.

Show comment
Hide comment
@kmombergZF

kmombergZF Nov 24, 2017

You can add utf-8 encoding to improve the server:

def handle(self):
    data = bytes.decode(self.request[0].strip(), encoding="utf-8")
    socket = self.request[1]
    print( "%s : " % self.client_address[0], str(data.encode("utf-8")))
    logging.info(str(data.encode("utf-8")))

It worked very well when I needed a syslog server for some tests. Thanks for sharing.

You can add utf-8 encoding to improve the server:

def handle(self):
    data = bytes.decode(self.request[0].strip(), encoding="utf-8")
    socket = self.request[1]
    print( "%s : " % self.client_address[0], str(data.encode("utf-8")))
    logging.info(str(data.encode("utf-8")))

It worked very well when I needed a syslog server for some tests. Thanks for sharing.

@truthadjustr

This comment has been minimized.

Show comment
Hide comment
@truthadjustr

truthadjustr Mar 28, 2018

Cute. If you are in python3, simply change SocketServer to socketserver. Official reference here

Cute. If you are in python3, simply change SocketServer to socketserver. Official reference here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment