Python

Send logs from Python apps using the requests library or the standard logging module.

Quick start — requests

Install requests if you haven't already, then send a log with a single POST:

python
import requests

LOGFLOW_API_KEY = "lf_YOUR_API_KEY"

def log(level: str, message: str, service: str = "app", **attributes):
    requests.post(
        "https://api.getlogflow.com/v1/logs",
        headers={"Authorization": f"Bearer {LOGFLOW_API_KEY}"},
        json={
            "level": level,
            "message": message,
            "service": service,
            "attributes": attributes,
        },
        timeout=3,
    )

log("info", "Server started", service="api", port=8000)
log("error", "DB connection failed", service="api", host="db-1")
💡
Set timeout=3 so logging never blocks your main thread. For high-volume apps, use batching below.

Batch sending

Send up to 1000 logs per request with /v1/logs/batch:

python
import requests, queue, threading, time

LOGFLOW_API_KEY = "lf_YOUR_API_KEY"
_queue: queue.Queue = queue.Queue()

def log(level: str, message: str, service: str = "app", **attributes):
    _queue.put({"level": level, "message": message, "service": service, "attributes": attributes})

def _flush():
    while True:
        batch = []
        try:
            while len(batch) < 100:
                batch.append(_queue.get(timeout=2))
        except queue.Empty:
            pass
        if batch:
            requests.post(
                "https://api.getlogflow.com/v1/logs/batch",
                headers={"Authorization": f"Bearer {LOGFLOW_API_KEY}"},
                json={"logs": batch},
                timeout=5,
            )

threading.Thread(target=_flush, daemon=True).start()

Using Python's logging module

Drop LogFlow into any existing Python app via a custom logging.Handler:

python
import logging, requests, threading, queue

class LogFlowHandler(logging.Handler):
    LEVEL_MAP = {
        logging.DEBUG:    "debug",
        logging.INFO:     "info",
        logging.WARNING:  "warn",
        logging.ERROR:    "error",
        logging.CRITICAL: "fatal",
    }

    def __init__(self, api_key: str, service: str = "app"):
        super().__init__()
        self.api_key = api_key
        self.service = service

    def emit(self, record: logging.LogRecord):
        try:
            requests.post(
                "https://api.getlogflow.com/v1/logs",
                headers={"Authorization": f"Bearer {self.api_key}"},
                json={
                    "level": self.LEVEL_MAP.get(record.levelno, "info"),
                    "message": self.format(record),
                    "service": self.service,
                },
                timeout=3,
            )
        except Exception:
            pass  # never let logging crash your app


# Usage
logger = logging.getLogger("myapp")
logger.addHandler(LogFlowHandler(api_key="lf_YOUR_API_KEY", service="api"))
logger.setLevel(logging.DEBUG)

logger.info("Server started")
logger.error("Payment failed", extra={"user_id": "u_123"})

Django integration

Add the handler to your LOGGING dict in settings.py:

python
LOGGING = {
    "version": 1,
    "handlers": {
        "logflow": {
            "class": "myapp.logging.LogFlowHandler",
            "api_key": "lf_YOUR_API_KEY",
            "service": "django",
        },
    },
    "root": {
        "handlers": ["logflow"],
        "level": "INFO",
    },
}

Environment variables

Store your API key in the environment, not in code:

python
import os
LOGFLOW_API_KEY = os.environ["LOGFLOW_API_KEY"]