Skip to content

Instantly share code, notes, and snippets.

@marcelom
Created December 5, 2012 18:06
Show Gist options
  • Save marcelom/4218010 to your computer and use it in GitHub Desktop.
Save marcelom/4218010 to your computer and use it in GitHub Desktop.
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.")
@sagism
Copy link

sagism commented Sep 10, 2017

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

@kmombergZF
Copy link

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
Copy link

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

@choeffer
Copy link

choeffer commented Jan 8, 2019

I developed a Python 3 implementation of a simple UDP syslog server which inserts the recieved messages into a MariaDB or MySQL database. https://github.com/choeffer/py3syslog
Your piece of code was helping me a lot to get started so I share mine also.

@laiaga
Copy link

laiaga commented May 7, 2019

Thx, using it for routers monitoring :p

@neeles83
Copy link

Nice thanks, will use it to trigger another script after syslog message is sent through EEM script.

@selimk92
Copy link

selimk92 commented Feb 2, 2020

I am just beginner in codding and my apoligise if I miss out the point but can someone explain me how to receive logs from regarding system? Don't you need to add this code in somewhere?

logging.getLogger()

@titaneric
Copy link

Thanks for this useful script to help to receive the syslog and send to Apache Kafka.

@marcelom
Copy link
Author

@maxenc7, seems like the contention is in the implementation of the logging.info() call itself. Nothing I can do there. That said, 1M/s seems a little bit too much for this tiny logger to handle. In fact, seems a lot for anything to handle in that amount of time. You are probably gonna have to use something to load balance this (haproxy or nginx) and then use several backends. This is completely outside the scope of this gist.

@marcelom
Copy link
Author

data is already the buffer. No need to double-buffer it or you will incur in extra latency. In regards to writing directly into the file, that can surely be done. You might have noticed that this code is 8+ years old, and I havent touched it (or even used it) in a long time. you are more than welcome to suggest a fix ;-)

@hett39
Copy link

hett39 commented Sep 8, 2021

how many syslogs can it process ? is there a limit

@sheracore
Copy link

sheracore commented Jan 26, 2022

Thanks a lot i used your script!

@nagasudhirpulla
Copy link

Hi, I am using this code a lot. But this server is not able to get logs when sending from a python SyslogHandler as shown in the code below

# sending logs to server
import logging
from logging.handlers import SysLogHandler
import time

logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(SysLogHandler(address=('localhost', 514)))

for i in range(20):
    logger.info("Hello World!!!")
    time.sleep(1)

@nagasudhirpulla
Copy link

Hi, I am using this code a lot. But this server is not able to get logs when sending from a python SyslogHandler as shown in the code below

# sending logs to server
import logging
from logging.handlers import SysLogHandler
import time

logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(SysLogHandler(address=('localhost', 514)))

for i in range(20):
    logger.info("Hello World!!!")
    time.sleep(1)

Hi, I resolved this issue.
It is working if send logs to "127.0.0.1" instead if "localhost"
So writing logger.addHandler(SysLogHandler(address=('localhost', 514))) solved the issue and logs are sent to python syslog listener

@mrogaski
Copy link

mrogaski commented Jan 7, 2023

It is working if send logs to "127.0.0.1" instead if "localhost"

The hostname localhost typically resolves to the 127.0.0.1 loopback address, and is reserved for this purpose in RFC 6761 section 6.3. However, not every platform defines the mapping.

@nagasudhirpulla
Copy link

It is working if send logs to "127.0.0.1" instead if "localhost"

The hostname localhost typically resolves to the 127.0.0.1 loopback address, and is reserved for this purpose in RFC 6761 section 6.3. However, not every platform defines the mapping.

👍

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