Skip to content

Commit

Permalink
Merge pull request #2 from malinkinsa/gelf_https
Browse files Browse the repository at this point in the history
Gelf https
  • Loading branch information
malinkinsa authored Apr 11, 2022
2 parents c1b0d6e + 90ddf09 commit d3f370a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ asyncio.run(main(message))
- ```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``` Path to custom (self-signed) certificate in pem format <i>for GELF HTTP input only</i> (default: None)
- ```compress``` Optional | Compress message before sending it to the server or not <i>for GELF HTTP input only</i> (default: false)
19 changes: 19 additions & 0 deletions asyncgelf/asyncgelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import httpx
import socket
import ssl

from typing import Optional

Expand All @@ -14,6 +15,7 @@ def __init__(
gelf_version: Optional[str] = 1.1,
level: Optional[str] = 1,
scheme: Optional[str] = 'http',
tls: Optional = None,
compress: Optional = False,
):
"""
Expand All @@ -22,6 +24,7 @@ def __init__(
:param gelf_version: GELF spec version
:param level: the level equal to the standard syslog levels
: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
"""

Expand All @@ -32,6 +35,7 @@ def __init__(
self.level = level
self.scheme = scheme
self.compress = compress
self.tls = tls

def make(self, message):
"""
Expand Down Expand Up @@ -83,6 +87,21 @@ async def http_handler(self, message):
header.update({'Content-Encoding': 'gzip,deflate'})

gelf_message = GelfBase.make(self, message)

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

gelf_endpoint = f'https://{self.host}:{self.port}/gelf'

async with httpx.AsyncClient(verify=ssl_contex) as client:
response = await client.post(
gelf_endpoint,
headers=header,
data=json.dumps(gelf_message),
)
return response.status_code

gelf_endpoint = f'{self.scheme}://{self.host}:{self.port}/gelf'

async with httpx.AsyncClient() as client:
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.1.0',
version='0.1.1',
author='Sergey Malinkin',
author_email='malinkinsa@yandex.ru',
url='https://github.com/malinkinsa/asyncgelf',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/0.1.0.tar.gz',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/0.1.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 d3f370a

Please sign in to comment.