Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sn0opy committed Apr 15, 2019
1 parent 0ab606d commit 8c4c3f2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
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')
```

49 changes: 49 additions & 0 deletions glogger/__init__.py
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")
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
23 changes: 23 additions & 0 deletions setup.py
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"
]
)

0 comments on commit 8c4c3f2

Please sign in to comment.