An extension to the builtin logging module that writes to a Graylog server instead of a flat file.
Project description
graylogging
Graylogging is an extension to the built-in logging module that allows logs to be written directly to Graylog via the GELF input rather than feeding from a flat file.
Installation
The package can be installed by one of the following methods:
PIP
pip install graylogging
Pipenv
pipenv install graylogging
General Use
Initialization
Import both the logging and the graylogging module:
import logging
import graylogging
After those have been imported, you can use both the GraylogHandler and GraylogFormatter classes like the other built-in Formatters and Handlers:
logger = logging.getLogger(name)
gh = GraylogHandler(graylog_server, gelf_port, transport="tcp", appname="MyKickassApp")
gh.setFormatter(GraylogFormatter)
logger.addHandler(gh)
logger.setLevel(logging.DEBUG)
The transport options are "tcp", "udp", and "http"; the correct choice will depend on the Graylog input configuration.
This handler could also be used in conjunction with another handler. For example if it's desired to output the logs to console as well, we can accomplish this with the following example:
graylog_server = "graylog.contoso.com"
gelf_port = 12201
appname = "MyKickassApp"
log_level = logging.INFO
logger = logging.getLogger(name)
logger.propogate = False
logger.disable_existing_loggers = True
console_formatter = logging.Formatter(
"%(asctime)s :: %(name)s :: %(levelname)s :: %(message)s"
)
sh = logging.StreamHandler()
sh.setFormatter(console_formatter)
logger.handlers = [ch]
gh = GraylogHandler(graylog_server, gelf_port, transport="http", appname=appname)
gh.setFormatter(GraylogFormatter)
logger.addHandler(gh)
logger.setLevel(log_level)
You should be ready to roll now.
Shipping actual log messages:
One logging has been initialized, graylogging can be used just like the built-in logging module:
logger.debug(message)
logger.info(message)
logger.warning(message)
logger.error(message)
logger.critical(message)
logger.exception(message)
Enhancements
JSON payloads
Graylogging can be fed either a string as normal, or a dict that will be json-encoded before shipping. This will allow a configured pipeline rule to parse the json and extract the key-value pairs into searchable fields. Example pipeline rule:
rule "parse gelf json input"
when
contains(to_string($message.application), "MyKickassApp")
then
let json_fields = parse_json(to_string($message.message));
let json_map = to_map(json_fields);
set_fields(json_map);
end
This will allow you to search by appname and e.g. severity level, function name, as well as any exception info (if called from logger.exception()).
Limitations
- Graylogging requires python3.6+
- GraylogHandler and GraylogFormatter are co-dependent. Don't try to use either without the other.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file graylogging-2.1.0.tar.gz.
File metadata
- Download URL: graylogging-2.1.0.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0572c0aee2659ab31337431989cfa7e847d366719a2b91f39ab75716c1a9446
|
|
| MD5 |
bdd223fa8510810a3a808352c1f66ae8
|
|
| BLAKE2b-256 |
c897c49179c4e1c65b0abafbf24c850a6c3aa90d89cd2d57526048ba2355ec26
|
File details
Details for the file graylogging-2.1.0-py3-none-any.whl.
File metadata
- Download URL: graylogging-2.1.0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6e88d387ad09dee863a615dd27cf83a6695a44f1be2ec5c5bfecfaa2a9edd6f
|
|
| MD5 |
e68d7d40c91c053893ec4e46eaaae928
|
|
| BLAKE2b-256 |
7a97b980778b1c009fe20ef553b6642c5395950097fba6231a20db80e644a0d8
|