Skip to main content

python-logging-loki

PyPI version Python version License Build Status

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

Installation

pip install 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"},
    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.

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.

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(...)

Download files

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

Source Distribution

python-logging-loki-0.3.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

python_logging_loki-0.3.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file python-logging-loki-0.3.1.tar.gz.

File metadata

  • Download URL: python-logging-loki-0.3.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.2

File hashes

Hashes for python-logging-loki-0.3.1.tar.gz
Algorithm Hash digest
SHA256 b83610c8a3adc99fbab072493b91dfb25ced69be4874fefe3ab457b391adbf60
MD5 9fd79fa39b6dd6ec5816e6dc63c21b37
BLAKE2b-256 39e2b1ca91524530e5c7d73e70c4a2df952e5b7f1519977c7c51b3966b0332a8

See more details on using hashes here.

File details

Details for the file python_logging_loki-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: python_logging_loki-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.2

File hashes

Hashes for python_logging_loki-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8a9131db037fbea3d390089c4c32dbe7ed233944905079615a9fb6f669b0f4e6
MD5 d128186e7a931d5142d2b343ad8681d7
BLAKE2b-256 e9ca1f5660fbda815ed04839d657cab77605337e32c224c03c780cd383c7bcd6

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

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