shell终端日志的规范打印

1. 先上简单代码

function echo_log(){
  color=$1
  msg=$2
  current=`date "+%Y-%m-%d %H:%M:%S"`
  if [[ $color == "error" ]]; then
    echo -e "\033[31m [$current]|${BASH_SOURCE}|${LINENO}|${FUNCNAME[0]}|error|$msg \033[0m"
  else
    echo -e "\033[32m [$current]|${BASH_SOURCE}|${LINENO}|${FUNCNAME[0]}|info|$msg \033[0m"
  fi
}

2. 使用方法

  # scripts parent path
  PROJECT_DIR=$(cd $(dirname $0); pwd)
SCENARIO_FILE="test.xml"
SCENARIO_FILE="${PROJECT_DIR}/${SCENARIO_FILE}"
if [[ ! -f $SCENARIO_FILE ]];then
    echo_log error "file not exist!“
else
    echo_log info "file exist!"
fi

 

posted @ 2021-07-13 16:11  代码诠释的世界  阅读(211)  评论(0编辑  收藏  举报