feat: 接入 LogHive 远程日志系统
- 添加 LogHiveHandler,启动时自动挂载到 root logger - 所有 logging 日志自动异步发送到 LogHive,不影响主业务 - vendored loghive-client 包,Docker 构建时自动安装 - API Key 缺失时自动跳过,不影响本地开发
This commit is contained in:
Vendored
+84
@@ -0,0 +1,84 @@
|
||||
# LogHive Client SDK
|
||||
|
||||
Python client SDK for sending logs to [LogHive](https://github.com/your-org/loghive).
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pip install loghive-client
|
||||
```
|
||||
|
||||
Or install from source:
|
||||
|
||||
```bash
|
||||
cd client
|
||||
pip install .
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Sync mode (recommended for scripts, Django, Flask)
|
||||
|
||||
```python
|
||||
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)
|
||||
|
||||
```python
|
||||
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:
|
||||
|
||||
```python
|
||||
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)
|
||||
|
||||
```python
|
||||
logger.set_trace_id("req-abc-123")
|
||||
```
|
||||
Reference in New Issue
Block a user