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.43.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9757c4da43609bb98526471ef502bca5474ebab7e79e7a949a2323662d35c770 |
|
MD5 | b6814fa24c88a5a20fb5c51b3c3f4b30 |
|
BLAKE2b-256 | 402249b03d190e8a8075539b2d9e6b418eee060518a52f009543a688bd1c73bd |
Close
Hashes for py_logging_logship-0.1.43-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 549882e2700f13b733f74ca501920b1ec1cd63c757793af574cad3a2760f4d2f |
|
MD5 | 56f7b063505e4d078ec65dacf81d539e |
|
BLAKE2b-256 | 67d26b11fa91f62762a3fd8e290ae9b14058ac0bdb0f2225d4b5a6e6f8958028 |