@@ -17,6 +17,7 @@ def __init__(
1717 scheme : Optional [str ] = 'http' ,
1818 tls : Optional = None ,
1919 compress : Optional = False ,
20+ debug : Optional = False
2021 ):
2122 """
2223 :param host: graylog server address
@@ -26,6 +27,7 @@ def __init__(
2627 :param scheme: HTTP Scheme for GELF HTTP input only
2728 :param tls: Path to custom (self-signed) certificate in pem format
2829 :param compress: compress message before sending it to the server or not
30+ :param debug: additional information in error log
2931 """
3032
3133 self .host = host
@@ -36,6 +38,7 @@ def __init__(
3638 self .scheme = scheme
3739 self .compress = compress
3840 self .tls = tls
41+ self .debug = debug
3942
4043 def make (self , message ):
4144 """
@@ -63,13 +66,46 @@ async def tcp_handler(self, massage):
6366 """ Transforming GELF dictionary into bytes """
6467 bytes_msg = json .dumps (gelf_message ).encode ('utf-8' )
6568
66- stream_reader , stream_writer = await asyncio .open_connection (
67- self .host , self .port
68- )
69+ if self .tls :
70+ ssl_contex = ssl .create_default_context ()
71+ ssl_contex .load_verify_locations (cafile = self .tls )
72+
73+ try :
74+ stream_reader , stream_writer = await asyncio .open_connection (
75+ self .host ,
76+ self .port ,
77+ ssl = ssl_contex ,
78+ )
79+
80+ """
81+ if you send the message over tcp, it should always be null terminated or the input will reject it
82+ """
83+ stream_writer .write (bytes_msg + b'\x00 ' )
84+ stream_writer .close ()
85+
86+ except Exception as e :
87+ if self .debug :
88+ return f"{ type (e ).__name__ } at line { e .__traceback__ .tb_lineno } of { __file__ } : { e } "
89+
90+ return getattr (e , 'message' , repr (e ))
91+
92+ try :
93+ stream_reader , stream_writer = await asyncio .open_connection (
94+ self .host ,
95+ self .port ,
96+ )
97+
98+ """
99+ if you send the message over tcp, it should always be null terminated or the input will reject it
100+ """
101+ stream_writer .write (bytes_msg + b'\x00 ' )
102+ stream_writer .close ()
69103
70- """ if you send the message over tcp, it should always be null terminated or the input will reject it """
71- stream_writer .write (bytes_msg + b'\x00 ' )
72- stream_writer .close ()
104+ except Exception as e :
105+ if self .debug :
106+ return f"{ type (e ).__name__ } at line { e .__traceback__ .tb_lineno } of { __file__ } : { e } "
107+
108+ return getattr (e , 'message' , repr (e ))
73109
74110
75111class GelfHttp (GelfBase ):
@@ -94,20 +130,36 @@ async def http_handler(self, message):
94130
95131 gelf_endpoint = f'https://{ self .host } :{ self .port } /gelf'
96132
97- async with httpx .AsyncClient (verify = ssl_contex ) as client :
133+ try :
134+ async with httpx .AsyncClient (verify = ssl_contex ) as client :
135+ response = await client .post (
136+ gelf_endpoint ,
137+ headers = header ,
138+ data = json .dumps (gelf_message ),
139+ )
140+
141+ return response .status_code
142+
143+ except Exception as e :
144+ if self .debug :
145+ return f"{ type (e ).__name__ } at line { e .__traceback__ .tb_lineno } of { __file__ } : { e } "
146+
147+ return getattr (e , 'message' , repr (e ))
148+
149+ gelf_endpoint = f'{ self .scheme } ://{ self .host } :{ self .port } /gelf'
150+
151+ try :
152+ async with httpx .AsyncClient () as client :
98153 response = await client .post (
99154 gelf_endpoint ,
100155 headers = header ,
101156 data = json .dumps (gelf_message ),
102157 )
158+
103159 return response .status_code
104160
105- gelf_endpoint = f'{ self .scheme } ://{ self .host } :{ self .port } /gelf'
161+ except Exception as e :
162+ if self .debug :
163+ return f"{ type (e ).__name__ } at line { e .__traceback__ .tb_lineno } of { __file__ } : { e } "
106164
107- async with httpx .AsyncClient () as client :
108- response = await client .post (
109- gelf_endpoint ,
110- headers = header ,
111- data = json .dumps (gelf_message ),
112- )
113- return response .status_code
165+ return getattr (e , 'message' , repr (e ))
0 commit comments