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.46.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 739d0de44ecb67ca5c45f29bdf6b531fbe7f6a0cbf3874aa170faf09423941ea |
|
MD5 | 35d0c653a2840bd8d10d4a81bd72d1a6 |
|
BLAKE2b-256 | 76bb9dee85cecc20056c1d0dce989ac6fed9c6fa1456b09a9d96df5d6196da96 |
Close
Hashes for py_logging_logship-0.1.46-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24c49887b266850b053effa7c45a8310fa3eb28d117e4431b869c4164b8d9782 |
|
MD5 | 4cf7cb82229053aed2870590d8336424 |
|
BLAKE2b-256 | 6755dde5982e7b2f62996e644b4f3cea048dd5df7e95d8ba9320bb312aa9a289 |