Skip to content

Commit

Permalink
Update to 1.0.0 (#7)
Browse files Browse the repository at this point in the history
* Support compress to tcp handler
* Added support GELF additional field as a dict
* Example with additional field
* Bump version
* Update docs
  • Loading branch information
malinkinsa authored Apr 18, 2022
1 parent c203224 commit 95744ac
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ Async python logging handlers that send messages in the Graylog Extended Log For
- [GELF TCP](#gelf-tcp)
- [GELF HTTP](#gelf-http)
- [GELF UDP](#gelf-udp)
- [Additional field](#additional-field)
- [Available params](#available-params)

## List of ready to run GELF handlers
- TCP (with and without TLS);
- HTTP (with and without TLS);
- UDP
- UDP;

## Get AsyncGELF
```python
Expand Down Expand Up @@ -73,6 +74,33 @@ async def main(message):
asyncio.run(main(message))
```

### Additional field

Expect dict with next moments:
- All keys must start with underscore (_) prefix;
- ```_id``` can't be additional field;
- Allowed characters in field names are any word character (letter, number, underscore), dashes and dots

```python
import asyncio
import asyncgelf

async def main(message):
additional_field = {
'_key_1': 'value_1',
'_key_2': 'value_2',
}

handler = asyncgelf.GelfTcp(
host='127.0.0.1',
additional_field=additional_field
)

await handler.tcp_handler(message)

asyncio.run(main(message))
```

### Available params
- ```host``` Requaried | Graylog server address;
- ```port``` Optional | Graylog input port (default: 12201);
Expand All @@ -81,4 +109,5 @@ 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)
- ```debug``` Optional | Additional information in error log (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)
41 changes: 38 additions & 3 deletions asyncgelf/asyncgelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import json
import math
import os
import re
import socket
import ssl
import struct
import zlib

from typing import Optional
from typing import Optional, Dict


class GelfBase(object):
Expand All @@ -20,8 +21,9 @@ def __init__(
level: Optional[str] = 1,
scheme: Optional[str] = 'http',
tls: Optional = None,
compress: Optional = False,
debug: Optional = False
compress: Optional[bool] = False,
debug: Optional[bool] = False,
additional_field: Optional[Dict] = None
):
"""
:param host: graylog server address
Expand All @@ -32,6 +34,7 @@ def __init__(
:param tls: Path to custom (self-signed) certificate in pem format
: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
"""

self.host = host
Expand All @@ -43,6 +46,30 @@ def __init__(
self.compress = compress
self.tls = tls
self.debug = debug
self.additional_field = additional_field

"""
Gelf compliance checks:
- All keys must start with underscore (_) prefix;
- _id can't be additional field;
- Allowed characters in field names are any word character (letter, number, underscore), dashes and dots.
"""
if self.additional_field:
prefix_pattern = re.compile(r'^_.*$')
character_pattern = re.compile(r'^[\w\.\-]*$')
id_pattern = re.compile(r'^_id$')

for k, v in self.additional_field.items():
if prefix_pattern.search(k) is None:
exit('Error. Allowed only names started with underscore (_)')

if character_pattern.search(k) is None:
exit('Error. One or more additional fields contain unsupported character. '
'Allowed characters in field names are any word character (letter, number, underscore), '
'dashes and dots.')

if id_pattern.search(k):
exit("Error. Don't allowed to send _id as additional field.")

def make(self, message):
"""
Expand All @@ -56,6 +83,11 @@ def make(self, message):
'short_message': json.dumps(message),
'level': self.level,
}

if self.additional_field:
for k, v in self.additional_field.items():
gelf_message.update({k: v})

return gelf_message


Expand All @@ -70,6 +102,9 @@ async def tcp_handler(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)

if self.tls:
ssl_contex = ssl.create_default_context()
ssl_contex.load_verify_locations(cafile=self.tls)
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='0.2.0',
version='1.0.0',
author='Sergey Malinkin',
author_email='[email protected]',
url='https://github.com/malinkinsa/asyncgelf',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/0.2.0.tar.gz',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/1.0.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 95744ac

Please sign in to comment.