Skip to content

Commit

Permalink
Merge pull request #14 from malinkinsa/1.1.0
Browse files Browse the repository at this point in the history
1.1.0
  • Loading branch information
malinkinsa authored Feb 25, 2023
2 parents f7eec5a + 6fc8938 commit 466746b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# AsyncGELF
[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/malinkinsa/asyncgelf.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/malinkinsa/asyncgelf/context:python)
[![CodeQL](https://github.com/malinkinsa/asyncgelf/actions/workflows/codeql-analysis.yml/badge.svg?branch=master)](https://github.com/malinkinsa/asyncgelf/actions/workflows/codeql-analysis.yml)
![PyPI - Downloads](https://img.shields.io/pypi/dm/asyncgelf)
![PyPI](https://img.shields.io/pypi/v/asyncgelf)
Expand Down Expand Up @@ -112,6 +111,7 @@ asyncio.run(main(message))
- ```scheme``` Optional | HTTP Scheme <i>for GELF HTTP input only</i> (default: http);
- ```tls``` Optional | Path to custom (self-signed) certificate in pem format (default: None)
- ```compress``` Optional | Compress message before sending it to the server or not (default: False)
- ```compress_level``` Optional | Set compression level: available from 1 (BEST_SPEED) to 9 (BEST_COMPRESSION) (default: 1)
- ```debug``` Optional | Additional information in error log (default: False)
- ```additional_field``` Optional | Dictionary with additional fields which will be added to every gelf message (default: None)
- ```dns_resolve``` Optional | Variable host will be checked to existing DNS as parameter and if dns is found, then on initialization will resolve to ip and variable will be updated. By default, UDP handler gets resolved by DNS on every log message. See more: [#91305](https://github.com/python/cpython/issues/91305) (default: False)
72 changes: 35 additions & 37 deletions asyncgelf/asyncgelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(
scheme: Optional[str] = 'http',
tls: Optional = None,
compress: Optional[bool] = False,
compress_level: Optional[int] = 1,
debug: Optional[bool] = False,
additional_field: Optional[Dict] = None,
dns_resolve: Optional[bool] = False,
Expand All @@ -34,6 +35,7 @@ def __init__(
:param scheme: HTTP Scheme for GELF HTTP input only
:param tls: Path to custom (self-signed) certificate in pem format
:param compress: compress message before sending it to the server or not
:param compress_level: set compression level: available from 1 (BEST_SPEED) to 9 (BEST_COMPRESSION)
:param debug: additional information in error log
:param additional_field: dictionary with additional fields which will be added to every gelf message
:param dns_resolve: If enabled - Variable host will be checked to existence DNS as parameter, and if dns is
Expand All @@ -48,6 +50,7 @@ def __init__(
self.level = level
self.scheme = scheme
self.compress = compress
self.compress_level = compress_level
self.tls = tls
self.debug = debug
self.additional_field = additional_field
Expand Down Expand Up @@ -110,59 +113,54 @@ def make(self, message):


class GelfTcp(GelfBase):
async def tcp_handler(self, massage):
async def tcp_handler(self, message):
"""
tcp handler for send logs to Graylog Input with type: gelf tcp
:param massage: input message
:param message: message to send, can be list, str, dict
:return: Exception
"""
gelf_message = GelfBase.make(self, massage)
""" Transforming GELF dictionary into bytes """
bytes_msg = json.dumps(gelf_message).encode('utf-8')

if self.compress:
bytes_msg = zlib.compress(bytes_msg, level=1)
messages = []
"""
Input type checking
"""
if type(message) is not list:
messages.append(message)

if self.tls:
ssl_contex = ssl.create_default_context()
ssl_contex.load_verify_locations(cafile=self.tls)
else:
messages = message

try:
try:
if self.tls:
ssl_contex = ssl.create_default_context()
ssl_contex.load_verify_locations(cafile=self.tls)
stream_reader, stream_writer = await asyncio.open_connection(
self.host,
self.port,
ssl=ssl_contex,
self.host, self.port, ssl=ssl_contex,
)

"""
if you send the message over tcp, it should always be null terminated or the input will reject it
"""
stream_writer.write(bytes_msg + b'\x00')
stream_writer.close()
else:
stream_reader, stream_writer = await asyncio.open_connection(
self.host, self.port,
)

except Exception as e:
if self.debug:
return f"{type(e).__name__} at line {e.__traceback__.tb_lineno} of {__file__}: {e}"
except Exception as e:
if self.debug:
return f"{type(e).__name__} at line {e.__traceback__.tb_lineno} of {__file__}: {e}"

return getattr(e, 'message', repr(e))
return getattr(e, 'message', repr(e))

try:
stream_reader, stream_writer = await asyncio.open_connection(
self.host,
self.port,
)
for message in messages:
gelf_message = GelfBase.make(self, message)
""" Transforming GELF dictionary into bytes """
bytes_msg = json.dumps(gelf_message).encode('utf-8')

if self.compress:
bytes_msg = zlib.compress(bytes_msg, level=self.compress_level)
"""
if you send the message over tcp, it should always be null terminated or the input will reject it
"""
stream_writer.write(bytes_msg + b'\x00')
stream_writer.close()

except Exception as e:
if self.debug:
return f"{type(e).__name__} at line {e.__traceback__.tb_lineno} of {__file__}: {e}"

return getattr(e, 'message', repr(e))
stream_writer.close()


class GelfHttp(GelfBase):
Expand Down Expand Up @@ -227,7 +225,7 @@ async def udp_handler(self, message):
"""
UDP handler for send logs to Graylog Input with type: gelf udp
:param message: input message
:return: Message send error in next case: message size more than 1048576 bytes
:return: error in next case: message size more than 1048576 bytes
"""
"""
Declaring limits for GELF messages
Expand All @@ -241,7 +239,7 @@ async def udp_handler(self, message):
bytes_msg = json.dumps(gelf_message).encode('utf-8')

if self.compress:
bytes_msg = zlib.compress(bytes_msg, level=1)
bytes_msg = zlib.compress(bytes_msg, level=self.compress_level)
"""
Checking the message size.
"""
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

setup(
name='asyncgelf',
version='1.0.3',
version='1.1.0',
author='Sergey Malinkin',
author_email='malinkinsa@yandex.ru',
url='https://github.com/malinkinsa/asyncgelf',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/1.0.2.tar.gz',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/1.1.0.tar.gz',
description='Async python logging handlers that send messages in the Graylog Extended Log Format (GELF).',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 466746b

Please sign in to comment.