improve log system

This commit is contained in:
2026-02-15 17:42:42 +00:00
parent 64b197cbe5
commit 456262ca65
5 changed files with 216 additions and 52 deletions
+31 -2
View File
@@ -13,10 +13,35 @@ GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
NC='\033[0m' # No Color
MAX_LOG_SIZE_KB=1024 # 1MB
# Function to log messages
# Função de Log (modificada para usar logger)
log() {
echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
local message="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# Escreve no ficheiro de log
echo "$timestamp: $message" | tee -a "$LOG_FILE"
# Envia para o syslog (opcional)
logger -t "debian_upgrade" "$message"
}
# Função para limitar o tamanho do log
limit_log_size() {
local log_file="$1"
local max_size_kb="$2"
local max_size_bytes=$((max_size_kb * 1024))
if [ -f "$log_file" ]; then
local current_size=$(stat -c %s "$log_file" 2>/dev/null || wc -c < "$log_file" 2>/dev/null)
if [ "$current_size" -gt "$max_size_bytes" ]; then
log "Aviso: Ficheiro de log $log_file excedeu $max_size_kb KB. A truncar..."
tail -n 500 "$log_file" > "${log_file}.tmp" && mv "${log_file}.tmp" "$log_file"
log "Ficheiro de log truncado. As últimas 500 linhas foram mantidas."
fi
fi
}
# Function to ask for confirmation
@@ -190,6 +215,10 @@ main() {
exit 1
fi
mkdir -p "$(dirname "$LOG_FILE")"
limit_log_size "$LOG_FILE" "$MAX_LOG_SIZE_KB" # Verifica o tamanho do log no início
# Check if this is a Debian system
if [ ! -f /etc/debian_version ]; then
log "${RED}This script is only for Debian-based systems.${NC}"