slantalpha
slantalpha

Reputation: 585

How can I receive syslog messages in a Python 3.6 program?

I did a quick Google search and saw a few Python modules for sending syslog messages.

How can I receive syslog messages in a Python program? Does the Python 3.6 standard library have something that I'm not aware of that does this?

Upvotes: 2

Views: 2306

Answers (2)

Guy Gangemi
Guy Gangemi

Reputation: 1773

By default rsyslog uses UDP. Python standard lib has a UDP Server example. I went even more bare bones.

import socketserver

class MyUDPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        print(self.request[0])

with socketserver.UDPServer(('10.0.1.90', 9999), MyUDPHandler) as server:
    server.serve_forever()

Triggering a rsyslog message on my test device resulted in:

b'<174>Oct 15 13:59:08 CN-6-12 Event: Logged into console: admin'

Served my purposes for testing the syslog functionality of my test device.

Upvotes: 4

Eugene Morozov
Eugene Morozov

Reputation: 15806

Syslog messages are supposed to be received by the syslog daemon. You couldn't find python modules for receiving syslog messages because usually there is no need for a python program to receive syslog messages. There is usually only one syslog daemon receiving syslog messages per machine.

Maybe you should look for this instead: Logging module for Python? It is not a replacement for syslog, but it is more appropriate choice for logging in 99% of python programs.

Upvotes: 1

Related Questions