forked from markshust/docker-magento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog
executable file
·128 lines (104 loc) · 4.5 KB
/
log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
LOGS_PATH="/var/www/html/var/log"
CONTAINER_NAME="$(basename "$(pwd)")-phpfpm-1"
echo -e "\033[94mDeveloped by \033[34mYevhen \033[93mZvieriev\033[0m"
function view_log() {
local log_file="$1"
docker exec -it "$CONTAINER_NAME" cat "$LOGS_PATH/$log_file"
}
function show_logs() {
echo -e "\n\033[96mAvailable log files:\033[0m"
docker exec -it "$CONTAINER_NAME" find "$LOGS_PATH" -maxdepth 1 -type f -name "*.log" -exec basename {} \; 2>/dev/null | awk '{print "\033[96m" NR ".\033[0m \033[32m" $0 "\033[0m"}'
}
function search_errors() {
local container_name="$(basename "$(pwd)")-phpfpm-1"
echo -e "\n\033[1mSearching for errors in all log files:\033[0m"
docker cp "$CONTAINER_NAME:$LOGS_PATH" ./ > /dev/null 2>&1
local found_errors=false
for log_file in ./log/*.log; do
local errors=$(grep -E -r --color=always "\(ERROR\|FATAL\|CRITICAL\)" "$log_file" | format_log_output || true)
local warnings=$(grep -E -r --color=always "\bWARNING\b" "$log_file" | format_log_output || true)
local info=$(grep -E -r --color=always "INFO" "$log_file" | format_log_output || true)
local debug=$(grep -E -r --color=always "DEBUG" "$log_file" | format_log_output || true)
if [ -n "$errors" ] || [ -n "$warnings" ] || [ -n "$info" ] || [ -n "$debug" ]; then
found_errors=true
echo -e "\n\033[1;33m----------------------------------------\033[0m"
echo -e "\033[1;33mLog File: \033[32m$(basename "$log_file")\033[0m"
echo -e "$warnings"
echo -e "$errors"
echo -e "$info"
echo -e "$debug" | cat
fi
done
rm -rf ./log
if [ "$found_errors" = false ]; then
echo -e "\n\033[1mNo errors found in any log file.\033[0m"
fi
}
function format_log_output() {
sed -E 's/(ERROR|FATAL|CRITICAL)/\o033[31m\1\o033[0m/g' | sed -E 's/\bWARNING\b/\o033[33m&\o033[0m/g' | sed -E 's/INFO/\o033[32m&\o033[0m/g' | sed -E 's/DEBUG/\o033[36m&\o033[0m/g'
}
function tail_all_logs() {
local logs=("$@")
echo
echo -e "\033[96mTailing logs from ${logs[*]}...\033[0m"
docker exec -it "$CONTAINER_NAME" tail -f "${logs[@]/#/$LOGS_PATH/}" | while IFS= read -r line; do
echo "$line" | format_log_output | tee /dev/tty
done
}
function tail_error_logs() {
echo
local log_file="$1"
docker exec -it "$CONTAINER_NAME" tail -f "$LOGS_PATH/$log_file" | grep -E --line-buffered "(ERROR|FATAL|CRITICAL)" | while IFS= read -r line; do
echo -e "$line" | format_log_output | tee /dev/tty
done
}
if [[ "$1" == "-t" || "$1" == "--tail" ]]; then
shift
for log_file in "$@"; do
tail_all_logs "$log_file"
done
fi
while true; do
echo -e "\n\033[96mChoose an option:\033[0m"
echo -e "\033[96m1.\033[0m View existing log files"
echo -e "\033[96m2.\033[0m Display the content of a specific log file"
echo -e "\033[96m3.\033[0m Search for errors in log files"
echo -e "\033[96m4.\033[0m Tail logs in real-time"
read -p "Enter the option number (1, 2, 3, or 4): " option
if [ "$option" == "1" ]; then
show_logs
elif [ "$option" == "2" ]; then
show_logs
read -p "Enter the log file name (with extension, e.g., system.log): " log_file
view_log "$log_file" | format_log_output
elif [ "$option" == "3" ]; then
search_errors
elif [ "$option" == "4" ]; then
echo -e "\n\033[96mChoose an option:\033[0m"
echo -e "\033[96m1.\033[0m View all logs in real-time"
echo -e "\033[96m2.\033[0m View only error logs in real-time"
read -p "Enter the option number (1 or 2): " tail_option
echo -e "\n\033[96mChoose a log file to tail:\033[0m"
show_logs
read -p "Enter the log file name (with extension, e.g., system.log): " log_file
echo -e "\033[96mPress [Ctrl+C] to stop tailing logs\033[0m"
if [ "$tail_option" == "1" ]; then
tail_all_logs "$log_file"
elif [ "$tail_option" == "2" ]; then
tail_error_logs "$log_file"
else
echo -e "\n\033[91mInvalid choice.\033[0m"
fi
echo -e "\033[96mPress [Enter] to return to the menu\033[0m"
read -r -n 1 -s discard
docker exec -it "$CONTAINER_NAME" pkill -f "tail -f $LOGS_PATH/$log_file"
else
echo -e "\n\033[91mInvalid choice.\033[0m"
break
fi
read -p "$(echo -e "\033[92mContinue (Y/\033[91mn)\033[0m")? " continue_option
if [[ "$continue_option" =~ [Nn] ]]; then
break
fi
done