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.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c5850b065e6a0bfbb24bf98d6e5c73d3bab44a28ef934521d3b0c8c217c7aef |
|
MD5 | 7ea13140cb91ab77b6642a80d6a5896d |
|
BLAKE2b-256 | b5cbcbde9911204ea944785a3e752c6a4ec7506bb590c378b726b317c95b2d76 |
Close
Hashes for python_logging_loki-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d014411db34f00380e3d191be3c5e3ad67420d0c8d13189250d8a9042656069f |
|
MD5 | a879e4acb90cab5a6d706a64ac7ef70e |
|
BLAKE2b-256 | 2f19520f706008c4903a5527143e0f3c4dd39872a98074889607014d80b3b65e |