添加定时任务日志API错误处理和调试日志
为了诊断500错误,在日志API中添加: 1. 完整的try-except错误捕获 2. 详细的调试日志输出: - 请求用户和任务ID - 权限检查结果 - 查询过程和结果 - 具体的错误信息和堆栈跟踪 3. 返回详细错误信息给前端(开发模式) 现在可以通过应用日志查看具体的500错误原因。 位置: app.py 第3274-3301行 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
15
app.py
15
app.py
@@ -3275,15 +3275,30 @@ def run_schedule_now_api(schedule_id):
|
|||||||
@login_required
|
@login_required
|
||||||
def get_schedule_logs_api(schedule_id):
|
def get_schedule_logs_api(schedule_id):
|
||||||
"""获取定时任务执行日志"""
|
"""获取定时任务执行日志"""
|
||||||
|
try:
|
||||||
|
print(f"[API日志] 用户 {current_user.id} 请求查看定时任务#{schedule_id}的执行日志")
|
||||||
|
|
||||||
schedule = database.get_schedule_by_id(schedule_id)
|
schedule = database.get_schedule_by_id(schedule_id)
|
||||||
if not schedule:
|
if not schedule:
|
||||||
|
print(f"[API日志] 定时任务#{schedule_id}不存在")
|
||||||
return jsonify({"error": "定时任务不存在"}), 404
|
return jsonify({"error": "定时任务不存在"}), 404
|
||||||
|
|
||||||
if schedule['user_id'] != current_user.id:
|
if schedule['user_id'] != current_user.id:
|
||||||
|
print(f"[API日志] 用户 {current_user.id} 无权访问定时任务#{schedule_id}(属于用户{schedule['user_id']})")
|
||||||
return jsonify({"error": "无权访问"}), 403
|
return jsonify({"error": "无权访问"}), 403
|
||||||
|
|
||||||
limit = request.args.get('limit', 10, type=int)
|
limit = request.args.get('limit', 10, type=int)
|
||||||
|
print(f"[API日志] 开始查询定时任务#{schedule_id}的执行日志,限制{limit}条")
|
||||||
|
|
||||||
logs = database.get_schedule_execution_logs(schedule_id, limit)
|
logs = database.get_schedule_execution_logs(schedule_id, limit)
|
||||||
|
print(f"[API日志] 查询到{len(logs)}条日志")
|
||||||
|
|
||||||
return jsonify(logs)
|
return jsonify(logs)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[API日志] 查询定时任务#{schedule_id}日志时出错: {str(e)}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
return jsonify({"error": f"服务器错误: {str(e)}"}), 500
|
||||||
|
|
||||||
|
|
||||||
# ==================== 批量操作API ====================
|
# ==================== 批量操作API ====================
|
||||||
|
|||||||
Reference in New Issue
Block a user