From 7be40e61be8e7e9195f1447cdbb55cabe93c1b8b Mon Sep 17 00:00:00 2001 From: amccauley Date: Wed, 16 Sep 2020 17:14:11 -0400 Subject: [PATCH 1/2] Added useSSL option to GELFHTTPHandler class --- graypy/handler.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/graypy/handler.py b/graypy/handler.py index 48e77e90e..a686373bb 100644 --- a/graypy/handler.py +++ b/graypy/handler.py @@ -723,7 +723,7 @@ class GELFHTTPHandler(BaseGELFHandler): """GELF HTTP handler""" def __init__( - self, host, port=12203, compress=True, path="/gelf", timeout=5, **kwargs + self, host, port=12203, compress=True, path="/gelf", useSSL=False, timeout=5, **kwargs ): """Initialize the GELFHTTPHandler @@ -750,6 +750,7 @@ def __init__( self.host = host self.port = port self.path = path + self.useSSL = useSSL self.timeout = timeout self.headers = {} @@ -765,7 +766,10 @@ def emit(self, record): :type record: logging.LogRecord """ pickle = self.makePickle(record) - connection = httplib.HTTPConnection( + + connectionClass = httplib.HTTPSConnection if self.useSSL else httplib.HTTPConnection + connection = connectionClass( host=self.host, port=self.port, timeout=self.timeout ) connection.request("POST", self.path, pickle, self.headers) + From e23d08ae42ac69ccf0c0ccaf55a21b6197ba213a Mon Sep 17 00:00:00 2001 From: amccauley Date: Thu, 17 Sep 2020 14:12:10 -0400 Subject: [PATCH 2/2] Added basic exception handling to HTTP request --- graypy/handler.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/graypy/handler.py b/graypy/handler.py index a686373bb..2467f3ecc 100644 --- a/graypy/handler.py +++ b/graypy/handler.py @@ -765,11 +765,13 @@ def emit(self, record): and emit to Graylog via a HTTP POST request. :type record: logging.LogRecord """ - pickle = self.makePickle(record) - - connectionClass = httplib.HTTPSConnection if self.useSSL else httplib.HTTPConnection - connection = connectionClass( - host=self.host, port=self.port, timeout=self.timeout - ) - connection.request("POST", self.path, pickle, self.headers) + try: + pickle = self.makePickle(record) + connectionClass = httplib.HTTPSConnection if self.useSSL else httplib.HTTPConnection + connection = connectionClass( + host=self.host, port=self.port, timeout=self.timeout + ) + connection.request("POST", self.path, pickle, self.headers) + except Exception as e: + print("Failed to send GELF via HTTP: ", e)