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.45.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 139f59b4234d2e241d8a9d942c1a9fdf8c6305e9275baa6a694cfa1b25c20522 |
|
MD5 | f1c09ecc3a2a34bc5f5115924acc19bc |
|
BLAKE2b-256 | e52862d8f133456abe4f563e0a7b1586369586a69440466941fc3632b561eb95 |
Close
Hashes for py_logging_logship-0.1.45-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6b35589b3d80878ee9e9df2b54b3689f10ebdf1c2925f4a72af4ddd30ecc756 |
|
MD5 | a118ddd89a94b15e18f2fbb990da8a26 |
|
BLAKE2b-256 | 745229e62ec3bc66aa764d8d9a0a1f5a4309fb7fcf806970b328dcb95b6c1e3a |