#!/bin/sh
_LOG_LEVEL="_info_"
function logit(){
_debug_=10
_info_=20
_notice_=30
_warn_=40
_error_=50
_crit_=60
_F_LOG_LEVEL=$1
##enter the log name or number ,convert it to log level number or name
function get_loglevel(){
_TMP_LOG_LEVEL=$1
case ${_TMP_LOG_LEVEL} in
_debug_ | 10 )
_TMP_LOG_LEVEL_NUM=10
_TMP_LOG_LEVEL_NAME=_debug_;;
_info_ | 20 )
_TMP_LOG_LEVEL_NUM=20
_TMP_LOG_LEVEL_NAME=_info_;;
_notice_ | 30 )
_TMP_LOG_LEVEL_NUM=30
_TMP_LOG_LEVEL_NAME=_notice_;;
_warn_ | 40)
_TMP_LOG_LEVEL_NUM=40
_TMP_LOG_LEVEL_NAME=_warn_;;
_error_ | 50 )
_TMP_LOG_LEVEL_NUM=50
_TMP_LOG_LEVEL_NAME=_error_;;
_crit_ | 60 )
_TMP_LOG_LEVEL_NUM=60
_TMP_LOG_LEVEL_NAME=_crit_;;
*)
_TMP_LOG_LEVEL_NUM=255
_TMP_LOG_LEVEL_NAME=wronglevel
echo "arguments error"
exit 255;;
esac
echo ${_TMP_LOG_LEVEL_NUM} ${_TMP_LOG_LEVEL_NAME}
}
#convert
_F_LOG_LEVEL_NUM=`get_loglevel ${_F_LOG_LEVEL} | awk '{print $1}'`
_F_LOG_LEVEL_NAME=`get_loglevel ${_F_LOG_LEVEL} | awk '{print $2}'`
_LOG_LEVEL_NUM=`get_loglevel ${_LOG_LEVEL} | awk '{print $1}'`
_LOG_LEVEL_NAME=`get_loglevel ${_LOG_LEVEL} | awk '{print $2}'`
#decide whether to dispaly it or not
#if the global _LOG_LEVEL config level is less then user configed level
#then display it
if [ "${_LOG_LEVEL_NUM}" -le "${_F_LOG_LEVEL_NUM}" ]
then
#define the output prefix format here.
echo -n `date +%F" "%H:%M` LOG_LEVEL[${_F_LOG_LEVEL_NAME}]:" "
echo "$2"
#we can execute $2 as a function directly.
# $2
fi
}
function help(){
#use $FUNCNAME to show the function name in the output,so we can know that who
#output the message
logit _crit_ "function[${FUNCNAME}]: help msg"
}
logit _debug_ "ls -l"
logit 20 "ls -l"
logit _crit_ "crit msg"
help
阅读(2518) | 评论(0) | 转发(0) |