Spaces:
Paused
Paused
feat: Add error handling and logging to status endpoint
Browse filesWrap the `/status` route logic in a try-except block to handle potential errors gracefully.
Add logging for endpoint access, successful response data, and errors.
Return a 500 Internal Server Error response if an exception occurs while fetching status information.
app.py
CHANGED
|
@@ -374,35 +374,42 @@ def index():
|
|
| 374 |
@app.route('/status')
|
| 375 |
def get_system_status():
|
| 376 |
"""提供系统状态的 JSON 数据"""
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
|
| 408 |
if __name__ == '__main__':
|
|
|
|
| 374 |
@app.route('/status')
|
| 375 |
def get_system_status():
|
| 376 |
"""提供系统状态的 JSON 数据"""
|
| 377 |
+
logger.info("访问 /status 路由", "StatusAPI") # 添加日志
|
| 378 |
+
try:
|
| 379 |
+
# 获取配置信息 (需要根据实际CONFIG结构调整)
|
| 380 |
+
config_info = {
|
| 381 |
+
"api_base_url": CONFIG["API"]["BASE_URL"],
|
| 382 |
+
"log_level": logger.level, # 使用logger.level获取日志级别
|
| 383 |
+
"is_temp_conversation": CONFIG["API"]["IS_TEMP_CONVERSATION"],
|
| 384 |
+
"is_custom_sso": CONFIG["API"]["IS_CUSTOM_SSO"],
|
| 385 |
+
"proxy": CONFIG["API"]["PROXY"],
|
| 386 |
+
"manager_switch": CONFIG["ADMIN"]["MANAGER_SWITCH"],
|
| 387 |
+
"show_thinking": CONFIG["SHOW_THINKING"],
|
| 388 |
+
"isshow_search_results": CONFIG["ISSHOW_SEARCH_RESULTS"]
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
# 获取令牌状态
|
| 392 |
+
token_status = {
|
| 393 |
+
"total_tokens": token_manager.get_total_token_count(),
|
| 394 |
+
"available_tokens": token_manager.get_remaining_token_request_capacity(),
|
| 395 |
+
"total_request_count": token_manager.get_total_request_count(),
|
| 396 |
+
"expired_tokens_count": len(token_manager.get_expired_tokens()),
|
| 397 |
+
"token_details": token_manager.get_token_status_map() # 提供更详细的令牌状态
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
# 获取日志摘要
|
| 401 |
+
log_summary = logger.get_recent_logs()
|
| 402 |
+
|
| 403 |
+
response_data = {
|
| 404 |
+
"config": config_info,
|
| 405 |
+
"token_status": token_status,
|
| 406 |
+
"log_summary": log_summary
|
| 407 |
+
}
|
| 408 |
+
logger.info(f"/status 路由返回数据: {json.dumps(response_data, indent=2)}", "StatusAPI") # 添加日志
|
| 409 |
+
return jsonify(response_data)
|
| 410 |
+
except Exception as e:
|
| 411 |
+
logger.error(f"访问 /status 路由时发生错误: {str(e)}", "StatusAPI") # 添加错误日志
|
| 412 |
+
return jsonify({"error": "无法获取系统状态"}), 500
|
| 413 |
|
| 414 |
|
| 415 |
if __name__ == '__main__':
|