{"library":"python-logging-loki","title":"Python Logging Loki Handler","description":"The `python-logging-loki` library provides a handler for Python's standard `logging` module, allowing applications to send logs directly to Grafana Loki. It includes `LokiHandler` for synchronous logging and `LokiQueueHandler` for asynchronous, non-blocking log submission. The library supports custom labels, basic HTTP authentication, and explicit Loki API versioning. The current stable version is 0.3.1.","language":"python","status":"active","last_verified":"Fri May 15","install":{"commands":["pip install python-logging-loki"],"cli":null},"imports":["from logging_loki import LokiHandler","from logging_loki import LokiQueueHandler"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import logging\nimport os\nfrom logging_loki import LokiHandler\n\n# Configure Loki URL and optional authentication\nLOKI_URL = os.environ.get('LOKI_URL', 'http://localhost:3100/loki/api/v1/push')\nLOKI_USERNAME = os.environ.get('LOKI_USERNAME', '')\nLOKI_PASSWORD = os.environ.get('LOKI_PASSWORD', '')\n\n# Create a Loki handler\nhandler_kwargs = {\n    'url': LOKI_URL,\n    'tags': {'app': 'my-python-app', 'environment': 'development'},\n    'version': '1' # Use '1' for Loki >= 0.4.0\n}\n\nif LOKI_USERNAME and LOKI_PASSWORD:\n    handler_kwargs['auth'] = (LOKI_USERNAME, LOKI_PASSWORD)\n\nhandler = LokiHandler(**handler_kwargs)\n\n# Get a logger and add the Loki handler\nlogger = logging.getLogger('my-app')\nlogger.setLevel(logging.INFO)\nlogger.addHandler(handler)\n\n# Log a message\nlogger.info('Hello, Loki! This is an info message.')\nlogger.warning('This is a warning message with extra data.', extra={'tags': {'component': 'auth'}})\n\nprint(f\"Logs sent to Loki at {LOKI_URL}\")","lang":"python","description":"This quickstart demonstrates how to configure a `LokiHandler` to send logs to a Loki instance. It includes setting the Loki URL, adding default and extra tags, and handling optional basic authentication using environment variables. It explicitly sets the Loki API version to '1' for compatibility with modern Loki deployments.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-15","installed_version":"0.3.1","pypi_latest":"0.3.1","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":2.1,"avg_import_s":0.76,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.79,"mem_mb":12.9,"disk_size":"21.3M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.1,"import_time_s":0.51,"mem_mb":12.9,"disk_size":"22M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.95,"mem_mb":14.1,"disk_size":"23.3M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.2,"import_time_s":0.77,"mem_mb":14.1,"disk_size":"24M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.83,"mem_mb":14.7,"disk_size":"15.1M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.9,"import_time_s":0.8,"mem_mb":14.7,"disk_size":"16M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.84,"mem_mb":15,"disk_size":"14.9M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2,"import_time_s":0.78,"mem_mb":15,"disk_size":"15M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.69,"mem_mb":12.5,"disk_size":"20.5M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"python-logging-loki","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.4,"import_time_s":0.63,"mem_mb":12.5,"disk_size":"21M"}]}}