-
-
Save marcelom/4218010 to your computer and use it in GitHub Desktop.
#!/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.") |
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
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.
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.
👍
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