Chinaunix首页 | 论坛 | 博客
  • 博客访问: 395824
  • 博文数量: 99
  • 博客积分: 5134
  • 博客等级: 大校
  • 技术积分: 1607
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 09:31
文章分类

全部博文(99)

文章存档

2011年(48)

2010年(40)

2009年(10)

2008年(1)

分类: LINUX

2011-01-10 12:20:01

#!/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
阅读(2505) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~