Skip to content

Commit

Permalink
Added key for resolving bug with UDP handler (#8)
Browse files Browse the repository at this point in the history
* Added key for resolving bug with UDP handler
* Bump version
* Update Readme
  • Loading branch information
malinkinsa authored Apr 24, 2022
1 parent 95744ac commit 6a90e69
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ asyncio.run(main(message))
```

### Available params
- ```host``` Requaried | Graylog server address;
- ```host``` Required | Graylog server address;
- ```port``` Optional | Graylog input port (default: 12201);
- ```gelf_version``` Optional | GELF spec version (default: 1.1)
- ```level``` Optional | The level equal to the standard syslog levels (default: 1);
- ```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)
- ```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)
- ```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 existence DNS as parameter, and if dns is found, than 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)
20 changes: 19 additions & 1 deletion asyncgelf/asyncgelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(
tls: Optional = None,
compress: Optional[bool] = False,
debug: Optional[bool] = False,
additional_field: Optional[Dict] = None
additional_field: Optional[Dict] = None,
dns_resolve: Optional[bool] = False,
):
"""
:param host: graylog server address
Expand All @@ -35,6 +36,9 @@ def __init__(
:param compress: compress message before sending it to the server or not
: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
found, than 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: https://github.com/python/cpython/issues/91305
"""

self.host = host
Expand All @@ -47,6 +51,7 @@ def __init__(
self.tls = tls
self.debug = debug
self.additional_field = additional_field
self.dns_resolve = dns_resolve

"""
Gelf compliance checks:
Expand All @@ -71,6 +76,19 @@ def __init__(
if id_pattern.search(k):
exit("Error. Don't allowed to send _id as additional field.")

"""
If dns_resolve = True.
Checking the self.host variable for presence DNS name. If found - dns recorde be resolved to ip and override in
self.host
"""
if self.dns_resolve:
hostname_pattern = re.compile(
r'^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$'
)

if hostname_pattern.search(self.host):
self.host = socket.gethostbyname(self.host)

def make(self, message):
"""
Transforms each message into GELF
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.0',
version='1.0.1',
author='Sergey Malinkin',
author_email='[email protected]',
url='https://github.com/malinkinsa/asyncgelf',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/1.0.0.tar.gz',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/1.0.1.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 6a90e69

Please sign in to comment.