2996920277
- 添加 LogHiveHandler,启动时自动挂载到 root logger - 所有 logging 日志自动异步发送到 LogHive,不影响主业务 - vendored loghive-client 包,Docker 构建时自动安装 - API Key 缺失时自动跳过,不影响本地开发
1.8 KiB
1.8 KiB
LogHive Client SDK
Python client SDK for sending logs to LogHive.
Installation
pip install loghive-client
Or install from source:
cd client
pip install .
Quick Start
Sync mode (recommended for scripts, Django, Flask)
from loghive_client import LogHiveLogger
logger = LogHiveLogger(
project="my-awesome-app",
api_key="your-api-key",
endpoint="http://localhost:8000",
)
logger.info("Server started", extra={"port": 8080})
logger.error("Database timeout", exc_info=True)
Async mode (for FastAPI, aiohttp, asyncio)
from loghive_client import AsyncLogHiveLogger
import asyncio
async def main():
async with AsyncLogHiveLogger(
project="my-api",
api_key="your-api-key",
endpoint="http://localhost:8000",
) as logger:
await logger.info("API started")
# ...
asyncio.run(main())
Standard logging integration (zero code change)
Add the handler to your existing logger:
import logging
from loghive_client import LogHiveHandler
handler = LogHiveHandler("my-project", "api-key", "http://localhost:8000")
logging.getLogger().addHandler(handler)
# All existing logger calls now forward to LogHive
logging.info("This goes to LogHive too!")
Configuration
| Param | Default | Description |
|---|---|---|
project |
(required) | Your project name in LogHive |
api_key |
(required) | Your project's API key |
endpoint |
http://localhost:8000 |
LogHive server URL |
batch_size |
50 | Max entries per HTTP request |
flush_interval |
2.0 | Seconds between flushes |
max_retries |
3 | Retries on failure |
timeout |
5.0 | HTTP request timeout |
Trace ID (request correlation)
logger.set_trace_id("req-abc-123")