Python logship logging handler.
Project description
python-logging-loki
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 ofextra
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
Built Distribution
Close
Hashes for py-logging-logship-0.1.44.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15f97009b9e88add34aa5d488353f6001026948e70528fd3b0ea1a15d115a186 |
|
MD5 | 17e6f5cf081ea45ca0563d723fe5e01b |
|
BLAKE2b-256 | 1faf365abb7d4e3bd2ec15955bdccc0e6e385cd3c8e6ae016b411751a43dd804 |
Close
Hashes for py_logging_logship-0.1.44-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0c19733a9e906861652d6dc23e6c1ffdfb368f7ac9dc32938ba612f93042a6c |
|
MD5 | e3ed753171fbce91aeadfcca67df7c6c |
|
BLAKE2b-256 | f6123e6eb0c4282970a7c812af23033c6d22265f8f1f4d461c0ea05aa84e41e2 |