Browse Source

feat: 增加交互式日志功能,方便调试

Yin Bin 4 months ago
parent
commit
eda3edc6e5
1 changed files with 59 additions and 22 deletions
  1. 59 22
      script/bin/mylog

+ 59 - 22
script/bin/mylog

@@ -1,26 +1,63 @@
 #!/bin/bash
 
-# log -c 清除日志
-# log -t 即tail -f -n 5000日志
-# log -l 即less 日志
-# log 直接回车或 -h,则打印使用方法
+# 交互式命令行模式
+interactive_mode() {
+    while true; do
+        read -p "Enter command (c: clear log, p: print log, l: less log, t: tail log, q: quit): " cmd
+        case $cmd in
+            c)
+                echo "Clearing log..."
+                > storage/logs/laravel.log
+                ;;
+            p)
+                cat storage/logs/laravel.log
+                ;;
+            l)
+                less storage/logs/laravel.log
+                ;;
+            t)
+                tail -f -n 1000 storage/logs/laravel.log
+                ;;
+            q)
+                exit 0
+                ;;
+            *)
+                echo "Invalid command. Use 'c' to clear, 'p' to print, 'l' to less, 't' to tail, or 'q' to quit."
+                ;;
+        esac
+    done
+}
 
-if [ "$1" = "-c" ]; then
-    echo "clear log..."
-    > storage/logs/laravel.log
-    exit 0
+# 处理命令行参数
+if [ $# -eq 0 ]; then
+    interactive_mode
+else
+    case "$1" in
+        -c)
+            echo "Clearing log..."
+            > storage/logs/laravel.log
+            ;;
+        -p)
+            cat storage/logs/laravel.log
+            ;;
+        -l)
+            less storage/logs/laravel.log
+            ;;
+        -t)
+            tail -f -n 1000 storage/logs/laravel.log
+            ;;
+        -h)
+            echo "Usage: $0 [-c|-p|-l|-t|-h]"
+            echo "  -c: clear log"
+            echo "  -p: print log"
+            echo "  -l: less log"
+            echo "  -t: tail log (last 1000 lines, follow mode)"
+            echo "  -h: show this help message"
+            echo "  No arguments: enter interactive mode"
+            ;;
+        *)
+            echo "Invalid option. Use '$0 -h' for help."
+            exit 1
+            ;;
+    esac
 fi
-
-if [ "$1" = "-t" ]; then
-    tail -f -n 5000 storage/logs/laravel.log
-    exit 0
-fi
-
-if [ "$1" = "-l" ]; then
-    shift
-    less storage/logs/laravel.log
-    exit 0
-fi
-
-echo "usage: log [-c|-t|-l|-h]"
-exit 0