Skip to main content

Python logging handler for Grafana Loki.

Project description

python-logging-loki

PyPI version Python version License Build Status

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

Installation

pip install inuits-python-logging-loki

Usage

import logging
import logging_loki


handler = logging_loki.LokiHandler(
    url="https://my-loki-instance/loki/api/v1/push",
    tags={"application": "my-app"},
    headers={"X-Scope-OrgID": "example-id"},
    auth=("username", "password"),
    props_to_labels = ["foo"]
)

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
  • Property foo from log record will be sent as loki label

Properties to label

Using a dict instead of a list for props_to_labels will enable renaming labels

handler = logging_loki.LokiHandler(
    url="https://my-loki-instance/loki/api/v1/push",
    tags={"application": "my-app"},
    props_to_labels = {
        "otelTraceID": "trace_id"
        "otelSpanID":  "span_id"
    }
)

In this case, the properties otelTraceID & otelSpanID will be renamed to trace_id & span_id loki labels

Non-blocking mode

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.

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"},
    headers={"X-Scope-OrgID": "example-id"},
    auth=("username", "password"),
    props_to_labels: Optional[list[str]] = ["foo"]
)
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.

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"),
)

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

inuits_python_logging_loki-1.2.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inuits_python_logging_loki-1.2.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file inuits_python_logging_loki-1.2.0.tar.gz.

File metadata

File hashes

Hashes for inuits_python_logging_loki-1.2.0.tar.gz
Algorithm Hash digest
SHA256 381f99ee1a45a879a824e825497263e85212c8a3f07295c0b0326ea8d7f9b348
MD5 09eaec360a7bc35ed552c78b8b2a5fae
BLAKE2b-256 de0cd8ee32dd979271f109449af31c3cb1a1a051826e6af2f0b123eb93a5b569

See more details on using hashes here.

File details

Details for the file inuits_python_logging_loki-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for inuits_python_logging_loki-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dfbc37296ab9532a8263230d2bd5918fc96ed3e7b8c6b5f6bd27c6f2dfa0e68f
MD5 0ed853bfbcd8908309de27bfc28defcc
BLAKE2b-256 8c9c590c64b4dc0dca927d18675b31ead6338a0e05022da9936d315a75e7156c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page