bash logger:找不到将MaxBackupIndex添加到此记录器实现的方法

时间:2013-10-22 13:48:54

标签: linux bash shell variables

我一直在尝试修改此记录器implementation,但我无法让它工作。这是一个示例:

#!/bin/bash

TRUE=1
FALSE=0

############### Added for testing
logger_LOG_ENABLED=$TRUE

logger_rootLogger=$TRACE,f,s
logger_appender_f=file
logger_appender_f_dir=$(pwd)
logger_appender_f_file=test.log
logger_appender_f_roll_format=%Y%m
logger_appender_f_roll=$TRUE
logger_appender_f_maxBackupIndex=10

####################################

logger_abs(){
    if [ "${1:0:1}" == "." ]; then
        builtin echo ${rootDir}/${1}
    else
        builtin echo ${1}
    fi
}

logger_check_app_dir(){

    if [ "$logger_LOG_ENABLED" -eq $TRUE ]; then
        dir=$(logger_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
logger_delete_old_files(){
    ##### Added for testing
    builtin echo "Running logger_delete_old_files $@" >&2
    #####
    if [ "$logger_LOG_ENABLED" -eq $TRUE ] && [ -n "$3" ] && [ "$4" -gt 0 ]; then
        local directory=$(logger_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 "logger_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
logger_filename(){
    builtin echo "Running logger_filename $@" >&2
    local format
    local filename

    logger_check_app_dir "${1}"

    if [ ${3} -eq 1 ];then
        local formatProp=${4}_roll_format
        format=${!formatProp}
        if [ -z ${format} ]; then
            format=$logger_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 echo "logger_filename Stepped into if: $previousFilenameVar => ${!previousFilenameVar}" >&2
            builtin eval export $previousFilenameVar=$filename
            logger_delete_old_files "${1}" "${2}" "${suffix}" "${!maxBackupIndexVar}"
        fi
    else
        filename=${1}/${2}
    fi

    builtin echo $filename
}

######################## Added for testing
filename_caller(){
    builtin echo "filename_caller Call $1"
    output=$(logger_abs $(logger_filename "${logger_appender_f_dir}" "${logger_appender_f_file}" "1" "logger_appender_f" ))

    builtin echo ${output}
}

#### Previous logs generation
for i in {1101..1120}; do
    file="${logger_appender_f_file}.2012${i:2:3}"
    builtin echo "${file} $i"
    touch -m -t "2012${i}0000" ${logger_appender_f_dir}/$file
done

for i in {1..4}; do
    filename_caller $i
done

这是输出

test.log.201201 1101
test.log.201202 1102
test.log.201203 1103
test.log.201204 1104
test.log.201205 1105
test.log.201206 1106
test.log.201207 1107
test.log.201208 1108
test.log.201209 1109
test.log.201210 1110
test.log.201211 1111
test.log.201212 1112
test.log.201213 1113
test.log.201214 1114
test.log.201215 1115
test.log.201216 1116
test.log.201217 1117
test.log.201218 1118
test.log.201219 1119
test.log.201220 1120
filename_caller Call 1
/web/com/138245668210919/test.log.201310
filename_caller Call 2
/web/com/138245668210919/test.log.201310
filename_caller Call 3
/web/com/138245668210919/test.log.201310
filename_caller Call 4
/web/com/138245668210919/test.log.201310
Running logger_filename /web/com/138245668210919 test.log 1 logger_appender_f
logger_filename Stepped into if: int_logger_appender_f_file_previous => 
Running logger_delete_old_files /web/com/138245668210919 test.log .201310 10
logger_delete_old_files About to start deletion /web/com/138245668210919/test.log.201201 /web/com/138245668210919/test.log.201202 /web/com/138245668210919/test.log.201203 /web/com/138245668210919/test.log.201204 /web/com/138245668210919/test.log.201205 /web/com/138245668210919/test.log.201206 /web/com/138245668210919/test.log.201207 /web/com/138245668210919/test.log.201208 /web/com/138245668210919/test.log.201209 /web/com/138245668210919/test.log.201210
Removing file /web/com/138245668210919/test.log.201201
Removing file /web/com/138245668210919/test.log.201202
Removing file /web/com/138245668210919/test.log.201203
Removing file /web/com/138245668210919/test.log.201204
Removing file /web/com/138245668210919/test.log.201205
Removing file /web/com/138245668210919/test.log.201206
Removing file /web/com/138245668210919/test.log.201207
Removing file /web/com/138245668210919/test.log.201208
Removing file /web/com/138245668210919/test.log.201209
Removing file /web/com/138245668210919/test.log.201210
Running logger_filename /web/com/138245668210919 test.log 1 logger_appender_f
logger_filename Stepped into if: int_logger_appender_f_file_previous => 
Running logger_delete_old_files /web/com/138245668210919 test.log .201310 10
logger_delete_old_files About to start deletion 
Running logger_filename /web/com/138245668210919 test.log 1 logger_appender_f
logger_filename Stepped into if: int_logger_appender_f_file_previous => 
Running logger_delete_old_files /web/com/138245668210919 test.log .201310 10
logger_delete_old_files About to start deletion 
Running logger_filename /web/com/138245668210919 test.log 1 logger_appender_f
logger_filename Stepped into if: int_logger_appender_f_file_previous => 
Running logger_delete_old_files /web/com/138245668210919 test.log .201310 10
logger_delete_old_files About to start deletion 

只有当计算出的日志文件名与前一个文件名不同时,我才希望logger_filename函数进入以下if

if [ -n "${!maxBackupIndexVar}" ] && [ "${!previousFilenameVar}" != "${filename}" ]; then

要应用这个场景,我需要正确设置$ {!previousFilenameVar},但情况并非如此,所以logger_filename一直步入此if,这实际上并不是必需的......

看起来问题是由于以下行无法正常工作:

builtin eval export $previousFilenameVar=$filename

我有一些理论来解释原因:

  • 在原始代码中,函数被声明并导出为readonly,这使得它们无法修改全局变量。我在上面的示例中删除了只读声明,但问题仍然存在。
  • 函数调用在$()中执行,这应该使它们运行到分离的shell实例中,因此修改后的变量不会导出到主shell

但我无法找到解决此问题的方法......

感谢任何帮助,提前谢谢!

1 个答案:

答案 0 :(得分:0)

我只是决定改变调用logger_filename函数的方式:

#Appender
# $1 Log directory
# $2 Log file
# $3 Log file roll ?
# $4 Appender Name
# $5 Output filename variable
logger_filename(){
#return result
        local format
        local filename

        logger_check_app_dir "${1}"

        if [ ${3} -eq 1 ];then
                local formatProp=${4}_roll_format
                format=${!formatProp}
                if [ -z ${format} ]; then
                        format=$logger_appender_file_format
                fi
                local suffix=.`date "+${format}"`

                filename=${1}/${2}${suffix}

                # Old log files deletion
                local previousFilenameVar=${4}_file_previous
                local maxBackupIndexVar=${4}_maxBackupIndex

                if [ -n "${!maxBackupIndexVar}" ] && [ "${!previousFilenameVar}" != "${filename}" ]; then
                        eval $previousFilenameVar=$filename
                        logger_delete_old_files "${1}" "${2}" "${suffix}" "${!maxBackupIndexVar}"
                fi
        else
                filename=${1}/${2}
        fi

        #builtin echo $filename
        eval $5=$filename
}

local logFileName=
logger_filename "${logFileDir}" "${logFile}" "${logFileRoll}" "${3}" logFileName
builtin echo "$2" >>"$(logger_abs "${logFileName}")"

这样,该函数不会在子shell中执行,变量${!previousFilenameVar}保留在范围内。