Sending Events to Graylog Server with Graypy Library

  • Share this:

Code introduction


This function uses the graypy library from the Graylog third-party library to send events to the Graylog server. It takes a message and a source as parameters, then formats these informations into JSON format and sends them to the Graylog server via a Socket connection.


Technology Stack : graypy, json

Code Type : Function

Code Difficulty : Intermediate


                
                    
def graylog_send_event(message, source):
    from graypy import SocketHandler
    import json

    def format_message(message):
        return json.dumps({"message": message, "source": source})

    handler = SocketHandler("localhost", 12201)
    handler.setFormatter(json.dumps)
    handler.connect()
    handler.send(format_message(message))
    handler.close()                
              
Tags: