Loki Logger Handler is a Python logging handler designed for transmitting logs to Grafana Loki. It formats logs in JSON by default, allows custom label definitions, and supports extracting extra keys from log records as labels or structured metadata. The library is currently at version 1.1.2 and appears to be under active development with regular updates addressing compatibility and feature enhancements.
pip install loki-logger-handlerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure the `LokiLoggerHandler` with Python's standard `logging` module. It sets up a handler to send logs to a Loki instance, including static labels and dynamically extracting `extra` dictionary keys as labels. Ensure the `LOKI_URL` environment variable is set or replace the placeholder.
Upgrade to version 1.0.0 or higher to ensure broader Python 2/3 compatibility. If sticking to older versions, test thoroughly across target Python environments.
Upgrade to version 1.1.1 or later to utilize the `auth` parameter for basic authentication credentials.
Ensure you are using `loki-logger-handler` version 1.1.0 or newer and have a Loki 3.0+ instance if you intend to send structured metadata.
Upgrade to version 1.1.2 or higher to resolve the `AttributeError` when `message_in_json_format` is `False`.
Move high-cardinality fields into the log message body or as structured metadata (if using Loki 3.0+ and the handler supports it) rather than labels. Use labels for broad categories like `application`, `environment`, `service`.