loki-logger-handler


Nameloki-logger-handler JSON
Version 0.1.4 PyPI version JSON
download
home_pagehttps://github.com/xente/loki-logger-handler
SummaryHandler designed for transmitting logs to Grafana Loki in JSON format.
upload_time2024-03-27 09:23:05
maintainerNone
docs_urlNone
authorXente
requires_python>=3.6
licenseMIT
keywords loki loguru logger handler
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # loki_logger_handler

A logging handler that sends log messages to 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
* Publish in batch of Streams
* Publis logs compressed

## Args

* url (str): The URL of the Loki server.
* labels (dict): A dictionary of labels to attach to each log message.
* labelKeys (dict, optional): A dictionary of keys to extract from each log message and use as labels. Defaults to None.
* timeout (int, optional): The time in seconds to wait before flushing the buffer. Defaults to 10.
* compressed (bool, optional): Whether to compress the log messages before sending them to Loki. Defaults to True.
* defaultFormatter (logging.Formatter, optional): The formatter to use for log messages. Defaults to LoggerFormatter().

## Formatters
* LoggerFormatter: Formater for default python logging implementation
* LoguruFormatter: Formater for Loguru python library

## How to use 

### Logger
```python
from loki_logger_handler.loki_logger_handler import LokiLoggerHandler,
import logging
import os 

# Set up logging
logger = logging.getLogger("custom_logger")
logger.setLevel(logging.DEBUG)

# Create an instance of the custom handler
custom_handler = LokiLoggerHandler(
    url=os.environ["LOKI_URL"],
    labels={"application": "Test", "envornment": "Develop"},
    labelKeys={},
    timeout=10,
)
# Create an instance of the custom handler

logger.addHandler(custom_handler)
logger.debug("Debug message", extra={'custom_field': 'custom_value'})


```


### Loguru

```python
from loki_logger_handler.loki_logger_handler import LokiLoggerHandler, LoguruFormatter
from loguru import logger
import os 

os.environ["LOKI_URL"]="https://USER:PASSWORD@logs-prod-eu-west-0.grafana.net/loki/api/v1/push"

custom_handler = LokiLoggerHandler(
    url=os.environ["LOKI_URL"],
    labels={"application": "Test", "envornment": "Develop"},
    labelKeys={},
    timeout=10,
    defaultFormatter=LoguruFormatter(),
)
logger.configure(handlers=[{"sink": custom_handler, "serialize": True}])

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": "Response code  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/loki-logger-handler",
    "name": "loki-logger-handler",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "loki, loguru, logger, handler",
    "author": "Xente",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/3c/c1/8d1cbdc34b7bafdee3c61c27e9363f40d7fbf71ac91be8a7f3b59c5f5198/loki-logger-handler-0.1.4.tar.gz",
    "platform": null,
    "description": "# loki_logger_handler\n\nA logging handler that sends log messages to 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* Publish in batch of Streams\n* Publis logs compressed\n\n## Args\n\n* url (str): The URL of the Loki server.\n* labels (dict): A dictionary of labels to attach to each log message.\n* labelKeys (dict, optional): A dictionary of keys to extract from each log message and use as labels. Defaults to None.\n* timeout (int, optional): The time in seconds to wait before flushing the buffer. Defaults to 10.\n* compressed (bool, optional): Whether to compress the log messages before sending them to Loki. Defaults to True.\n* defaultFormatter (logging.Formatter, optional): The formatter to use for log messages. Defaults to LoggerFormatter().\n\n## Formatters\n* LoggerFormatter: Formater for default python logging implementation\n* LoguruFormatter: Formater for Loguru python library\n\n## How to use \n\n### Logger\n```python\nfrom loki_logger_handler.loki_logger_handler import LokiLoggerHandler,\nimport logging\nimport os \n\n# Set up logging\nlogger = logging.getLogger(\"custom_logger\")\nlogger.setLevel(logging.DEBUG)\n\n# Create an instance of the custom handler\ncustom_handler = LokiLoggerHandler(\n    url=os.environ[\"LOKI_URL\"],\n    labels={\"application\": \"Test\", \"envornment\": \"Develop\"},\n    labelKeys={},\n    timeout=10,\n)\n# Create an instance of the custom handler\n\nlogger.addHandler(custom_handler)\nlogger.debug(\"Debug message\", extra={'custom_field': 'custom_value'})\n\n\n```\n\n\n### Loguru\n\n```python\nfrom loki_logger_handler.loki_logger_handler import LokiLoggerHandler, LoguruFormatter\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\ncustom_handler = LokiLoggerHandler(\n    url=os.environ[\"LOKI_URL\"],\n    labels={\"application\": \"Test\", \"envornment\": \"Develop\"},\n    labelKeys={},\n    timeout=10,\n    defaultFormatter=LoguruFormatter(),\n)\nlogger.configure(handlers=[{\"sink\": custom_handler, \"serialize\": True}])\n\nlogger.info(\n    \"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\": \"Response code  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": "MIT",
    "summary": "Handler designed for transmitting logs to Grafana Loki in JSON format.",
    "version": "0.1.4",
    "project_urls": {
        "Homepage": "https://github.com/xente/loki-logger-handler"
    },
    "split_keywords": [
        "loki",
        " loguru",
        " logger",
        " handler"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "41992e99f2e4778ee89ecbdd9a9f3ef69e6723d1fdb23bbcf9bbab0a77214ca5",
                "md5": "cd263a1ffe4c82b3a6df64f32ae9a295",
                "sha256": "89ca9439dea41929e6cc493608c832277640affbda14cceea848208f534db6c5"
            },
            "downloads": -1,
            "filename": "loki_logger_handler-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cd263a1ffe4c82b3a6df64f32ae9a295",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 9546,
            "upload_time": "2024-03-27T09:23:04",
            "upload_time_iso_8601": "2024-03-27T09:23:04.334133Z",
            "url": "https://files.pythonhosted.org/packages/41/99/2e99f2e4778ee89ecbdd9a9f3ef69e6723d1fdb23bbcf9bbab0a77214ca5/loki_logger_handler-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3cc18d1cbdc34b7bafdee3c61c27e9363f40d7fbf71ac91be8a7f3b59c5f5198",
                "md5": "a3baeb934930078925302264e742eef1",
                "sha256": "26411f141959d843cd833a70ff378eb4fa2e7ab31670815db5ae9f62e57f6140"
            },
            "downloads": -1,
            "filename": "loki-logger-handler-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "a3baeb934930078925302264e742eef1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 7331,
            "upload_time": "2024-03-27T09:23:05",
            "upload_time_iso_8601": "2024-03-27T09:23:05.386353Z",
            "url": "https://files.pythonhosted.org/packages/3c/c1/8d1cbdc34b7bafdee3c61c27e9363f40d7fbf71ac91be8a7f3b59c5f5198/loki-logger-handler-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-27 09:23:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "xente",
    "github_project": "loki-logger-handler",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "loki-logger-handler"
}
        
Elapsed time: 0.21476s