feat: Enhance log inspection and querying capabilities for test environments

This commit is contained in:
2026-07-15 16:13:46 +08:00
parent 5a926e36fc
commit 708d779533
8 changed files with 147 additions and 34 deletions

View File

@@ -5,20 +5,26 @@ skill_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
project_dir="$(cd "${skill_dir}/../../.." && pwd)"
local_log="${project_dir}/.local/logs/backend.log"
ssh_host="root@39.98.44.136"
ssh_port="6022"
remote_log_dir="/opt/service/project/shz-backend/logs"
online_ssh_host="${SHZ_ONLINE_SSH_HOST:-root@39.98.44.136}"
online_ssh_port="${SHZ_ONLINE_SSH_PORT:-6022}"
online_remote_log_dir="${SHZ_ONLINE_LOG_DIR:-/opt/service/project/shz-backend/logs}"
test_ssh_host="${SHZ_TEST_SSH_HOST:-root@47.111.103.66}"
test_ssh_port="${SHZ_TEST_SSH_PORT:-22}"
test_remote_log_dir="${SHZ_TEST_LOG_DIR:-/data/code/shz-backend/logs}"
usage() {
cat <<'EOF'
Usage:
view.sh local [lines] [--follow]
view.sh online [backend|error|info|user] [lines] [--follow]
view.sh online|production [backend|error|info|user] [lines] [--follow]
view.sh test [backend|error|info|user] [lines] [--follow]
Examples:
view.sh local 200
view.sh local 100 --follow
view.sh online backend 200
view.sh test error 200
view.sh online error 500 --follow
EOF
}
@@ -59,11 +65,30 @@ view_local() {
fi
}
view_online() {
local kind="${1:-backend}"
local lines="${2:-200}"
local follow="${3:-}"
local remote_file
view_remote() {
local target="$1"
local kind="${2:-backend}"
local lines="${3:-200}"
local follow="${4:-}"
local ssh_host ssh_port remote_log_dir remote_file
case "${target}" in
online|production)
ssh_host="${online_ssh_host}"
ssh_port="${online_ssh_port}"
remote_log_dir="${online_remote_log_dir}"
;;
test)
ssh_host="${test_ssh_host}"
ssh_port="${test_ssh_port}"
remote_log_dir="${test_remote_log_dir}"
;;
*)
echo "Unknown remote environment: ${target}" >&2
usage >&2
exit 2
;;
esac
case "${kind}" in
backend) remote_file="${remote_log_dir}/backend.log" ;;
@@ -71,7 +96,7 @@ view_online() {
info) remote_file="${remote_log_dir}/sys-info.log" ;;
user) remote_file="${remote_log_dir}/sys-user.log" ;;
*)
echo "Unknown online log type: ${kind}" >&2
echo "Unknown log type: ${kind}" >&2
usage >&2
exit 2
;;
@@ -80,11 +105,16 @@ view_online() {
validate_lines "${lines}"
validate_follow "${follow}"
local -a ssh_args=(-o BatchMode=yes -o ConnectTimeout=10)
if [[ -n "${ssh_port}" ]]; then
ssh_args+=(-p "${ssh_port}")
fi
if [[ "${follow}" == "--follow" ]]; then
ssh -p "${ssh_port}" -o BatchMode=yes -o ConnectTimeout=10 "${ssh_host}" \
ssh "${ssh_args[@]}" "${ssh_host}" \
tail -n "${lines}" -F -- "${remote_file}"
else
ssh -p "${ssh_port}" -o BatchMode=yes -o ConnectTimeout=10 "${ssh_host}" \
ssh "${ssh_args[@]}" "${ssh_host}" \
tail -n "${lines}" -- "${remote_file}"
fi
}
@@ -95,9 +125,9 @@ case "${target}" in
[[ $# -le 3 ]] || { usage >&2; exit 2; }
view_local "${2:-200}" "${3:-}"
;;
online)
online|production|test)
[[ $# -le 4 ]] || { usage >&2; exit 2; }
view_online "${2:-backend}" "${3:-200}" "${4:-}"
view_remote "${target}" "${2:-backend}" "${3:-200}" "${4:-}"
;;
help|-h|--help)
usage