From 501d29cad74aae2630c62b28d88cc32814a40112 Mon Sep 17 00:00:00 2001 From: Thuan Bui Date: Sat, 4 Jan 2025 19:47:31 +0900 Subject: [PATCH] Add logging function with improved output formatting - Introduced a log() function to capture console output to a file - Added date separator and blank lines for better readability - Stripped ANSI color codes to avoid strange characters in the log --- .gitignore | 3 ++- kazewp.sh | 4 ++++ lib/colors.sh | 17 +++++++++++++---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 92fafc5..ef8aea6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ caddy/* -wordpress/* \ No newline at end of file +wordpress/* +kazewp.log \ No newline at end of file diff --git a/kazewp.sh b/kazewp.sh index eef8856..f1a439d 100755 --- a/kazewp.sh +++ b/kazewp.sh @@ -1,5 +1,9 @@ #!/bin/bash +LOG_FILE="kazewp.log" +exec > >(tee -a "$LOG_FILE") 2>&1 +echo -e "\n========== $(date '+%Y-%m-%d %H:%M:%S') ==========" + # Get the directory where the script is located SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" diff --git a/lib/colors.sh b/lib/colors.sh index 8cc32cb..563fc88 100644 --- a/lib/colors.sh +++ b/lib/colors.sh @@ -1,4 +1,13 @@ -RED='\033[0;31m' -GREEN='\033[0;32m' -BLUE='\033[0;34m' -NC='\033[0m' # No Color \ No newline at end of file +if [ -t 1 ]; then + # Terminal supports colors + NC='\033[0m' # No Color + RED='\033[0;31m' + GREEN='\033[0;32m' + BLUE='\033[0;34m' +else + # Disable colors for non-terminal outputs (like logs) + NC='' + RED='' + GREEN='' + BLUE='' +fi \ No newline at end of file