查询日志接口
DNS 查询日志相关的 API 接口。
获取日志列表
获取 DNS 查询日志列表。
http
GET /api/logs/list?page=1&pageSize=50&start_time=2025-01-01&end_time=2025-01-15
Authorization: Bearer {token}查询参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| page | int | ❌ | 页码,默认 1 |
| pageSize | int | ❌ | 每页数量,默认 50,最大 1000 |
| start_time | string | ❌ | 开始时间,格式:2025-01-01T00:00:00Z |
| end_time | string | ❌ | 结束时间 |
| client_ip | string | ❌ | 客户端 IP 筛选 |
| domain | string | ❌ | 域名筛选 |
| query_type | string | ❌ | 查询类型筛选:A、AAAA、MX 等 |
| source | string | ❌ | 来源筛选:local/cache/upstream |
响应:
json
{
"code": 200,
"data": {
"list": [
{
"id": 1,
"timestamp": "2025-01-15T10:30:25Z",
"client_ip": "192.168.1.100",
"domain": "www.example.com",
"query_type": "A",
"response": "192.168.1.50",
"response_time": 25,
"source": "cache",
"upstream": "",
"cache_hit": true,
"status": "success"
}
],
"total": 15000
}
}获取日志统计
获取查询日志的统计信息。
http
GET /api/logs/stats?start=2025-01-01&end=2025-01-15
Authorization: Bearer {token}响应:
json
{
"code": 200,
"data": {
"total_queries": 150000,
"cache_hit_rate": 65.5,
"avg_response_time": 12.5,
"top_domains": [
{"domain": "www.google.com", "count": 15234},
{"domain": "www.baidu.com", "count": 12567}
],
"top_clients": [
{"ip": "192.168.1.100", "count": 25678},
{"ip": "192.168.1.101", "count": 18234}
],
"query_type_distribution": {
"A": 85000,
"AAAA": 15000,
"MX": 5000,
"others": 45000
}
}
}导出日志
导出查询日志为文件。
http
GET /api/logs/export?format=csv&start=2025-01-01&end=2025-01-15
Authorization: Bearer {token}查询参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| format | string | ❌ | 格式:csv/json,默认 csv |
| start | string | ❌ | 开始日期 |
| end | string | ❌ | 结束日期 |
响应:
文件下载响应。
WebSocket 实时日志
通过 WebSocket 实时接收查询日志。
javascript
const ws = new WebSocket('ws://localhost:8085/api/logs/stream');
ws.onopen = () => {
console.log('WebSocket 连接成功');
};
ws.onmessage = (event) => {
const log = JSON.parse(event.data);
console.log('新查询:', log);
};
ws.onclose = () => {
console.log('WebSocket 连接关闭');
};日志数据格式:
json
{
"timestamp": "2025-01-15T10:30:25.123Z",
"client_ip": "192.168.1.100",
"domain": "www.example.com",
"query_type": "A",
"response": "192.168.1.50",
"response_time": 25,
"source": "cache",
"upstream": "",
"cache_hit": true
}使用示例
cURL 示例
bash
TOKEN="your_jwt_token"
# 获取今日日志
curl -s "http://localhost:8085/api/logs/list?page=1&pageSize=10" \
-H "Authorization: Bearer $TOKEN" | jq
# 按域名筛选
curl -s "http://localhost:8085/api/logs/list?domain=example.com" \
-H "Authorization: Bearer $TOKEN" | jq
# 获取统计信息
curl -s "http://localhost:8085/api/logs/stats?start=2025-01-01&end=2025-01-15" \
-H "Authorization: Bearer $TOKEN" | jq
# 导出日志
curl -s "http://localhost:8085/api/logs/export?format=csv&start=2025-01-01" \
-H "Authorization: Bearer $TOKEN" \
-o logs_export.csvPython 示例
python
import requests
import websocket
import json
class DNSGoLogsAPI:
def __init__(self, base_url, token):
self.base_url = base_url
self.headers = {'Authorization': f'Bearer {token}'}
def list_logs(self, page=1, page_size=50, **filters):
params = {'page': page, 'pageSize': page_size, **filters}
response = requests.get(
f'{self.base_url}/api/logs/list',
headers=self.headers,
params=params
)
return response.json()
def get_stats(self, start, end):
params = {'start': start, 'end': end}
response = requests.get(
f'{self.base_url}/api/logs/stats',
headers=self.headers,
params=params
)
return response.json()
def export_logs(self, format='csv', **filters):
params = {'format': format, **filters}
response = requests.get(
f'{self.base_url}/api/logs/export',
headers=self.headers,
params=params
)
return response.content
def stream_logs(self, callback):
"""WebSocket 实时日志"""
token = self.headers['Authorization'].replace('Bearer ', '')
ws_url = self.base_url.replace('http', 'ws') + '/api/logs/stream'
def on_message(ws, message):
log = json.loads(message)
callback(log)
ws = websocket.WebSocketApp(
ws_url,
header=[f'Authorization: Bearer {token}'],
on_message=on_message
)
ws.run_forever()
# 使用示例
api = DNSGoLogsAPI('http://localhost:8085', 'your_token')
# 获取日志
logs = api.list_logs(domain='google.com', page_size=10)
print(logs)
# 获取统计
stats = api.get_stats('2025-01-01', '2025-01-15')
print(stats)
# 实时日志回调
def handle_log(log):
print(f"[{log['timestamp']}] {log['client_ip']} -> {log['domain']}")
# api.stream_logs(handle_log)相关文档
- 查询日志 - 了解查询日志功能