Handler designed for transmitting logs to Grafana Loki in JSON format.
Project description
loki_logger_handler
A logging handler that sends log messages to (Grafana) Loki in JSON format.
Features
- Logs pushed in JSON format by default
- Custom labels definition
- Allows defining loguru and logger extra keys as labels
- Logger extra keys added automatically as keys into pushed JSON
- Publish in batch of Streams
- Publish logs compressed
Args
- url (str): The URL of the Loki server.
- labels (dict): A dictionary of labels to attach to each log message.
- label_keys (dict, optional): A dictionary of keys to extract from each log message and use as labels. Defaults to None.
- auth (tuple, optional): Basic authentication credentials for the Loki request. Defaults to None.
- additional_headers (dict, optional): Additional headers for the Loki request. Defaults to None.
- timeout (int, optional): Timeout interval in seconds to wait before flushing the buffer. Defaults to 10 seconds.
- compressed (bool, optional): Whether to compress the log messages before sending them to Loki. Defaults to True.
- loguru (bool, optional): Whether to use
LoguruFormatter. Defaults to False. - default_formatter (logging.Formatter, optional): Formatter for the log records. If not provided,
LoggerFormatterorLoguruFormatterwill be used. - enable_self_errors (bool, optional): Set to True to show Hanlder errors on console. Defaults to False
Loki 3.0
- enable_structured_loki_metadata (bool, optional): Whether to include structured loki_metadata in the logs. Defaults to False. Only supported for Loki 3.0 and above
- loki_metadata (dict, optional): Default loki_metadata values. Defaults to None. Only supported for Loki 3.0 and above
- loki_metadata_keys (arrray, optional): Specific log record keys to extract as loki_metadata. Only supported for Loki 3.0 and above
Formatters
- LoggerFormatter: Formatter for default python logging implementation
- LoguruFormatter: Formatter for Loguru python library
How to use
First create a environment varialble to setup your loki url with this structure (Ej: https://100239:wdadw....dwad@logs-prod-eu-west-0.grafana.net/loki/api/v1/push)
LOKI_URL="https://{{USER}}:{{PASSWORD}}@{{GRAFANA_LOKI_URL}}/loki/api/v1/push"
Install
pip install loki-logger-handler
Logger
from loki_logger_handler.loki_logger_handler import LokiLoggerHandler,
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 = LokiLoggerHandler(
url=os.environ["LOKI_URL"],
labels={"application": "Test", "environment": "Develop"},
label_keys={},
timeout=10,
)
# Create an instance of the custom handler
logger.addHandler(custom_handler)
logger.debug("Debug message", extra={'custom_field': 'custom_value'})
Loguru
from loki_logger_handler.loki_logger_handler import LokiLoggerHandler
from loki_logger_handler.formatters.loguru_formatter import LoguruFormatter
from loguru import logger
import os
custom_handler = LokiLoggerHandler(
url=os.environ["LOKI_URL"],
labels={"application": "Test", "environment": "Develop"},
label_keys={},
timeout=10,
default_formatter=LoguruFormatter(),
)
logger.configure(handlers=[{"sink": custom_handler, "serialize": True}])
logger.info(
"Response code {code} HTTP/1.1 GET {url}", code=200, url="https://loki_handler.io"
)
Loki messages samples
Without extra
{
"message": "Starting service",
"timestamp": 1681638266.542849,
"process": 48906,
"thread": 140704422327936,
"function": "run",
"module": "test",
"name": "__main__"
}
With extra
{
"message": "Response code 200 HTTP/1.1 GET https://loki_handler.io",
"timestamp": 1681638225.877143,
"process": 48870,
"thread": 140704422327936,
"function": "run",
"module": "test",
"name": "__main__",
"code": 200,
"url": "https://loki_handler.io"
}
Exceptions
{
"message": "name 'plan' is not defined",
"timestamp": 1681638284.358464,
"process": 48906,
"thread": 140704422327936,
"function": "run",
"module": "test",
"name": "__main__",
"file": "test.py",
"path": "/test.py",
"line": 39
}
Loki Query Sample
Loki query sample :
{environment="Develop"} |= `` | json
Filter by level:
{environment="Develop", level="INFO"} |= `` | json
Filter by extra:
{environment="Develop", level="INFO"} |= `` | json | code=`200`
Loki Structured Metadata
Loki structured metadata to include additional context in your logs. This can be useful for filtering and querying logs in Loki.
We can add metadata in 3 ways:
- Defile static loki_metadata that will be injected into all logs lines
- Use logger extra options adding metadata inside
loki_metadatakey - Use logger
loki_metadata_keysto move logs keys to loki metadata.
Example global metadata
from loki_logger_handler.loki_logger_handler import LokiLoggerHandler
import logging
import os
# Set up logging
logger = logging.getLogger("custom_logger")
logger.setLevel(logging.DEBUG)
# Create an instance of the custom handler with structured metadata
custom_handler = LokiLoggerHandler(
url=os.environ["LOKI_URL"],
labels={"application": "Test", "environment": "Develop"},
label_keys={},
timeout=10,
enable_structured_loki_metadata=True,
loki_metadata={"service": "user-service", "version": "1.0.0"}
)
logger.addHandler(custom_handler)
In this example, the loki_metadata dictionary includes metadata that will be attached to every log message. The enable_structured_loki_metadata flag ensures that this metadata is included in the logs.
Example log metadata
from loki_logger_handler.loki_logger_handler import LokiLoggerHandler
import logging
import os
# Set up logging
logger = logging.getLogger("custom_logger")
logger.setLevel(logging.DEBUG)
# Create an instance of the custom handler with structured metadata
custom_handler = LokiLoggerHandler(
url=os.environ["LOKI_URL"],
labels={"application": "Test", "environment": "Develop"},
label_keys={},
timeout=10,
enable_structured_loki_metadata=True,
loki_metadata={"service": "user-service", "version": "1.0.0"}
)
logger.addHandler(custom_handler)
logger.info("User acction", extra={"loki_metadata": {"user_id": 12345, "operation": "update", "status": "success"}})
from loki_logger_handler.loki_logger_handler import LokiLoggerHandler
import logging
import os
# Set up logging
logger = logging.getLogger("custom_logger")
logger.setLevel(logging.DEBUG)
# Create an instance of the custom handler with structured metadata
custom_handler = LokiLoggerHandler(
url=os.environ["LOKI_URL"],
labels={"application": "Test", "environment": "Develop"},
label_keys={},
timeout=10,
enable_structured_loki_metadata=True,
loki_metadata={"service": "user-service", "version": "1.0.0"},
loki_metadata_keys=["trace_id"]
)
logger.addHandler(custom_handler)
logger.info("User acction", extra={"loki_metadata": {"user_id": 12345, "operation": "update", "status": "success"}, "trace_id": "000-000000-0000"})
Querying Logs with Structured Metadata
You can query logs in Loki using the structured metadata.
This query will return all logs where the service metadata is set to user-service.
{application="Test"} |= `` | service="user-service"
This query will return all logs where the user_id metadata is set to 12345.
{application="Test"} |= `` | user_id="12345"
This query will return all logs where the trace_id metadata is set to 000-000000-0000.
{application="Test"} |= `` | trace_id="000-000000-0000"
Development Environment: Dev Container
This project uses a Dev Container to provide a consistent and reproducible development environment. A Dev Container ensures all team members have the same tools, dependencies, and configurations, avoiding "works on my machine" issues.
Why Use a Dev Container?
- Consistency: Ensures everyone works in the same environment, regardless of the host OS.
- Isolation: Keeps project dependencies separate from your system.
- Portability: Easily onboard new developers by setting up the environment with a single command.
- Pre-configured Tools: Includes all required tools and dependencies for the project.
Getting Started with the Dev Container
To start working with the Dev Container, follow these steps:
Prerequisites
- Install Docker Desktop (required for running containers).
- Install Visual Studio Code (VS Code).
- Install the Dev Containers extension in VS Code:
- Go to Extensions (
Ctrl+Shift+X/Cmd+Shift+X) and search forDev Containers. - Install the extension by Microsoft.
- Go to Extensions (
Setup Instructions
- Clone the repository
- Open in VS Code
- Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and select: Dev Containers: Reopen in Container
- VS Code will:
- Pull the Dev Container image.
- Install all dependencies and tools specified.
Resources
The loki_logger_handler Dev Container provides the following resources:
- Grafana: Accessible externally at http://localhost:3000.
- Loki: Accessible internally at http://loki:3100/loki/api/v1/push. You can use this URL in your code as the publish endpoint for logs. Logs can be viewed and queried via the Grafana interface.
os.environ["LOKI_URL"]=http://loki:3100/loki/api/v1/push
License
The MIT License
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file loki_logger_handler-1.1.2.tar.gz.
File metadata
- Download URL: loki_logger_handler-1.1.2.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3b588903420197ac9bb477f9c8fd6c18edf3e475b94f3d27d2aeed3930eb5d1
|
|
| MD5 |
87bdba1d8ea3e1311b843a050d3ee480
|
|
| BLAKE2b-256 |
ea606c212ae881abf82e6b85fddd6ef338404c4dbf812c3b1cb88527903c7633
|
Provenance
The following attestation bundles were made for loki_logger_handler-1.1.2.tar.gz:
Publisher:
publish.yml on xente/loki-logger-handler
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
loki_logger_handler-1.1.2.tar.gz -
Subject digest:
b3b588903420197ac9bb477f9c8fd6c18edf3e475b94f3d27d2aeed3930eb5d1 - Sigstore transparency entry: 220096246
- Sigstore integration time:
-
Permalink:
xente/loki-logger-handler@ef37f0e62ba938cb4fe88f6c1fe2cbe5ba4dd173 -
Branch / Tag:
refs/tags/1.1.2 - Owner: https://github.com/xente
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ef37f0e62ba938cb4fe88f6c1fe2cbe5ba4dd173 -
Trigger Event:
release
-
Statement type:
File details
Details for the file loki_logger_handler-1.1.2-py3-none-any.whl.
File metadata
- Download URL: loki_logger_handler-1.1.2-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ee10259162caf6aa21b784d753dc2f73102225517857bb40051b7e933cfd07d
|
|
| MD5 |
1bd4e6183dde48931c4042e077c5bfdd
|
|
| BLAKE2b-256 |
090093631b307a801d8fe48867e20923fdb161f5d0aa165ca322b71544c35b96
|
Provenance
The following attestation bundles were made for loki_logger_handler-1.1.2-py3-none-any.whl:
Publisher:
publish.yml on xente/loki-logger-handler
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
loki_logger_handler-1.1.2-py3-none-any.whl -
Subject digest:
7ee10259162caf6aa21b784d753dc2f73102225517857bb40051b7e933cfd07d - Sigstore transparency entry: 220096248
- Sigstore integration time:
-
Permalink:
xente/loki-logger-handler@ef37f0e62ba938cb4fe88f6c1fe2cbe5ba4dd173 -
Branch / Tag:
refs/tags/1.1.2 - Owner: https://github.com/xente
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ef37f0e62ba938cb4fe88f6c1fe2cbe5ba4dd173 -
Trigger Event:
release
-
Statement type: