You can download this code by clicking the button below.
This code is now available for download.
This function sends a log message to a Graylog server using the GELF protocol, while filtering by a specified field and value.
Technology Stack : graypy, GELFHandler, GelfJsonFormatter
Code Type : Log
Code Difficulty : Intermediate
def graylog_query_filter_by_field(field, value):
from graypy import GELFHandler
from graypy.formatters import GelfJsonFormatter
# Create a GELF formatter
formatter = GelfJsonFormatter()
# Create a GELF handler
handler = GELFHandler('graylog_server_ip', 12201, version=1)
handler.setFormatter(formatter)
# Define the GELF message
gelf_message = {
"version": "1.1",
"host": "localhost",
"short_message": f"Filtering by field: {field} with value: {value}",
"fields": {
field: value
}
}
# Send the GELF message to Graylog
handler.send(gelf_message)