本文主要是介绍shell 终端打印格式化日志 log::info,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码
logging.sh
#!/bin/bash
NC='\033[0m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'log::err() {printf "[$(date +'%Y-%m-%dT%H:%M:%S.%2N%z')][${RED}ERROR${NC}] %b\n" "$@"
}log::info() {printf "[${GREEN}$(date +'%Y-%m-%dT%H:%M:%S.%2N%z')][${YELLOW}INFO]${NC} %b\n" "$*"
}log::warning() {printf "[$(date +'%Y-%m-%dT%H:%M:%S.%2N%z')][${YELLOW}WARNING${NC}] \033[0m%b\n" "$@"
}
test.sh
# 测试语法#!/bin/bash
source logging.shif [[ 2 -gt 1 ]]; thenlog::info "${GREEN} Successful:\t\t\t""2大于1"
elselog::err "${RED} The Error Messages is:\t\t\t""语法错误"
fi
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
log::info "${GREEN} 磁盘使用率:\t\t\t"`df -hT | grep "/$" | awk '{print $6}'`
log::warning "${YELLOW} 内存剩余单位M:\t\t\t"`free -h | grep -i "Mem" |awk '{print $4}'`
if [ ! -d /opt/docker ];then log::err "${RED} 目录不存在:\t\t\t""/opt/docker";fi
效果
这篇关于shell 终端打印格式化日志 log::info的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!