-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# glogger | ||
|
||
A python library for logging loguru messages straight to a gelf instance, for example Sematext via HTTP | ||
|
||
## Install glogger | ||
|
||
Simply install it via pip: `pip install git+https://github.com/fino-digital/glogger.git` | ||
|
||
## Getting started | ||
Make sure the following environment variables have been set: `ENVIRONMENT`, `LOGGING_URL` and `LOG_FACILITY` | ||
|
||
```python | ||
from glogger import logger | ||
|
||
logger.info('go') | ||
logger.bind(custom='field').info('yay') | ||
``` | ||
|
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,49 @@ | ||
import json | ||
import os | ||
import threading | ||
|
||
import requests | ||
from loguru import logger | ||
|
||
LOGGING_URL = os.getenv("LOGGING_URL", "") + "glogger" | ||
s = requests.Session() | ||
|
||
|
||
def send_as_gelf(record): | ||
k = json.loads(record) | ||
data = { | ||
"message": k["record"]["message"], | ||
"severity": k["record"]["level"]["name"], | ||
"level": k["record"]["level"]["no"], | ||
"thread": k["record"]["thread"]["name"], | ||
"timestamp": k["record"]["time"]["timestamp"], | ||
"source_name": k["record"]["file"]["path"] + ":" + k["record"]["module"], | ||
"source_line_number": k["record"]["line"], | ||
"source_method_name": k["record"]["function"], | ||
} | ||
for k, v in k["record"]["extra"].items(): | ||
data[k] = v | ||
|
||
s.post(LOGGING_URL, json=data) | ||
|
||
|
||
class Gelf: | ||
def write(self, record): | ||
if "LOGGING_URL" not in os.environ: | ||
return | ||
t = threading.Thread(target=send_as_gelf, args=[record]) | ||
t.daemon = True | ||
t.start() | ||
|
||
|
||
logger.add(sink=Gelf(), serialize=True) | ||
|
||
if "ENVIRONMENT" in os.environ: | ||
logger = logger.bind(environment=os.getenv("ENVIRONMENT")) | ||
else: | ||
logger.warning("ENVIRONMENT not configured") | ||
|
||
if "LOG_FACILITY" in os.environ: | ||
logger = logger.bind(facility=os.getenv("LOG_FACILITY")) | ||
else: | ||
logger.warning("LOG_FACILITY not configured") |
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,2 @@ | ||
[metadata] | ||
description-file = README.md |
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,23 @@ | ||
import setuptools | ||
|
||
with open("README.md", "r") as fh: | ||
long_description = fh.read() | ||
setuptools.setup( | ||
name='glogger', | ||
version='1.0.0', | ||
description="A python library for logging loguru messages straight to a gelf instance, for example Sematext via HTTP", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/fino-digital/glogger", | ||
packages=setuptools.find_packages(), | ||
classifiers=[ | ||
"Programming Language :: Python :: 3.7", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
], | ||
python_requires='>=3.6', | ||
install_requires=[ | ||
"requests", | ||
"loguru" | ||
] | ||
) |