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 = ["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 - Property
foo
from log record will be sent as loki label
Properties to label
Using a dict instead of a list for props_to_labels
will enable renaming labels
handler = logging_loki.LokiHandler(
url="https://my-loki-instance/loki/api/v1/push",
tags={"application": "my-app"},
props_to_labels = {
"otelTraceID": "trace_id"
"otelSpanID": "span_id"
}
)
In this case, the properties otelTraceID
& otelSpanID
will be renamed to trace_id
& span_id
loki labels
Non-blocking mode
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
Hashes for inuits_python_logging_loki-1.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d366daae860cbd6e76f110a5b5510a2693b1f2517bfc45ba9b85e4d7d6b364dd |
|
MD5 | 9738ca0262d7334c2717f44d5fa0e8f7 |
|
BLAKE2b-256 | dbf2be7f325c17f55655c279ed0d6c376f5058b755c84b8665a8b2c5aa1a5914 |
Hashes for inuits_python_logging_loki-1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0079a443b24b2aff3ff2870503a644c014162c3e3bc18dc2299b9641fed8995 |
|
MD5 | 3efc0e10a0141057584ca60e3257d5d2 |
|
BLAKE2b-256 | 1ac727f3d2daa78ef76fe8284b72eccc189c9ef7ad46351ac29f58bda7737fa2 |