0

このロガーの実装を変更しようとしていますが、うまく機能させることができません。サンプルは次のとおりです。

#!/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 

if計算されたログ ファイル名が前のものと異なる場合にのみ、logger_filename 関数が次のステップに進むことを期待します。

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

このシナリオを適用するには、 ${!previousFilenameVar} を正しく設定する必要がありますが、そうではないため、logger_filename は常にこれに介入しますが、ifこれは実際には必要ありません...

この問題は、次の行が正しく機能していないことが原因のようです。

builtin eval export $previousFilenameVar=$filename

理由を説明するいくつかの理論があります。

  • 元のコードでは、関数は読み取り専用として宣言およびエクスポートされるため、グローバル変数を変更できません。上記のサンプルで readonly 宣言を削除しましたが、問題は解決しません。
  • 関数呼び出しは $() で実行され、個別のシェル インスタンスで実行されるため、変更された変数はメイン シェルにエクスポートされません。

しかし、この問題の回避策を見つけることができません...

どんな助けでも大歓迎です、事前に感謝します!

4

1 に答える 1