app-logs-ai
Python SDK for AI Application Logs. Batches log entries and ships them to the ingest API.
Install
pip install app-logs-ai
Quick Start
from app_logs_ai import create_logger
logger = create_logger({
"api_key": "your-api-key-here",
})
logger.info("Server started", {"port": 8000})
logger.error("Database connection failed", {"error": "ECONNREFUSED"})
Configuration
Options passed to create_logger():
api_key(required): Your project's API keyendpoint: Ingest endpoint URL (defaults to https://api-app-logs.up.railway.app/v1/ingest)batch_size: Flush when this many entries are buffered (default: 20)flush_interval_ms: Flush at most this often in milliseconds (default: 2000)source: Logical source label (default: "backend")max_buffer_size: Max entries kept in memory (default: 10000)max_retries: Send attempts per flush before re-queueing (default: 4)retry_backoff_ms: Base delay for exponential backoff (default: 500)gzip_threshold: Compress payloads larger than this (bytes, 0 to disable, default: 1024)persist_path: File path for crash-durable buffering (optional)flush_on_exit: Auto-flush on SIGTERM/SIGINT (default: True)fallback_to_stderr: Print undelivered logs to stderr (default: True)enable_metrics_heartbeat: Send periodic metrics (default: True)metrics_heartbeat_ms: Metrics interval in milliseconds (default: 15000)
Log Levels
The logger supports these levels:
debug(message, attributes)info(message, attributes)warn(message, attributes)error(message, attributes)fatal(message, attributes)
The generic log(level, message, attributes) method is also available.
Structured Events
The SDK supports rich structured event types:
HTTP Requests
Record completed HTTP requests with timing and status:
from app_logs_ai import record_http_request, HttpRequestEvent
record_http_request(logger, HttpRequestEvent(
method="GET",
route="/api/users",
status=200,
duration_ms=125.5,
extra={"user_id": "user123"} # Optional context
))
Exceptions
Record exceptions with stack traces:
from app_logs_ai import record_exception, ExceptionEvent
try:
do_something()
except Exception as e:
record_exception(logger, ExceptionEvent(
error=e,
method="POST",
route="/api/process",
status=500,
extra={"request_id": "req-123"}
))
Metrics
Periodically emit process metrics (uptime, memory):
from app_logs_ai import start_metrics_heartbeat
stop = start_metrics_heartbeat(logger, interval_ms=15000)
# Later, to stop:
stop.set()
Durability
- Batching: Entries are buffered and sent every 2 seconds or when 20 entries accumulate
- Retries: Failed sends are retried with exponential backoff
- Persistence: With
persist_pathset, undelivered entries survive process restarts - Graceful shutdown: Automatic flush on exit; call
logger.flush()to ensure delivery
Example with FastAPI
See examples/fastapi-app/ for a complete example application.
from fastapi import FastAPI
from app_logs_ai import create_logger, record_http_request, record_exception, HttpRequestEvent
import os
import time
app = FastAPI()
logger = create_logger({"api_key": os.environ.get("APP_LOGS_KEY", "test-key")})
@app.middleware("http")
async def log_requests(request, call_next):
start = time.time()
try:
response = await call_next(request)
duration_ms = (time.time() - start) * 1000
# Record structured HTTP event
record_http_request(logger, HttpRequestEvent(
method=request.method,
route=request.url.path,
status=response.status_code,
duration_ms=duration_ms
))
return response
except Exception as exc:
duration_ms = (time.time() - start) * 1000
record_exception(logger, ExceptionEvent(
error=exc,
method=request.method,
route=request.url.path,
duration_ms=duration_ms
))
raise
@app.get("/api/users")
async def get_users():
logger.info("Fetching users", {"endpoint": "/api/users"})
return {"users": []}
@app.exception_handler(Exception)
async def exception_handler(request, exc):
record_exception(logger, ExceptionEvent(
"path": str(request.url),
"error": str(exc),
})
return {"error": str(exc)}
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file app_logs_ai-0.1.6.tar.gz.
File metadata
- Download URL: app_logs_ai-0.1.6.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed2e7a825703190f1d285f902d71fe4e4c0a4aa46cdeccbb1e80ccc5e4d1c151
|
|
| MD5 |
4e063488f63769ddbb46183d759c76fb
|
|
| BLAKE2b-256 |
9d3748d2a2032290a3c1c7061f987c4440bbf8aaee03f14a95089b5eb58af701
|
File details
Details for the file app_logs_ai-0.1.6-py3-none-any.whl.
File metadata
- Download URL: app_logs_ai-0.1.6-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee7126a564b95dce7c7713e9319a222ca4c797e17ab2d5745ed6dff62e7cd973
|
|
| MD5 |
02f5e75a42f6b9a68f3c7ac48871f4de
|
|
| BLAKE2b-256 |
e865081d52659da3075123cd1707b2339c98619fd4a7a2c04acd7e806762b6c5
|