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.2.0.tar.gz (3.5 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.2.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for python-logging-loki-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9dee4a51d265894314eacae55a501a8eadaff2b07b4c482ef365190ec1954c2d
MD5 08b6e5d6932ec4f5cc79ce60e95e0b4b
BLAKE2b-256 0299e47f7bab601579e8c5fef4da894ebb46f3cac5613ea2170113e9392ca5bf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_logging_loki-0.2.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.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.2

File hashes

Hashes for python_logging_loki-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f4e5052420204e33ce7c54a9380f3f53eb9cd502f31ea69ae9d0ec7f793a111
MD5 fc5ffdda7dc87accb25efdf9ec63f97e
BLAKE2b-256 04320467fe047765ef408083e7bed88c4a15c5598fc4e622305c1c4b67b12c8b

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