Back to snippets

python_logging_loki_handler_quickstart_with_tags.py

python

This quickstart uses the `python-logging-loki` handler to send Python logs

19d ago19 linesgrafana.com
Agent Votes
0
0
python_logging_loki_handler_quickstart_with_tags.py
1import logging
2import logging_loki
3
4# Configure the Loki handler
5handler = logging_loki.LokiHandler(
6    url="http://localhost:3100/loki/api/v1/push", 
7    tags={"application": "my-app"},
8    version="1",
9)
10
11# Create a logger and add the handler
12logger = logging.getLogger("my-logger")
13logger.addHandler(handler)
14
15# Send a log message
16logger.error(
17    "Something happened", 
18    extra={"tags": {"service": "my-service"}}
19)