Handler designed for transmitting logs to Grafana Loki in JSON format.
Low maintenance activity. Last release 18 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
Handler designed for transmitting logs to Grafana Loki in JSON format.
Low maintenance activity. Last release 18 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install loki-logger-handlerA logging handler that sends log messages to (Grafana) Loki in JSON format.
LoguruFormatter. Defaults to False.LoggerFormatter or LoguruFormatter will be used.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"
pip install loki-logger-handler
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'})
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"
)
{
"message": "Starting service",
"timestamp": 1681638266.542849,
"process": 48906,
"thread": 140704422327936,
"function": "run",
"module": "test",
"name": "__main__"
}
{
"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"
}
{
"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 :
{environment="Develop"} |= `` | json
Filter by level:
{environment="Develop", level="INFO"} |= `` | json
Filter by extra:
{environment="Develop", level="INFO"} |= `` | json | code=`200`
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:
loki_metadata keyloki_metadata_keys to move logs keys to loki 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.
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"})
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"
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.
To start working with the Dev Container, follow these steps:
Ctrl+Shift+X / Cmd+Shift+X) and search for Dev Containers.The loki_logger_handler Dev Container provides the following resources:
os.environ["LOKI_URL"]=http://loki:3100/loki/api/v1/push
The MIT License
Use instead of loki-logger-handler: tools that do the same job, closest first.
Compare with python-logging-loki →Compare with loggly-python-handler →Compare with logging-azure-rest →Compare all 4 →
Python logging handler for Grafana Loki.
Recent release
last release 551 days ago
Commit activity
0/26 weeks with commits
Release cadence
0 releases in 12 months
Issues & PRs handled
50% closed in the past year
Contributor spread
13 authors in last 100 commits
OpenSSF Scorecard
not evaluated
Python logging handler that sends messages to Loggly
A python threadded logging handler and service extension for Azure Log Workspace OMS REST API.
A Python logging handler to sends logs to Splunk using HTTP event collector (HEC)
Python logging handler for Logstash.
OpenSearch logging handler
Use together with loki-logger-handler: extensions, drivers, servers and companion tools.