Skip to main content

Handler created for Loguru that sends logs to Grafana Loki in JSON format

Project description

loguru-loki-handlers

Handler created for Loguru that sends logs to Grafana Loki in JSON format

Features

  • Logs pushed in JSON format
  • Custom labels definition
  • Allows defining loguru and logger extra keys as labels
  • Logger extra keys added automatically as keys into pushed JSON

How to use

from loguru_loki_handler import loki_handler
from loguru import logger
import os 

os.environ["LOKI_URL"]="https://USER:PASSWORD@logs-prod-eu-west-0.grafana.net/loki/api/v1/push"

logger.configure(handlers=[{"sink":  loki_handler(os.environ["LOKI_URL"],{"application":"Test", "envornment":"Develop"}), "serialize": True}])
logger.info("Starting service")
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": "Request return 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 :

{envornment="Develop"} |= `` | json

Filter by level:

{envornment="Develop", level="INFO"} |= `` | json

Filter by extra:

{envornment="Develop", level="INFO"} |= `` | json | code=`200`

License

The MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

loguru-loki-handler-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

loguru_loki_handler-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file loguru-loki-handler-0.1.0.tar.gz.

File metadata

  • Download URL: loguru-loki-handler-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for loguru-loki-handler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 980567fdabc3c30552e2ce5b723b7bb4235bbee94f193e4467b3c44e6be18550
MD5 40137fd163bd2c763a080647bd4b1057
BLAKE2b-256 709aa22de37754ddf25128cd5e82ed4c1d174d1290d64187131f4040b5580fe8

See more details on using hashes here.

File details

Details for the file loguru_loki_handler-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for loguru_loki_handler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd7f87ba419e97af8ea51279c333b0c0956a368d86f8ff57e054336cbcc5078d
MD5 4d284e5d9b7b4a5917bc7a8742da1543
BLAKE2b-256 bfe72850f80e7189fbc15173ebb540ca685a5afffa78602788af31da3bfb6ea3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page