Async python logging handlers that send messages in the Graylog Extended Log Format (GELF).
- TCP (with and without TLS);
- HTTP (with and without TLS);
- UDP;
pip install asyncgelfimport asyncio
import asyncgelf
async def main(message):
handler = asyncgelf.GelfTcp(
host='127.0.0.1',
)
await handler.tcp_handler(message)
asyncio.run(main(message))import asyncio
import asyncgelf
from datetime import datetime
async def main(message):
handler = asyncgelf.GelfTcp(
host='127.0.0.1',
)
event_time = "2024-02-06 08:25:56.789"
timeformat = "%Y-%m-%d %H:%M:%S.%f"
await handler.tcp_handler(message, datetime.strptime(event_time, timeformat).timestamp())
asyncio.run(main(message))import asyncio
import asyncgelf
async def main(message):
handler = asyncgelf.GelfHttp(
host='127.0.0.1',
)
await handler.http_handler(message)
asyncio.run(main(message))import asyncio
import asyncgelf
async def main(message):
handler = asyncgelf.GelfUdp(
host='127.0.0.1',
)
await handler.udp_handler(message)
asyncio.run(main(message))Expect Dict with the following moments:
- All keys must start with underscore (_) prefix;
_idcan't be an additional field;- Allowed characters in field names are any word character (letter, number, underscore), dashes and dots
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))hostRequired | Graylog server address;portOptional | Graylog input port (default: 12201);gelf_versionOptional | GELF spec version (default: 1.1)levelOptional | The level equals to the standard syslog levels (default: 1);schemeOptional | HTTP Scheme for GELF HTTP input only (default: http);tlsOptional | Path to custom (self-signed) certificate in pem format (default: None)compressOptional | Compress message before sending it to the server or not (default: False)compress_levelOptional | Set compression level: available from 1 (BEST_SPEED) to 9 (BEST_COMPRESSION) (default: 1)debugOptional | Additional information in error log (default: False)additional_fieldOptional | Dictionary with additional fields which will be added to every gelf message (default: None)dns_resolveOptional | 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 (default: False)