Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyZmeem committed Feb 20, 2019
0 parents commit 2224795
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[Makefile]
indent_style = tab

[*.py]
charset = utf-8
indent_style = space
indent_size = 4
122 changes: 122 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# JetBrains IDE
.idea

# MacOS
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Andrey Maslov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
python-logging-loki
===================

[![PyPI version](https://img.shields.io/pypi/v/python-logging-loki.svg)](https://pypi.org/project/python-logging-loki/)
[![Python version](https://img.shields.io/badge/python-3.5%20%7C%203.6%20%7C%203.7-blue.svg)](https://www.python.org/)
[![License](https://img.shields.io/pypi/l/python-logging-loki.svg)](https://opensource.org/licenses/MIT)

Python logging handler for Loki.
https://grafana.com/loki

Installation
============
```bash
pip install python-logging-loki
```

Usage
=====

```python
import logging
import logging_loki


handler = logging_loki.LokiHandler(
url="https://my-loki-instnace/api/prom/push",
tags={"application": "my-app"},
auth=("username", "password"),
)

logger = logging.getLogger("my-logger")
logger.addHandler(handler)
logger.error(
"Something happened",
extra={"tags": {"service": "my-service"}},
)
```

Example above will send `Something happened` message along with these labels:
- Default labels from handler
- Message level as `serverity`
- Logger's name as `logger`
- Labels from `tags` item of `extra` dict

The given example is blocking (i.e. each call will wait for the message to be sent).
But you can use the built-in `QueueHandler` and` QueueListener` to send messages in a separate thread.

```python
import logging.handlers
import logging_loki
from queue import Queue


queue = Queue(-1)
handler = logging.handlers.QueueHandler(queue)
handler_loki = logging_loki.LokiHandler(
url="https://my-loki-instnace/api/prom/push",
tags={"application": "my-app"},
auth=("username", "password"),
)
logging.handlers.QueueListener(queue, handler_loki)

logger = logging.getLogger("my-logger")
logger.addHandler(handler)
logger.error(...)
```

Or you can use `LokiQueueHandler` shortcut, which will automatically create listener and handler.

```python
import logging.handlers
import logging_loki
from queue import Queue


handler = logging_loki.LokiQueueHandler(
Queue(-1),
url="https://my-loki-instnace/api/prom/push",
tags={"application": "my-app"},
auth=("username", "password"),
)

logger = logging.getLogger("my-logger")
logger.addHandler(handler)
logger.error(...)
```
5 changes: 5 additions & 0 deletions logging_loki/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .handlers import LokiHandler, LokiQueueHandler

__all__ = ["LokiHandler", "LokiQueueHandler"]
__version__ = "0.1.0"
name = "logging_loki"
96 changes: 96 additions & 0 deletions logging_loki/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import logging
from logging.handlers import QueueHandler, QueueListener
from queue import Queue
from typing import Optional, Tuple, Dict, Any

import requests
import rfc3339

BasicAuth = Optional[Tuple[str, str]]


class LokiQueueHandler(QueueHandler):
"""
This handler automatically creates listener and `LokiHandler` to handle logs queue.
"""

def __init__(self, queue: Queue, url: str, tags: Optional[dict] = None, auth: BasicAuth = None):
super().__init__(queue)
self.handler = LokiHandler(url, tags, auth)
self.listener = QueueListener(self.queue, self.handler)
self.listener.start()


class LokiHandler(logging.Handler):
"""
This handler sends log records to Loki via HTTP API.
https://github.com/grafana/loki/blob/master/docs/api.md
"""

level_tag: str = "severity"
logger_tag: str = "logger"

def __init__(self, url: str, tags: Optional[dict] = None, auth: BasicAuth = None):
super().__init__()

# Tags that will be added to all records handled by this handler.
self.tags = tags or {}

# Loki HTTP API endpoint (e.g `http://127.0.0.1/api/prom/push`)
self.url = url

# Optional tuple with username and password for basic authentication
self.auth = auth

self._session: requests.Session = None

@property
def session(self) -> requests.Session:
if self._session is None:
self._session = requests.Session()
self._session.auth = self.auth or None
return self._session

def handleError(self, record):
super().handleError(record)
if self._session is not None:
self._session.close()
self._session = None

def emit(self, record: logging.LogRecord):
"""
Send log record to Loki.
"""
# noinspection PyBroadException
try:
labels = self.build_labels(record)
ts = rfc3339.format(record.created)
line = self.format(record)
payload = {"streams": [{"labels": labels, "entries": [{"ts": ts, "line": line}]}]}
resp = self.session.post(self.url, json=payload)
if resp.status_code != 204:
raise ValueError("Unexpected Loki API response status code: %s" % resp.status_code)
except Exception:
self.handleError(record)

def build_labels(self, record: logging.LogRecord) -> str:
"""
Return Loki labels string.
"""
tags = self.build_tags(record)
labels = ",".join(['%s="%s"' % (k, str(v).replace('"', '\\"')) for k, v in tags.items()])
return "{%s}" % labels

def build_tags(self, record: logging.LogRecord) -> Dict[str, Any]:
"""
Return tags that must be send to Loki with a log record.
"""
tags = self.tags.copy()
tags[self.level_tag] = record.levelname.lower()
tags[self.logger_tag] = record.name

extra_tags = getattr(record, "tags", {})
if isinstance(extra_tags, dict):
tags.update(extra_tags)

return tags
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tool.black]
line-length = 120
py36 = true
exclude = '''
/(
\.git
)/
'''
32 changes: 32 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="python-logging-loki",
version="0.1.0",
description="Python logging handler for Grafana Loki.",
long_description=long_description,
long_description_content_type="text/markdown",
license="MIT",
author="Andrey Maslov",
author_email="greyzmeem@gmail.com",
url="https://github.com/greyzmeem/python-logging-loki",
packages=setuptools.find_packages(),
python_requires=">=3.5",
install_requires=["rfc3339", "requests"],
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: System :: Logging",
"Topic :: Internet :: WWW/HTTP",
],
)

0 comments on commit 2224795

Please sign in to comment.