2018-04-09-shell-log

layouttitledescriptiontagscategory
post
shell logging
linux, shell, logging
linux
linux

2018-04-09-shell-log

在Shell中进行日志的打印

#!/bin/bash
# set LOG_FILEPATH, then log will be printed to the location. e.g., LOG_FILEPATH=./log/cpc_impression_report.log
# e.g., noticelog "first log information"

if [ "$LOG_FILEPATH" != "" ]
then
   _logname=`basename $LOG_FILEPATH `
   _logdir=${LOG_FILEPATH%/*}
   _logpath=$LOG_FILEPATH
else
   _logname=`basename $0`.log
   _logdir=./log && test -d ./logs  && _logdir=./logs
   _logpath=${_logdir}/${_logname}
fi


_logrotate=14
_script=$0
_logpathwf=${_logpath}.wf
_log_initflag=1

_initlog()
{
   if [ ! -d "$_logdir" ]
   then
      _logpath=/dev/stdout && _logpathwf=/dev/stderr
   fi
}

_writelog()
{
   _loglevel=$1
   if [ $1 == "" ] ; then
      _loglevel="INFO"
   fi
   _logmsg="$2"
   if [ "$_logmsg" == "" ] ; then
      _logmsg= "NULL"
   fi

   _logstamp=`date +%Y%m%d-%H:%M:%S`

   if [ "$_loglevel" == "STDOUT" ] ; then
      echo "${_logstamp} ${_loglevel} : ${_logmsg}"
      return
   fi

   if [ ! -d "$_logdir" ]
   then
      if [ "$_loglevel" == "INFO" ] || [ "$_loglevel" == "DEBUG" ]
      then
         echo "${_logstamp} ${_loglevel} : ${_logmsg}"
      else
         echo "${_logstamp} ${_loglevel} : ${_logmsg}" 1>&2
      fi
   else
      if [ "$_loglevel" == "INFO" ] || [ "$_loglevel" == "DEBUG" ]
      then
         echo "${_logstamp} ${_loglevel} : ${_logmsg}" >> ${_logpath}
      else
         echo "${_logstamp} ${_loglevel} : ${_logmsg}" >> ${_logpathwf}
      fi
   fi
}

setlogpath ()
{
   _logpath=$1
   _logname=`basename $_logpath `
   _logdir=${_logpath%/*}
   _logpathwf=${_logpath}.wf

   _initlog
}

stdoutlog ()
{
   _writelog "STDOUT" "$1"
}

debuglog ()
{
   _writelog "DEBUG" "$1"
}

infolog ()
{
   _writelog "INFO" "$1"
}

noticelog ()
{
   _writelog "NOTICE" "$1"
}

warnlog ()
{
   _writelog "WARNING" "$1"
}

errorlog ()
{
   _writelog "ERROR" "$1"
}

fatallog ()
{
   _writelog "FATAL" "$1"
}
criticallog ()
{
   _writelog "CRITICAL" "$1"
}

_initlog
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值