自定义错误日志功能脚本

时间:2018-03-01 06:27:05

标签: shell ksh

#!/bin/ksh
DateForFileName=`date +%Y-%m-%d-%H-%M-%S`
DateTimeForLog=$(date +"%m/%d/%Y %l:%M %p")
StdOutPutlogFile='/tmp/Suganya/LofFileCheck'
StdErrorLogFile='/tmp/Suganya/LofFileCheckError'
ScriptName=$(basename $0 | cut -d'.' -f1)
#function to capture common error logs with timestamp
OutputLog()
 {
  read IN
  echo $DateTimeForLog-$ScriptName-"Information"-$IN >> $StdOutPutlogFile
 }
errorLog()
{
 read IN
 echo "error"
 echo $DateTimeForLog-$ScriptName-"Error"-$IN >> $StdErrorLogFile
 }
Customoutput()
 {
 echo $DateTimeForLog-$ScriptName-"Information"-$1 >> $StdOutPutlogFile
 }
#######set of commands#########
{
echo 'started'
ls -la
cd /tmp/kjhdakdha
ls -la
} 2> errorLog 1> OutputLog

我正在尝试使用针对该组命令的自定义函数单独捕获错误日志和输出日志。但实际上并没有调用userdefined函数。相反,它正在创建文件" errorLog"和" outputLog"。请帮助调用该命令集的函数来捕获日志。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您需要使用参数调用该函数,以便它可以相应地获取错误日志并根据其中的命令进行打印,请参阅下面的更正,

    #!/bin/ksh
    DateForFileName=`date +%Y-%m-%d-%H-%M-%S`
    DateTimeForLog=$(date +"%m/%d/%Y %l:%M %p")
    StdOutPutlogFile='/tmp/Suganya/LofFileCheck'
    StdErrorLogFile='/tmp/Suganya/LofFileCheckError'
    ScriptName=$(basename $0 | cut -d'.' -f1)
    #function to capture common error logs with timestamp
    OutputLog()
     {
      echo $DateTimeForLog-$ScriptName-"Information"-$1>> $StdOutPutlogFile
     }
    errorLog()
    {
     echo "error"
     echo $DateTimeForLog-$ScriptName-"Error"-$1>> $StdErrorLogFile
     }
    Customoutput()
     {
     echo $DateTimeForLog-$ScriptName-"Information"-$1 >> $StdOutPutlogFile
     }
    #######set of commands#########
    {
    echo 'started'
    ls -la
    cd /tmp/kjhdakdha
    ls -la
    } 2> errorLog.txt 1> OutputLog.txt
    if [ -s errorLog.txt ];then
        LE=`cat errorLog.txt`
        errorLog "$LE"
    fi
    if [ -s OutputLog.txt ];then
        LO=`cat OutputLog`
        OutputLog "$LO"
    fi
rm errorLog.txt OutputLog.txt