log4bash: Cannot find a way to add MaxBackupIndex to this logger implementation
Posted
by
Syffys
on Stack Overflow
See other posts from Stack Overflow
or by Syffys
Published on 2013-10-22T13:48:54Z
Indexed on
2013/10/22
15:54 UTC
Read the original article
Hit count: 233
I have been trying to modify this log4bash implementation but I cannot manage to make it work. Here's a sample:
#!/bin/bash
TRUE=1
FALSE=0
############### Added for testing
log4bash_LOG_ENABLED=$TRUE
log4bash_rootLogger=$TRACE,f,s
log4bash_appender_f=file
log4bash_appender_f_dir=$(pwd)
log4bash_appender_f_file=test.log
log4bash_appender_f_roll_format=%Y%m
log4bash_appender_f_roll=$TRUE
log4bash_appender_f_maxBackupIndex=10
####################################
log4bash_abs(){
if [ "${1:0:1}" == "." ]; then
builtin echo ${rootDir}/${1}
else
builtin echo ${1}
fi
}
log4bash_check_app_dir(){
if [ "$log4bash_LOG_ENABLED" -eq $TRUE ]; then
dir=$(log4bash_abs $1)
if [ ! -d ${dir} ]; then
#log a seperation line
mkdir $dir
fi
fi
}
# Delete old log files
# $1 Log directory
# $2 Log filename
# $3 Log filename suffix
# $4 Max backup index
log4bash_delete_old_files(){
##### Added for testing
builtin echo "Running log4bash_delete_old_files $@" >&2
#####
if [ "$log4bash_LOG_ENABLED" -eq $TRUE ] && [ -n "$3" ] && [ "$4" -gt 0 ]; then
local directory=$(log4bash_abs $1)
local filename=$2
local maxBackupIndex=$4
local suffix=$(echo "${3}" | sed -re 's/[^.]/?/g')
local logFileList=$(find "${directory}" -mindepth 1 -maxdepth 1 -name "${filename}${suffix}" -type f | xargs ls -1rt)
local fileCnt=$(builtin echo -e "${logFileList}" | wc -l)
local fileToDeleteCnt=$(($fileCnt-$maxBackupIndex))
local fileToDelete=($(builtin echo -e "${logFileList}" | head -n "${fileToDeleteCnt}" | sed ':a;N;$!ba;s/\n/ /g'))
##### Added for testing
builtin echo "log4bash_delete_old_files About to start deletion ${fileToDelete[@]}" >&2
#####
if [ ${fileToDeleteCnt} -gt 0 ]; then
for f in "${fileToDelete[@]}"; do
#### Added for testing
builtin echo "Removing file ${f}" >&2
####
builtin eval rm -f ${f}
done
fi
fi
}
#Appender
# $1 Log directory
# $2 Log file
# $3 Log file roll ?
# $4 Appender Name
log4bash_filename(){
builtin echo "Running log4bash_filename $@" >&2
local format
local filename
log4bash_check_app_dir "${1}"
if [ ${3} -eq 1 ];then
local formatProp=${4}_roll_format
format=${!formatProp}
if [ -z ${format} ]; then
format=$log4bash_appender_file_format
fi
local suffix=.`date "+${format}"`
filename=${1}/${2}${suffix}
# Old log files deletion
local previousFilenameVar=int_${4}_file_previous
local maxBackupIndexVar=${4}_maxBackupIndex
if [ -n "${!maxBackupIndexVar}" ] && [ "${!previousFilenameVar}" != "${filename}" ]; then
builtin eval export $previousFilenameVar=$filename
log4bash_delete_old_files "${1}" "${2}" "${suffix}" "${!maxBackupIndexVar}"
else
builtin echo "log4bash_filename $previousFilenameVar => ${!previousFilenameVar}"
fi
else
filename=${1}/${2}
fi
builtin echo $filename
}
######################## Added for testing
filename_caller(){
builtin echo "filename_caller Call $1"
output=$(log4bash_abs $(log4bash_filename "${log4bash_appender_f_dir}" "${log4bash_appender_f_file}" "1" "log4bash_appender_f" ))
builtin echo ${output}
}
#### Previous logs generation
for i in {1101..1120}; do
file="${log4bash_appender_f_file}.2012${i:2:3}"
builtin echo "${file} $i"
touch -m -t "2012${i}0000" ${log4bash_appender_f_dir}/$file
done
for i in {1..4}; do
filename_caller $i
done
I expect log4bash_filename function to step into the following if
only when the calculated log filename is different from the previous one:
if [ -n "${!maxBackupIndexVar}" ] && [ "${!previousFilenameVar}" != "${filename}" ]; then
For this scenario to apply, I'd need ${!previousFilenameVar} to be correctly set, but it's not the case, so log4bash_filename steps into this if
all the time which is really not necessary...
It looks like the issue is due to the following line not working properly:
builtin eval export $previousFilenameVar=$filename
I have a some theories to explain why:
- in the original code, functions are declared and exported as readonly which makes them unable to modify global variable. I removed readonly declarations in the above sample, but probleme persists.
- Function calls are performed in $() which should make them run into seperated shell instances so variable modified are not exported to the main shell
But I cannot manage to find a workaround to this issue...
Any help is appreciated, thanks in advance!
© Stack Overflow or respective owner