Files
2026-04-07 14:11:45 +08:00

36 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# ///
# query_logs.sh
# 描述:查询最近日志
# 作者:AI Generated
# 创建日期:2026-04-06
#
# 使用方式:
# ./query_logs.sh # 最近 50 条
# ./query_logs.sh 100 # 最近 100 条
# ./query_logs.sh 50 error # 最近 50 条 error 日志
# ///
DB_HOST=${DB_HOST:-localhost}
DB_PORT=${DB_PORT:-5432}
DB_NAME=${DB_NAME:-wxauto}
DB_USER=${DB_USER:-wxauto}
DB_PASS=${DB_PASS:-wxauto_password}
export PGPASSWORD="$DB_PASS"
LIMIT=${1:-50}
LEVEL_FILTER=${2:-}
if [ -n "$LEVEL_FILTER" ]; then
echo "=== 最近 ${LIMIT}${LEVEL_FILTER} 日志 ==="
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c \
"SELECT id, to_char(timestamp, 'YYYY-MM-DD HH24:MI:SS') as time, level, source, message \
FROM logs WHERE level = '$LEVEL_FILTER' ORDER BY timestamp DESC LIMIT $LIMIT;"
else
echo "=== 最近 ${LIMIT} 条日志 ==="
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c \
"SELECT id, to_char(timestamp, 'YYYY-MM-DD HH24:MI:SS') as time, level, source, message \
FROM logs ORDER BY timestamp DESC LIMIT $LIMIT;"
fi