Skip to content

Commit 189bf32

Browse files
authored
Merge pull request #4 from malinkinsa/error_processing_and_tcp_tls
Error processing and tcp tls
2 parents e610d4e + e137445 commit 189bf32

3 files changed

Lines changed: 74 additions & 21 deletions

File tree

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Async python logging handlers that send messages in the Graylog Extended Log For
1414
- [Available params](#available-params)
1515

1616
## List of ready to run GELF handlers
17-
- TCP (without TLS);
18-
- HTTP;
17+
- TCP (with and without TLS);
18+
- HTTP (with and without TLS);
1919

2020
## Get AsyncGELF
2121
```python
@@ -62,5 +62,6 @@ asyncio.run(main(message))
6262
- ```gelf_version``` Optional | GELF spec version (default: 1.1)
6363
- ```level``` Optional | The level equal to the standard syslog levels (default: 1);
6464
- ```scheme``` Optional | HTTP Scheme <i>for GELF HTTP input only</i> (default: http);
65-
- ```tls``` Path to custom (self-signed) certificate in pem format <i>for GELF HTTP input only</i> (default: None)
66-
- ```compress``` Optional | Compress message before sending it to the server or not <i>for GELF HTTP input only</i> (default: false)
65+
- ```tls``` Optional | Path to custom (self-signed) certificate in pem format (default: None)
66+
- ```compress``` Optional | Compress message before sending it to the server or not <i>for GELF HTTP input only</i> (default: False)
67+
- ```debug``` Optional | Additional information in error log (default: False)

asyncgelf/asyncgelf.py

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

75111
class 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))

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
setup(
44
name='asyncgelf',
5-
version='0.1.2',
5+
version='0.1.3',
66
author='Sergey Malinkin',
77
author_email='[email protected]',
88
url='https://github.com/malinkinsa/asyncgelf',
9-
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/0.1.2.tar.gz',
9+
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/0.1.3.tar.gz',
1010
description='Async python logging handlers that send messages in the Graylog Extended Log Format (GELF).',
1111
long_description=open('README.md').read(),
1212
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)