Python logging handler for Grafana Loki.
Project description
python-logging-loki
Python logging handler for Loki.
https://grafana.com/loki
Installation
pip install inuits-python-logging-loki
Usage
import logging
import logging_loki
handler = logging_loki.LokiHandler(
url="https://my-loki-instance/loki/api/v1/push",
tags={"application": "my-app"},
headers={"X-Scope-OrgID": "example-id"},
auth=("username", "password"),
props_to_labels: Optional[list[str]] = ["foo"]
)
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.LokiHandler(
url="https://my-loki-instance/loki/api/v1/push",
tags={"application": "my-app"},
headers={"X-Scope-OrgID": "example-id"},
auth=("username", "password"),
props_to_labels: Optional[list[str]] = ["foo"]
)
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"),
)
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 inuits-python-logging-loki-1.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0483f44e7d35f48102887466cdf1b31fee7b0ca6de0ba689ac6f055cce43ea70 |
|
MD5 | 24671261687e44663ca0368709246a0a |
|
BLAKE2b-256 | 85c0e26fb38c46bdecac61157772df84d84060cab0f9564d7d40df85bc215989 |
Close
Hashes for inuits_python_logging_loki-1.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c36c358196462696e0c3b0d7c633acf56c3538805158fee014756f0009e29da |
|
MD5 | 3f2375130b710bea934d552f048e6f55 |
|
BLAKE2b-256 | c2d179ddceb3be420fc3ef9d6950a0d7e5795ba5ddf447fc1a855f20cecabcdb |