代码
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
效果