Skip to main content

Python logship logging handler.

Project description

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

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

py-logging-logship-0.1.47.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

py_logging_logship-0.1.47-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file py-logging-logship-0.1.47.tar.gz.

File metadata

  • Download URL: py-logging-logship-0.1.47.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for py-logging-logship-0.1.47.tar.gz
Algorithm Hash digest
SHA256 17438b3d40d4edce391de9b3f69b9df37af0521da9be9914f8ed033d5d1bc4f0
MD5 1ddb05832f2dcc503e7fe23c9f288394
BLAKE2b-256 93608ae234af9cee2be24e06c2374b38b010f24f9f0b2fa0a4df686b1beb6cde

See more details on using hashes here.

File details

Details for the file py_logging_logship-0.1.47-py3-none-any.whl.

File metadata

  • Download URL: py_logging_logship-0.1.47-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for py_logging_logship-0.1.47-py3-none-any.whl
Algorithm Hash digest
SHA256 7229a6cb8c865eaa174d926adca8040b434efa908cb1de389ec1bde713603c98
MD5 ed815f2bdb6e76a44c193cd0e96cc804
BLAKE2b-256 c3765699cc7f9039f102badf29f6ad860521f1bdd498980cfb6026ce057215ea

See more details on using hashes here.

Supported by

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