-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from odin-detector/graylog
Add optional logging to a graylog server
- Loading branch information
Showing
7 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,8 @@ dev = | |
tox | ||
pytest-asyncio | ||
pytest-cov | ||
graylog = | ||
pygelf | ||
|
||
[options.packages.find] | ||
where = src | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
import sys | ||
import logging | ||
import getpass | ||
from typing import Optional | ||
|
||
|
||
def add_graylog_handler( | ||
log_server: str, log_level: int = logging.INFO, static_fields: Optional[str] = None | ||
) -> None: | ||
"""Add a graylog handler to the root logger | ||
Args: | ||
log_server: Graylog server endpoint, e.g. "127.0.0.1:12201" | ||
log_level: Log level to filter messages by in handler | ||
static_fields: Comma-separated string of extra fields to include in log message | ||
metadata, e.g. "_field1=value1,_field2=value2" (fields should have a | ||
leading underscore). | ||
""" | ||
try: | ||
from pygelf import GelfUdpHandler | ||
except ImportError: | ||
logging.error("Cannot add graylog handler - pygelf is not installed") | ||
return | ||
|
||
host, port = log_server.split(":") | ||
config = { | ||
"host": host, | ||
"port": int(port), | ||
"debug": True, # Include file, line, module, func, logger_name | ||
# Add custom fields | ||
"include_extra_fields": True, | ||
"_username": getpass.getuser(), | ||
"_process_id": os.getpid(), | ||
"_application_name": os.path.split(sys.argv[0])[1] | ||
} | ||
|
||
if static_fields is not None: | ||
static_fields = dict(entry.split("=") for entry in static_fields.split(",")) | ||
config.update(static_fields) | ||
|
||
handler: logging.Handler = GelfUdpHandler(**config) | ||
handler.setLevel(log_level) | ||
logging.getLogger().addHandler(handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters