python-logging-loki


Namepython-logging-loki JSON
Version 0.3.1 PyPI version JSON
download
home_pagehttps://github.com/greyzmeem/python-logging-loki
SummaryPython logging handler for Grafana Loki.
upload_time2019-11-28 22:34:38
maintainer
docs_urlNone
authorAndrey Maslov
requires_python>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            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.6%20%7C%203.7%20%7C%203.8-blue.svg)](https://www.python.org/)
[![License](https://img.shields.io/pypi/l/python-logging-loki.svg)](https://opensource.org/licenses/MIT)
[![Build Status](https://travis-ci.org/GreyZmeem/python-logging-loki.svg?branch=master)](https://travis-ci.org/GreyZmeem/python-logging-loki)

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-instance/loki/api/v1/push", 
    tags={"application": "my-app"},
    auth=("username", "password"),
    version="1",
)

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 multiprocessing import Queue


queue = Queue(-1)
handler = logging.handlers.QueueHandler(queue)
handler_loki = logging_loki.LokiHandler(
    url="https://my-loki-instance/loki/api/v1/push", 
    tags={"application": "my-app"},
    auth=("username", "password"),
    version="1",
)
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 multiprocessing import Queue


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

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



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/greyzmeem/python-logging-loki",
    "name": "python-logging-loki",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Andrey Maslov",
    "author_email": "greyzmeem@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/39/e2/b1ca91524530e5c7d73e70c4a2df952e5b7f1519977c7c51b3966b0332a8/python-logging-loki-0.3.1.tar.gz",
    "platform": "",
    "description": "python-logging-loki\n===================\n\n[![PyPI version](https://img.shields.io/pypi/v/python-logging-loki.svg)](https://pypi.org/project/python-logging-loki/)\n[![Python version](https://img.shields.io/badge/python-3.6%20%7C%203.7%20%7C%203.8-blue.svg)](https://www.python.org/)\n[![License](https://img.shields.io/pypi/l/python-logging-loki.svg)](https://opensource.org/licenses/MIT)\n[![Build Status](https://travis-ci.org/GreyZmeem/python-logging-loki.svg?branch=master)](https://travis-ci.org/GreyZmeem/python-logging-loki)\n\nPython logging handler for Loki.  \nhttps://grafana.com/loki\n\nInstallation\n============\n```bash\npip install python-logging-loki\n```\n\nUsage\n=====\n\n```python\nimport logging\nimport logging_loki\n\n\nhandler = logging_loki.LokiHandler(\n    url=\"https://my-loki-instance/loki/api/v1/push\", \n    tags={\"application\": \"my-app\"},\n    auth=(\"username\", \"password\"),\n    version=\"1\",\n)\n\nlogger = logging.getLogger(\"my-logger\")\nlogger.addHandler(handler)\nlogger.error(\n    \"Something happened\", \n    extra={\"tags\": {\"service\": \"my-service\"}},\n)\n```\n\nExample above will send `Something happened` message along with these labels:\n- Default labels from handler\n- Message level as `serverity`\n- Logger's name as `logger` \n- Labels from `tags` item of `extra` dict\n\nThe given example is blocking (i.e. each call will wait for the message to be sent).  \nBut you can use the built-in `QueueHandler` and` QueueListener` to send messages in a separate thread.  \n\n```python\nimport logging.handlers\nimport logging_loki\nfrom multiprocessing import Queue\n\n\nqueue = Queue(-1)\nhandler = logging.handlers.QueueHandler(queue)\nhandler_loki = logging_loki.LokiHandler(\n    url=\"https://my-loki-instance/loki/api/v1/push\", \n    tags={\"application\": \"my-app\"},\n    auth=(\"username\", \"password\"),\n    version=\"1\",\n)\nlogging.handlers.QueueListener(queue, handler_loki)\n\nlogger = logging.getLogger(\"my-logger\")\nlogger.addHandler(handler)\nlogger.error(...)\n```\n\nOr you can use `LokiQueueHandler` shortcut, which will automatically create listener and handler.\n\n```python\nimport logging.handlers\nimport logging_loki\nfrom multiprocessing import Queue\n\n\nhandler = logging_loki.LokiQueueHandler(\n    Queue(-1),\n    url=\"https://my-loki-instance/loki/api/v1/push\", \n    tags={\"application\": \"my-app\"},\n    auth=(\"username\", \"password\"),\n    version=\"1\",\n)\n\nlogger = logging.getLogger(\"my-logger\")\nlogger.addHandler(handler)\nlogger.error(...)\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python logging handler for Grafana Loki.",
    "version": "0.3.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e9ca1f5660fbda815ed04839d657cab77605337e32c224c03c780cd383c7bcd6",
                "md5": "d128186e7a931d5142d2b343ad8681d7",
                "sha256": "8a9131db037fbea3d390089c4c32dbe7ed233944905079615a9fb6f669b0f4e6"
            },
            "downloads": -1,
            "filename": "python_logging_loki-0.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d128186e7a931d5142d2b343ad8681d7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 7004,
            "upload_time": "2019-11-28T22:34:36",
            "upload_time_iso_8601": "2019-11-28T22:34:36.635229Z",
            "url": "https://files.pythonhosted.org/packages/e9/ca/1f5660fbda815ed04839d657cab77605337e32c224c03c780cd383c7bcd6/python_logging_loki-0.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "39e2b1ca91524530e5c7d73e70c4a2df952e5b7f1519977c7c51b3966b0332a8",
                "md5": "9fd79fa39b6dd6ec5816e6dc63c21b37",
                "sha256": "b83610c8a3adc99fbab072493b91dfb25ced69be4874fefe3ab457b391adbf60"
            },
            "downloads": -1,
            "filename": "python-logging-loki-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "9fd79fa39b6dd6ec5816e6dc63c21b37",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 5214,
            "upload_time": "2019-11-28T22:34:38",
            "upload_time_iso_8601": "2019-11-28T22:34:38.273654Z",
            "url": "https://files.pythonhosted.org/packages/39/e2/b1ca91524530e5c7d73e70c4a2df952e5b7f1519977c7c51b3966b0332a8/python-logging-loki-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2019-11-28 22:34:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "greyzmeem",
    "github_project": "python-logging-loki",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "python-logging-loki"
}
        
Elapsed time: 6.57367s