Skip to main content

Python logging handler for Grafana Loki.

Project description

python-logging-loki

PyPI version Python version License

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-instnace/api/prom/push", 
    tags={"application": "my-app"},
    auth=("username", "password"),    
)

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 queue import Queue


queue = Queue(-1)
handler = logging.handlers.QueueHandler(queue)
handler_loki = logging_loki.LokiHandler(
    url="https://my-loki-instnace/api/prom/push", 
    tags={"application": "my-app"},
    auth=("username", "password"),    
)
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 queue import Queue


handler = logging_loki.LokiQueueHandler(
    Queue(-1),
    url="https://my-loki-instnace/api/prom/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

python-logging-loki-0.1.0.tar.gz (3.3 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.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python-logging-loki-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for python-logging-loki-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8c5850b065e6a0bfbb24bf98d6e5c73d3bab44a28ef934521d3b0c8c217c7aef
MD5 7ea13140cb91ab77b6642a80d6a5896d
BLAKE2b-256 b5cbcbde9911204ea944785a3e752c6a4ec7506bb590c378b726b317c95b2d76

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_logging_loki-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for python_logging_loki-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d014411db34f00380e3d191be3c5e3ad67420d0c8d13189250d8a9042656069f
MD5 a879e4acb90cab5a6d706a64ac7ef70e
BLAKE2b-256 2f19520f706008c4903a5527143e0f3c4dd39872a98074889607014d80b3b65e

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