Send Gelf Message to Graylog with Tags

  • Share this:

Code introduction


This function creates a Gelf message object and sends it to a Graylog server. It takes a message and a list of tags as inputs.


Technology Stack : graylog2.gelf, graylog2.client

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
def graylog_message_filter(message, tags):
    from graylog2.gelf import GelfMessage
    from graylog2.client import Graylog2Client

    # Create a GelfMessage object with the given message and tags
    gelf_message = GelfMessage(message, version="1.1", additional_data={'tags': tags})

    # Create a Graylog2Client instance
    graylog_client = Graylog2Client('localhost', 12201)

    # Send the GelfMessage to Graylog
    graylog_client.gelf_message(gelf_message)

    return "Message sent to Graylog with tags: {}".format(tags)