Logging handler to send logs to Grafana Loki
Project description
loki_logging_handler
A logging handler that sends log messages to Loki in text or JSON format.
Features
- Buffer logs in memory and send to Loki in batch
- Logs pushed in text or JSON format
- Logger extra keys added automatically as keys into pushed JSON
- Publish logs compressed
Args
url
(str): The URL of the Loki server.labels
(dict): A dictionary of labels to attach to each log message.auth
(tuple, optional): A tuple of user id and api key. Defaults to None.buffer_timeout
(int, optional): The time in seconds to wait before flushing the buffer. Defaults to 10.buffer_size_threshold
(int, optional): The number of log messages to buffer before flushing. Defaults to 10000.compressed
(bool, optional): Whether to compress the log messages before sending them to Loki. Defaults toTrue
.defaultFormatter
(logging.Formatter, optional): The formatter to use for log messages. Defaults toPlainFormatter
.
Formatters
logging.Formatter
: Formater for logging the message text. (default)JsonFormatter
: Formater for logging the message and additional fields as JSON.
Quick start
from loki_logging_handler.loki_handler import LokiHandler
import logging
import os
# Set up logging
logger = logging.getLogger("custom_logger")
logger.setLevel(logging.DEBUG)
# Create an instance of the custom handler
custom_handler = LokiHandler(
url=os.environ["LOKI_URL"],
labels={"application": "Test", "envornment": "Develop"},
auth=(os.environ["LOKI_USER_ID"], os.environ["LOKI_API_KEY"]),
# formatter=JsonFormatter(),
# buffer_timeout=10,
# buffer_size_threshold=10000,
)
logger.addHandler(custom_handler)
logger.info("sample message with args %s %d", "test", 42)
logger.info("sample message with extra", extra={'custom_field': 'custom_value'})
logger.error("error message")
try:
raise Exception("test exception")
except Exception as e:
logger.exception("exception message")
Messages samples
Default formatter
sample message with args test 42
with fields:
Field | Value |
---|---|
application | Test |
envornment | Develop |
level | INFO |
Default formatter with exception
exception message
Traceback (most recent call last):
File "/home/eric/loki-logger-handler/example.py", line 32, in <module>
raise Exception("test exception")
Exception: test exception
Json Formatter
{
"message": "sample message test 42",
"timestamp": 1727007836.0348141,
"thread": 140158402386816,
"function": "<module>",
"module": "example",
"logger": "custom_logger",
"level": "INFO",
"exc_text": null
}
Json Formatter with exception
{
"message": "exception message",
"timestamp": 1727007836.0350208,
"thread": 140158402386816,
"function": "<module>",
"module": "example",
"logger": "custom_logger",
"level": "ERROR",
"exc_text": null,
"file": "example.py",
"path": "/home/eric/loki-logger-handler/example.py",
"line": 27,
"stacktrace": "Traceback (most recent call last):\n File \"/home/eric/loki-logger-handler/example.py\", line 25, in <module>\n raise Exception(\"test exception\")\nException: test exception\n"
}
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file loki_logging_handler-0.0.5.tar.gz
.
File metadata
- Download URL: loki_logging_handler-0.0.5.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e5845c39a53d9653c0c7bd14f8c08a1689161b7e2b01bd0e4de8942e92adc22 |
|
MD5 | 042d09b73e1e690c634a0ed5a34d0e7f |
|
BLAKE2b-256 | 89979090a9b1309c515a7300649dd04e6e5c4fdc8efb2f754e84e2dc4fff36f9 |
File details
Details for the file loki_logging_handler-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: loki_logging_handler-0.0.5-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c07bda3ae9c51a1bfb8212b3824f4b3b61440d92f91c5e7a455dcb9b8c4dc218 |
|
MD5 | 6203b9800a6dce2cfa0306f1a3337fa2 |
|
BLAKE2b-256 | 5ee5980bab5b4c8a03d3116b9ea274b54212f5a850db07f2762aa0b33fa26c21 |