Add colored logging with shared common.sh library
- Create scripts/common.sh with reusable log() function and color definitions - Refactor backup.sh to use common.sh for consistent logging - Add color support: ERROR (red), WARN (yellow), SUCCESS (green), INFO (normal) - Colors only appear in terminal, plain text in log files - Improve code organization and DRY principle - Fix shellcheck spacing warnings in backup.sh 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
33
scripts/common.sh
Normal file
33
scripts/common.sh
Normal file
@@ -0,0 +1,33 @@
|
||||
# scripts/common.sh - Fonctions communes à tous les scripts
|
||||
|
||||
# Couleurs
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
# Fonction de logging
|
||||
log() {
|
||||
local level="$1"
|
||||
shift
|
||||
local message="$*"
|
||||
local timestamp
|
||||
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
|
||||
local color=""
|
||||
case "$level" in
|
||||
ERROR) color="${RED}" ;;
|
||||
WARN) color="${YELLOW}" ;;
|
||||
SUCCESS) color="${GREEN}" ;;
|
||||
INFO) color="" ;;
|
||||
esac
|
||||
|
||||
echo "[$timestamp] [$level] $message" >>"${LOG_FILE:-/tmp/script.log}"
|
||||
|
||||
if [ -t 1 ]; then
|
||||
echo -e "${color}[$timestamp] [$level] $message${NC}"
|
||||
else
|
||||
echo "[$timestamp] [$level] $message"
|
||||
fi
|
||||
}
|
||||
Reference in New Issue
Block a user