Files
YG_FT_Platform/request/view_logs.sh
DESKTOP-72TV0V4\caoxiaozhu bda8f13446 1. 增加了请求框架
2. 增加了删除虚拟环境的脚本
2026-01-12 14:20:44 +08:00

277 lines
11 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 直接设置项目根目录
PROJECT_ROOT="$(pwd)"
LOGS_DIR="${LOGS_DIR:-$PROJECT_ROOT/logs}"
echo "📋 X-Request 高级日志查看工具"
echo "============================"
TODAY=$(date +%Y-%m-%d)
TODAY_DIR="$LOGS_DIR/$TODAY"
# 检查日志目录是否存在
if [ ! -d "$LOGS_DIR" ]; then
echo "❌ 日志目录不存在: $LOGS_DIR"
echo "💡 请先启动应用生成日志"
exit 1
fi
echo "📁 日志目录: $LOGS_DIR"
echo "📅 今天的日志: $TODAY_DIR"
echo ""
# 显示可用日期目录
echo "📅 可用的日志日期:"
echo "=================="
ls -1 "$LOGS_DIR" | grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" | sort -r
echo ""
# 显示今天的日志文件统计
if [ -d "$TODAY_DIR" ]; then
echo "📄 今天的日志文件:"
echo "=================="
echo "成功日志:"
ls -lh "$TODAY_DIR"/*_success.log 2>/dev/null | awk '{print " " $9 " (" $5 ")"}' || echo " 无成功日志"
echo ""
echo "失败日志:"
ls -lh "$TODAY_DIR"/*_error.log 2>/dev/null | awk '{print " " $9 " (" $5 ")"}' || echo " 无失败日志"
echo ""
else
echo "⚠️ 今天还没有日志文件"
echo ""
fi
# 主循环函数
show_menu_and_process() {
while true; do
echo ""
echo "请选择操作:"
echo "1) 实时监控成功日志"
echo "2) 实时监控失败日志"
echo "3) 搜索今天的日志 (按关键词)"
echo "4) 选择特定日期的日志"
echo "5) 查看日志统计"
echo "6) 清理旧日志 (7天前)"
echo "0) 退出"
echo ""
read -p "请输入选项 (0-6): " choice
case $choice in
1)
if [ ! -d "$TODAY_DIR" ]; then
echo "❌ 今天的日志目录不存在"
continue
fi
# 获取可用路由列表
echo "📋 请选择要监控的路由:"
echo "======================"
success_routes=$(find "$TODAY_DIR" -name "*_success.log" -exec basename {} _success.log \; 2>/dev/null | sort | uniq)
if [ -z "$success_routes" ]; then
echo "❌ 今天没有成功日志的路由"
continue
fi
echo "$success_routes" | nl
echo ""
read -p "请输入路由编号: " route_num
selected_route=$(echo "$success_routes" | sed -n "${route_num}p")
if [ -n "$selected_route" ] && [ -f "$TODAY_DIR/${selected_route}_success.log" ]; then
echo "🔄 实时监控 $selected_route 路由成功日志"
echo "========================================"
echo "💡 提示:监控将持续运行,输入 'x' 并按回车可退出监控"
echo ""
# 在后台启动tail命令
tail -f "$TODAY_DIR/${selected_route}_success.log" &
TAIL_PID=$!
# 等待用户输入x退出
while true; do
read -p "" input
if [ "$input" = "x" ] || [ "$input" = "X" ]; then
kill $TAIL_PID 2>/dev/null
echo ""
echo "✅ 已停止实时监控"
break
fi
done
else
echo "❌ 无效的路由选择"
fi
;;
2)
if [ ! -d "$TODAY_DIR" ]; then
echo "❌ 今天的日志目录不存在"
continue
fi
# 获取可用路由列表
echo "📋 请选择要监控的路由:"
echo "======================"
error_routes=$(find "$TODAY_DIR" -name "*_error.log" -exec basename {} _error.log \; 2>/dev/null | sort | uniq)
if [ -z "$error_routes" ]; then
echo "❌ 今天没有失败日志的路由"
continue
fi
echo "$error_routes" | nl
echo ""
read -p "请输入路由编号: " route_num
selected_route=$(echo "$error_routes" | sed -n "${route_num}p")
if [ -n "$selected_route" ] && [ -f "$TODAY_DIR/${selected_route}_error.log" ]; then
echo "🔄 实时监控 $selected_route 路由失败日志"
echo "========================================"
echo "💡 提示:监控将持续运行,输入 'x' 并按回车可退出监控"
echo ""
# 在后台启动tail命令
tail -f "$TODAY_DIR/${selected_route}_error.log" &
TAIL_PID=$!
# 等待用户输入x退出
while true; do
read -p "" input
if [ "$input" = "x" ] || [ "$input" = "X" ]; then
kill $TAIL_PID 2>/dev/null
echo ""
echo "✅ 已停止实时监控"
break
fi
done
else
echo "❌ 无效的路由选择"
fi
;;
3)
read -p "请输入搜索关键词: " keyword
if [ -z "$keyword" ]; then
echo "❌ 搜索关键词不能为空"
continue
fi
echo "🔍 搜索结果 (关键词: $keyword)"
echo "==================================="
found_any=false
# 使用临时文件来避免子shell问题
temp_result="/tmp/search_result_$$"
> "$temp_result"
# 搜索包含关键词的文件
find "$TODAY_DIR" -name "*.log" -exec grep -l "$keyword" {} \; 2>/dev/null | while read file; do
echo "📄 $file:" >> "$temp_result"
grep "$keyword" "$file" | tail -5 >> "$temp_result"
echo "" >> "$temp_result"
done
# 检查是否有结果
if [ -s "$temp_result" ]; then
cat "$temp_result"
else
echo "❌ 未找到匹配的日志"
fi
# 清理临时文件
rm -f "$temp_result"
;;
4)
echo "📅 选择特定日期:"
echo "=================="
echo "可用的日期:"
ls -1 "$LOGS_DIR" | grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" | sort -r | nl
echo ""
read -p "请输入日期编号 (或按Enter返回): " date_num
if [ -z "$date_num" ]; then
continue
fi
selected_date=$(ls -1 "$LOGS_DIR" | grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" | sort -r | sed -n "${date_num}p")
if [ -n "$selected_date" ] && [ -d "$LOGS_DIR/$selected_date" ]; then
echo ""
echo "📄 $selected_date 的日志文件:"
echo "========================"
ls -la "$LOGS_DIR/$selected_date/" | grep -E "\.log$"
# 询问是否查看该日期的某个文件
echo ""
read -p "是否查看该日期的某个日志文件? (y/N): " view_file
if [[ $view_file == [yY] ]]; then
echo "该日期的日志文件列表:"
ls -1 "$LOGS_DIR/$selected_date/" | grep -E "\.log$" | nl
read -p "请输入文件编号: " file_num
selected_file=$(ls -1 "$LOGS_DIR/$selected_date/" | grep -E "\.log$" | sed -n "${file_num}p")
if [ -f "$LOGS_DIR/$selected_date/$selected_file" ]; then
echo ""
echo "📄 查看文件: $LOGS_DIR/$selected_date/$selected_file"
echo "==========================================="
tail -20 "$LOGS_DIR/$selected_date/$selected_file"
else
echo "❌ 无效的文件选择"
fi
fi
else
echo "❌ 无效的日期选择"
fi
;;
5)
echo "📊 日志统计:"
echo "============"
if [ -d "$TODAY_DIR" ]; then
success_count=$(find "$TODAY_DIR" -name "*_success.log" -exec wc -l {} + 2>/dev/null | tail -1 | awk '{print $1}' || echo "0")
error_count=$(find "$TODAY_DIR" -name "*_error.log" -exec wc -l {} + 2>/dev/null | tail -1 | awk '{print $1}' || echo "0")
echo "今天的日志统计:"
echo " 成功请求: $success_count"
echo " 失败请求: $error_count"
if [ $((success_count + error_count)) -gt 0 ]; then
success_rate=$(echo "scale=2; $success_count * 100 / ($success_count + $error_count)" | bc 2>/dev/null || echo "0")
echo " 成功率: ${success_rate}%"
fi
echo ""
echo "路由统计 (按请求条数):"
find "$TODAY_DIR" -name "*.log" -exec basename {} .log \; | sort | uniq -c | sort -nr
echo ""
echo "文件大小统计:"
find "$TODAY_DIR" -name "*.log" -exec ls -lh {} \; | awk '{print " " $9 " (" $5 ")"}'
else
echo "❌ 今天没有日志文件"
fi
;;
6)
read -p "确认清理7天前的日志? (y/N): " confirm
if [[ $confirm == [yY] ]]; then
echo "🧹 正在清理7天前的日志..."
deleted_dirs=$(find "$LOGS_DIR" -type d -name "????-??-??" -mtime +7 -exec rm -rf {} \; 2>/dev/null | wc -l)
echo "✅ 旧日志清理完成,删除了 $deleted_dirs 个目录"
else
echo "❌ 操作已取消"
fi
;;
0)
echo "👋 退出日志查看工具"
exit 0
;;
*)
echo "❌ 无效选项请输入0-6之间的数字"
;;
esac
# 如果不是退出选项,等待用户按回车继续
if [ "$choice" != "0" ]; then
echo ""
read -p "按 Enter 键继续..." dummy
fi
done
}
# 启动主循环
show_menu_and_process