# loguru-loki-handlers
Handler created for Loguru that sends logs to Grafana Loki in JSON format
## Features
* Logs pushed in JSON format
* Custom labels definition
* Allows defining loguru and logger extra keys as labels
* Logger extra keys added automatically as keys into pushed JSON
## How to use
```python
from loguru_loki_handler import loki_handler
from loguru import logger
import os
os.environ["LOKI_URL"]="https://USER:PASSWORD@logs-prod-eu-west-0.grafana.net/loki/api/v1/push"
logger.configure(handlers=[{"sink": loki_handler(os.environ["LOKI_URL"],{"application":"Test", "envornment":"Develop"}), "serialize": True}])
logger.info("Starting service")
logger.info("Response code {code} HTTP/1.1 GET {url}", code=200, url="https://loki_handler.io")
```
## Loki messages samples
### Without extra
```json
{
"message": "Starting service",
"timestamp": 1681638266.542849,
"process": 48906,
"thread": 140704422327936,
"function": "run",
"module": "test",
"name": "__main__"
}
```
### With extra
```json
{
"message": "Request return 200 HTTP/1.1 GET https://loki_handler.io",
"timestamp": 1681638225.877143,
"process": 48870,
"thread": 140704422327936,
"function": "run",
"module": "test",
"name": "__main__",
"code": 200,
"url": "https://loki_handler.io"
}
```
### Exceptions
```json
{
"message": "name 'plan' is not defined",
"timestamp": 1681638284.358464,
"process": 48906,
"thread": 140704422327936,
"function": "run",
"module": "test",
"name": "__main__",
"file": "test.py",
"path": "/test.py",
"line": 39
}
```
## Loki Query Sample
Loki query sample :
```
{envornment="Develop"} |= `` | json
```
Filter by level:
```
{envornment="Develop", level="INFO"} |= `` | json
```
Filter by extra:
```
{envornment="Develop", level="INFO"} |= `` | json | code=`200`
```
## License
The MIT License
Raw data
{
"_id": null,
"home_page": "https://github.com/xente/loguru-loki-handlers",
"name": "loguru-loki-handler",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "loki,loguru",
"author": "Xente",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/55/cb/eb08bff76a47f6c208b16eb363055a1cbb7269f19fce72344789de68e149/loguru-loki-handler-0.1.1.tar.gz",
"platform": null,
"description": "# loguru-loki-handlers\n\nHandler created for Loguru that sends logs to Grafana Loki in JSON format\n\n## Features\n\n* Logs pushed in JSON format\n* Custom labels definition\n* Allows defining loguru and logger extra keys as labels\n* Logger extra keys added automatically as keys into pushed JSON\n\n## How to use\n\n```python\nfrom loguru_loki_handler import loki_handler\nfrom loguru import logger\nimport os \n\nos.environ[\"LOKI_URL\"]=\"https://USER:PASSWORD@logs-prod-eu-west-0.grafana.net/loki/api/v1/push\"\n\nlogger.configure(handlers=[{\"sink\": loki_handler(os.environ[\"LOKI_URL\"],{\"application\":\"Test\", \"envornment\":\"Develop\"}), \"serialize\": True}])\nlogger.info(\"Starting service\")\nlogger.info(\"Response code {code} HTTP/1.1 GET {url}\", code=200, url=\"https://loki_handler.io\")\n\n```\n\n## Loki messages samples\n\n### Without extra\n\n```json\n{\n \"message\": \"Starting service\",\n \"timestamp\": 1681638266.542849,\n \"process\": 48906,\n \"thread\": 140704422327936,\n \"function\": \"run\",\n \"module\": \"test\",\n \"name\": \"__main__\"\n}\n\n```\n\n### With extra\n\n```json\n{\n \"message\": \"Request return 200 HTTP/1.1 GET https://loki_handler.io\",\n \"timestamp\": 1681638225.877143,\n \"process\": 48870,\n \"thread\": 140704422327936,\n \"function\": \"run\",\n \"module\": \"test\",\n \"name\": \"__main__\",\n \"code\": 200,\n \"url\": \"https://loki_handler.io\"\n}\n```\n\n### Exceptions\n\n```json\n{\n \"message\": \"name 'plan' is not defined\",\n \"timestamp\": 1681638284.358464,\n \"process\": 48906,\n \"thread\": 140704422327936,\n \"function\": \"run\",\n \"module\": \"test\",\n \"name\": \"__main__\",\n \"file\": \"test.py\",\n \"path\": \"/test.py\",\n \"line\": 39\n}\n```\n\n## Loki Query Sample\n\nLoki query sample :\n\n ```\n {envornment=\"Develop\"} |= `` | json\n ```\n\nFilter by level:\n\n```\n{envornment=\"Develop\", level=\"INFO\"} |= `` | json\n```\nFilter by extra:\n\n```\n{envornment=\"Develop\", level=\"INFO\"} |= `` | json | code=`200`\n```\n\n## License\nThe MIT License\n",
"bugtrack_url": null,
"license": "",
"summary": "Handler created for Loguru that sends logs to Grafana Loki in JSON format",
"version": "0.1.1",
"project_urls": {
"Homepage": "https://github.com/xente/loguru-loki-handlers"
},
"split_keywords": [
"loki",
"loguru"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f7bed548441098ae8953aeb0dfc619acba35560701fa739ad1becaf4a1e4694e",
"md5": "ee8d1fe4cc47d2cf6f052df645950442",
"sha256": "61313d4e0b97ace663e3e887e798da785c36acf075968f0aac942685113c5a83"
},
"downloads": -1,
"filename": "loguru_loki_handler-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ee8d1fe4cc47d2cf6f052df645950442",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4128,
"upload_time": "2023-10-12T21:03:07",
"upload_time_iso_8601": "2023-10-12T21:03:07.879224Z",
"url": "https://files.pythonhosted.org/packages/f7/be/d548441098ae8953aeb0dfc619acba35560701fa739ad1becaf4a1e4694e/loguru_loki_handler-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "55cbeb08bff76a47f6c208b16eb363055a1cbb7269f19fce72344789de68e149",
"md5": "f7e3173bce13d2f686649822580df1f2",
"sha256": "12ee0be4da98a34d2f31a501a2846994ce70859cbdc001616ad3ba852147eee9"
},
"downloads": -1,
"filename": "loguru-loki-handler-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "f7e3173bce13d2f686649822580df1f2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4223,
"upload_time": "2023-10-12T21:03:08",
"upload_time_iso_8601": "2023-10-12T21:03:08.807105Z",
"url": "https://files.pythonhosted.org/packages/55/cb/eb08bff76a47f6c208b16eb363055a1cbb7269f19fce72344789de68e149/loguru-loki-handler-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-12 21:03:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "xente",
"github_project": "loguru-loki-handlers",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "loguru-loki-handler"
}