Generate GELF Log Messages for Graylog

  • Share this:

Code introduction


This function uses the GELF protocol of Graylog to send log messages. It creates a GELF message, sets the message content and timestamp, and then sends the message to the specified Graylog server through a GELF client.


Technology Stack : Graylog2 GELF, GELFMessage, GELFClient

Code Type : Function

Code Difficulty : Advanced


                
                    
def generate_random_graylog_message(message_id, message_content):
    from graylog2_gelf import GELFMessage
    from graylog2_gelf import GELFClient

    # Create a GELF message
    gelf_message = GELFMessage(
        version='1.1',
        message=message_content,
        timestamp=int(message_id),
        host='localhost'
    )

    # Create a GELF client to send the message
    gelf_client = GELFClient('localhost', 12201)

    # Send the GELF message
    gelf_client.send(gelf_message)