loki-logging-handler


Nameloki-logging-handler JSON
Version 0.0.5 PyPI version JSON
download
home_pagehttps://github.com/fuyufjh/loki_logging_handler
SummaryLogging handler to send logs to Grafana Loki
upload_time2024-09-22 14:28:11
maintainerNone
docs_urlNone
authorEric Fu
requires_python>=3.6
licenseMIT
keywords loki logging handler
VCS
bugtrack_url
requirements requests
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # loki_logging_handler

A logging handler that sends log messages to Loki in text or JSON format.

## Features

* Buffer logs in memory and send to Loki in batch
* Logs pushed in text or JSON format
* Logger extra keys added automatically as keys into pushed JSON
* Publish logs compressed

## Args

* `url` (str): The URL of the Loki server.
* `labels` (dict): A dictionary of labels to attach to each log message.
* `auth` (tuple, optional): A tuple of user id and api key. Defaults to None.
* `buffer_timeout` (int, optional): The time in seconds to wait before flushing the buffer. Defaults to 10.
* `buffer_size_threshold` (int, optional): The number of log messages to buffer before flushing. Defaults to 10000.
* `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 `PlainFormatter`.

## Formatters

* `logging.Formatter`: Formater for logging the message text. (default)
* `JsonFormatter`: Formater for logging the message and additional fields as JSON.

## Quick start

```python
from loki_logging_handler.loki_handler import LokiHandler
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 = LokiHandler(
    url=os.environ["LOKI_URL"],
    labels={"application": "Test", "envornment": "Develop"},
    auth=(os.environ["LOKI_USER_ID"], os.environ["LOKI_API_KEY"]),
    # formatter=JsonFormatter(),
    # buffer_timeout=10,
    # buffer_size_threshold=10000,
)
logger.addHandler(custom_handler)

logger.info("sample message with args %s %d", "test", 42)
logger.info("sample message with extra", extra={'custom_field': 'custom_value'})
logger.error("error message")
try:
    raise Exception("test exception")
except Exception as e:
    logger.exception("exception message")
```

## Messages samples

### Default formatter

```
sample message with args test 42
```

with fields:

| Field       | Value   |
|-------------|---------|
| application | Test    |
| envornment  | Develop |
| level       | INFO    |

### Default formatter with exception

```
exception message
Traceback (most recent call last):
  File "/home/eric/loki-logger-handler/example.py", line 32, in <module>
    raise Exception("test exception")
Exception: test exception
```

### Json Formatter

```json
{
  "message": "sample message test 42",
  "timestamp": 1727007836.0348141,
  "thread": 140158402386816,
  "function": "<module>",
  "module": "example",
  "logger": "custom_logger",
  "level": "INFO",
  "exc_text": null
}
```

### Json Formatter with exception

```json
{
  "message": "exception message",
  "timestamp": 1727007836.0350208,
  "thread": 140158402386816,
  "function": "<module>",
  "module": "example",
  "logger": "custom_logger",
  "level": "ERROR",
  "exc_text": null,
  "file": "example.py",
  "path": "/home/eric/loki-logger-handler/example.py",
  "line": 27,
  "stacktrace": "Traceback (most recent call last):\n  File \"/home/eric/loki-logger-handler/example.py\", line 25, in <module>\n    raise Exception(\"test exception\")\nException: test exception\n"
}
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/fuyufjh/loki_logging_handler",
    "name": "loki-logging-handler",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "loki, logging, handler",
    "author": "Eric Fu",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/89/97/9090a9b1309c515a7300649dd04e6e5c4fdc8efb2f754e84e2dc4fff36f9/loki_logging_handler-0.0.5.tar.gz",
    "platform": null,
    "description": "# loki_logging_handler\n\nA logging handler that sends log messages to Loki in text or JSON format.\n\n## Features\n\n* Buffer logs in memory and send to Loki in batch\n* Logs pushed in text or JSON format\n* Logger extra keys added automatically as keys into pushed JSON\n* Publish 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* `auth` (tuple, optional): A tuple of user id and api key. Defaults to None.\n* `buffer_timeout` (int, optional): The time in seconds to wait before flushing the buffer. Defaults to 10.\n* `buffer_size_threshold` (int, optional): The number of log messages to buffer before flushing. Defaults to 10000.\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 `PlainFormatter`.\n\n## Formatters\n\n* `logging.Formatter`: Formater for logging the message text. (default)\n* `JsonFormatter`: Formater for logging the message and additional fields as JSON.\n\n## Quick start\n\n```python\nfrom loki_logging_handler.loki_handler import LokiHandler\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 = LokiHandler(\n    url=os.environ[\"LOKI_URL\"],\n    labels={\"application\": \"Test\", \"envornment\": \"Develop\"},\n    auth=(os.environ[\"LOKI_USER_ID\"], os.environ[\"LOKI_API_KEY\"]),\n    # formatter=JsonFormatter(),\n    # buffer_timeout=10,\n    # buffer_size_threshold=10000,\n)\nlogger.addHandler(custom_handler)\n\nlogger.info(\"sample message with args %s %d\", \"test\", 42)\nlogger.info(\"sample message with extra\", extra={'custom_field': 'custom_value'})\nlogger.error(\"error message\")\ntry:\n    raise Exception(\"test exception\")\nexcept Exception as e:\n    logger.exception(\"exception message\")\n```\n\n## Messages samples\n\n### Default formatter\n\n```\nsample message with args test 42\n```\n\nwith fields:\n\n| Field       | Value   |\n|-------------|---------|\n| application | Test    |\n| envornment  | Develop |\n| level       | INFO    |\n\n### Default formatter with exception\n\n```\nexception message\nTraceback (most recent call last):\n  File \"/home/eric/loki-logger-handler/example.py\", line 32, in <module>\n    raise Exception(\"test exception\")\nException: test exception\n```\n\n### Json Formatter\n\n```json\n{\n  \"message\": \"sample message test 42\",\n  \"timestamp\": 1727007836.0348141,\n  \"thread\": 140158402386816,\n  \"function\": \"<module>\",\n  \"module\": \"example\",\n  \"logger\": \"custom_logger\",\n  \"level\": \"INFO\",\n  \"exc_text\": null\n}\n```\n\n### Json Formatter with exception\n\n```json\n{\n  \"message\": \"exception message\",\n  \"timestamp\": 1727007836.0350208,\n  \"thread\": 140158402386816,\n  \"function\": \"<module>\",\n  \"module\": \"example\",\n  \"logger\": \"custom_logger\",\n  \"level\": \"ERROR\",\n  \"exc_text\": null,\n  \"file\": \"example.py\",\n  \"path\": \"/home/eric/loki-logger-handler/example.py\",\n  \"line\": 27,\n  \"stacktrace\": \"Traceback (most recent call last):\\n  File \\\"/home/eric/loki-logger-handler/example.py\\\", line 25, in <module>\\n    raise Exception(\\\"test exception\\\")\\nException: test exception\\n\"\n}\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Logging handler to send logs to Grafana Loki",
    "version": "0.0.5",
    "project_urls": {
        "Homepage": "https://github.com/fuyufjh/loki_logging_handler"
    },
    "split_keywords": [
        "loki",
        " logging",
        " handler"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5ee5980bab5b4c8a03d3116b9ea274b54212f5a850db07f2762aa0b33fa26c21",
                "md5": "6203b9800a6dce2cfa0306f1a3337fa2",
                "sha256": "c07bda3ae9c51a1bfb8212b3824f4b3b61440d92f91c5e7a455dcb9b8c4dc218"
            },
            "downloads": -1,
            "filename": "loki_logging_handler-0.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6203b9800a6dce2cfa0306f1a3337fa2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 9715,
            "upload_time": "2024-09-22T14:28:09",
            "upload_time_iso_8601": "2024-09-22T14:28:09.148069Z",
            "url": "https://files.pythonhosted.org/packages/5e/e5/980bab5b4c8a03d3116b9ea274b54212f5a850db07f2762aa0b33fa26c21/loki_logging_handler-0.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "89979090a9b1309c515a7300649dd04e6e5c4fdc8efb2f754e84e2dc4fff36f9",
                "md5": "042d09b73e1e690c634a0ed5a34d0e7f",
                "sha256": "4e5845c39a53d9653c0c7bd14f8c08a1689161b7e2b01bd0e4de8942e92adc22"
            },
            "downloads": -1,
            "filename": "loki_logging_handler-0.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "042d09b73e1e690c634a0ed5a34d0e7f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 7866,
            "upload_time": "2024-09-22T14:28:11",
            "upload_time_iso_8601": "2024-09-22T14:28:11.336730Z",
            "url": "https://files.pythonhosted.org/packages/89/97/9090a9b1309c515a7300649dd04e6e5c4fdc8efb2f754e84e2dc4fff36f9/loki_logging_handler-0.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-22 14:28:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fuyufjh",
    "github_project": "loki_logging_handler",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "requests",
            "specs": [
                [
                    "==",
                    "2.28.2"
                ]
            ]
        }
    ],
    "lcname": "loki-logging-handler"
}
        
Elapsed time: 9.07558s