Python logging handler for Grafana Loki.
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.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 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 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
Built Distribution
Close
Hashes for python-logging-loki-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9dee4a51d265894314eacae55a501a8eadaff2b07b4c482ef365190ec1954c2d |
|
MD5 | 08b6e5d6932ec4f5cc79ce60e95e0b4b |
|
BLAKE2b-256 | 0299e47f7bab601579e8c5fef4da894ebb46f3cac5613ea2170113e9392ca5bf |
Close
Hashes for python_logging_loki-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f4e5052420204e33ce7c54a9380f3f53eb9cd502f31ea69ae9d0ec7f793a111 |
|
MD5 | fc5ffdda7dc87accb25efdf9ec63f97e |
|
BLAKE2b-256 | 04320467fe047765ef408083e7bed88c4a15c5598fc4e622305c1c4b67b12c8b |