fix: enhance logs.sh to support tailing logs with options and improve usage instructions

This commit is contained in:
duthink 2025-11-15 17:13:29 +05:30
parent ef9bad88bf
commit d09023ceea

View file

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# View ERPNext Logs Script # View ERPNext Logs Script
# Usage: ./logs.sh [service-number-or-name] # Usage: ./logs.sh [service-number-or-name] [--tail[=N]]
# Colors # Colors
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m' RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
@ -13,21 +13,37 @@ echo_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Navigate to production directory # Navigate to production directory
cd "$(dirname "$(dirname "${BASH_SOURCE[0]}")")" || exit 1 cd "$(dirname "$(dirname "${BASH_SOURCE[0]}")")" || exit 1
# Show menu only if no argument provided FOLLOW_MODE="follow"
if [ -z "$1" ]; then TAIL_LINES=200
echo_info "Available services:" SERVICE_ARG=""
echo " 1. backend 2. frontend 3. websocket"
echo " 4. queue-short 5. queue-long 6. scheduler 7. all"
read -p "Enter number or name: " INPUT
else
INPUT=$1
fi
# Map input to service name while [[ $# -gt 0 ]]; do
case "$INPUT" in case "$1" in
--tail)
FOLLOW_MODE="tail"
if [[ "${2:-}" =~ ^[0-9]+$ ]]; then
TAIL_LINES=$2
shift 2
continue
fi
shift
;;
--tail=*)
FOLLOW_MODE="tail"
VALUE="${1#*=}"
[[ "$VALUE" =~ ^[0-9]+$ ]] || { echo_error "--tail expects an integer"; exit 1; }
TAIL_LINES=$VALUE
shift
;;
--lines)
[[ "${2:-}" =~ ^[0-9]+$ ]] || { echo_error "--lines expects an integer"; exit 1; }
TAIL_LINES=$2
FOLLOW_MODE="tail"
shift 2
;;
-h|--help) -h|--help)
cat << EOF cat << EOF
Usage: $0 [service-number-or-name] Usage: $0 [service-number-or-name] [--tail[=N]]
Services: Services:
1 or backend - Gunicorn backend 1 or backend - Gunicorn backend
@ -38,14 +54,41 @@ Services:
6 or scheduler - Background scheduler 6 or scheduler - Background scheduler
7 or all - All services 7 or all - All services
Flags:
--tail[=N] Show the last N (default 200) log lines and exit
--lines N Alias for --tail=N
-h, --help Show this help
Examples: Examples:
$0 # Interactive menu $0 # Interactive menu (follow)
$0 1 # View backend logs $0 backend # Follow backend logs
$0 backend # Same as above $0 backend --tail 50 # Show last 50 backend log lines
$0 all # View all logs $0 --tail # Show last 200 lines for all services
EOF EOF
exit 0 exit 0
;; ;;
*)
SERVICE_ARG="$1"
shift
;;
esac
done
if [[ -z "$SERVICE_ARG" ]]; then
if [[ "$FOLLOW_MODE" == "tail" ]]; then
INPUT="all"
else
echo_info "Available services:"
echo " 1. backend 2. frontend 3. websocket"
echo " 4. queue-short 5. queue-long 6. scheduler 7. all"
read -p "Enter number or name: " INPUT
fi
else
INPUT="$SERVICE_ARG"
fi
# Map input to service name
case "$INPUT" in
1|backend) SERVICE="backend" ;; 1|backend) SERVICE="backend" ;;
2|frontend) SERVICE="frontend" ;; 2|frontend) SERVICE="frontend" ;;
3|websocket) SERVICE="websocket" ;; 3|websocket) SERVICE="websocket" ;;
@ -60,6 +103,11 @@ esac
docker ps | grep -q "erpnext-production" || { echo_error "ERPNext is not running!"; exit 1; } docker ps | grep -q "erpnext-production" || { echo_error "ERPNext is not running!"; exit 1; }
# Show logs # Show logs
echo_info "Logs for: $SERVICE (Ctrl+C to exit)" echo_info "Logs for: $SERVICE"
[ "$SERVICE" = "all" ] && SERVICE="" [ "$SERVICE" = "all" ] && SERVICE=""
if [[ "$FOLLOW_MODE" == "follow" ]]; then
echo_info "Streaming (Ctrl+C to exit)"
docker compose --project-name erpnext-production -f production.yaml logs -f $SERVICE docker compose --project-name erpnext-production -f production.yaml logs -f $SERVICE
else
docker compose --project-name erpnext-production -f production.yaml logs --tail "$TAIL_LINES" $SERVICE
fi