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.47.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17438b3d40d4edce391de9b3f69b9df37af0521da9be9914f8ed033d5d1bc4f0 |
|
MD5 | 1ddb05832f2dcc503e7fe23c9f288394 |
|
BLAKE2b-256 | 93608ae234af9cee2be24e06c2374b38b010f24f9f0b2fa0a4df686b1beb6cde |
Close
Hashes for py_logging_logship-0.1.47-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7229a6cb8c865eaa174d926adca8040b434efa908cb1de389ec1bde713603c98 |
|
MD5 | ed815f2bdb6e76a44c193cd0e96cc804 |
|
BLAKE2b-256 | c3765699cc7f9039f102badf29f6ad860521f1bdd498980cfb6026ce057215ea |