博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
log.sh
阅读量:6883 次
发布时间:2019-06-27

本文共 5345 字,大约阅读时间需要 17 分钟。

#!/bin/echo Warnning, this library must only be sourced!# vim: set expandtab smarttab shiftwidth=4 tabstop=4:## Author: tuantuan.lv 
# Description: a simple log library for pet## Create the log file if not exists# $1: the log file namefunction _create_logfile(){ # FIXME: change the permission of log file if [ ! -f "$1" ]; then mkdir -p `dirname $1`; touch $1; chmod 666 $1 fi}## Prevent the pet log library to be sourced again, so we can protect# the log file to be created only once.#function _prevent_sourced_again(){ _petlog_sourced_="__petlog_sourced_$$__" if [ -n "${!_petlog_sourced_}" ]; then return fi eval "$_petlog_sourced_=1" # Set the default log file path if [ -f "$0" ]; then # Use the script name (not including the extension) _petlog_filename="/tmp/$(basename $0 | awk -F. '{print $1}').log.`date +'%Y%m%d'`" else # Otherwise, just using the default name _petlog_filename="/tmp/petlog.log.`date +'%Y%m%d'`" fi _create_logfile "$_petlog_filename" # The log level, we only print logs whose level less than this _petlog_level=3 # DEBUG # The log data format _petlog_datefmt='%Y-%m-%d %H:%M:%S' # 2014-11-11 11:11:11 # The log line format _petlog_fmt="[
] [
]
" # [DEUBG] [2014-11-11 11:11:11] a simple log}# Print log messages# $1: The log level number# $2: C-style printf format string# $3..$N: C-style printf argumentsfunction _print_log(){ local level=$1 msg="$2" fmt="${_petlog_fmt}" local levelnames=('ERROR' 'WARNING' 'INFO' 'DEBUG') local logcolors=('red' 'yellow' 'green' '') # Prepare the log format strings fmt="${fmt//
/$(date +"$_petlog_datefmt")}" fmt="${fmt//
/$msg}" fmt="${fmt//
/${levelnames[$level]}}" # We also decide to print all the log messages to the log file shift 2 && printf "$fmt" "$@" >> $_petlog_filename # Only print the log whose level less than the log level we set before if [ $level -le $_petlog_level ]; then ${logcolors[level]:-printf} "$fmt" "$@" fi}## Make the strings to be echoed as differnt colors#function red() { printf "\033[1;31m$(echo "$1" | sed -r 's/(\\n)?$/\\033[0m\1/')" "${@:2}"; }function yellow() { printf "\033[1;33m$(echo "$1" | sed -r 's/(\\n)?$/\\033[0m\1/')" "${@:2}"; }function green() { printf "\033[1;32m$(echo "$1" | sed -r 's/(\\n)?$/\\033[0m\1/')" "${@:2}"; }function blue() { printf "\033[1;34m$(echo "$1" | sed -r 's/(\\n)?$/\\033[0m\1/')" "${@:2}"; }function cyan() { printf "\033[1;36m$(echo "$1" | sed -r 's/(\\n)?$/\\033[0m\1/')" "${@:2}"; }function purple() { printf "\033[1;35m$(echo "$1" | sed -r 's/(\\n)?$/\\033[0m\1/')" "${@:2}"; }## Log print functions, such as debug_msg, info_msg... etc.## Define the log level constants, just for convenienceLOG_QUIET=-1 LOG_ERROR=0 LOG_WARNING=1 LOG_INFO=2 LOG_DEBUG=3function debug_msg() { _print_log $LOG_DEBUG "$1" "${@:2}"; }function info_msg() { _print_log $LOG_INFO "$1" "${@:2}"; }function warn_msg() { _print_log $LOG_WARNING "$1" "${@:2}"; }function error_msg() { _print_log $LOG_ERROR "$1" "${@:2}"; return 1; }function die_msg() { error_msg "$@"; exit 1; }function exit_msg() { die_msg "$@"; } # should be deprecated, use die_msg insteadfunction warning_msg() { warn_msg "$@"; } # the same as warn_msg## Customize the log functions## Set the default log file# $1: the log filenamefunction set_logfile(){ if [ -z "$1" ]; then die_msg "the log filename is empty\n" fi _petlog_filename="$1" _create_logfile "$_petlog_filename"}# Get the log filenamefunction get_logfile() { echo "$_petlog_filename"; }# Set the default log level# $1: level name or level numberfunction set_loglevel(){ if echo "$1" | grep -qE "^(-1|0|1|2|3)$"; then _petlog_level="$1" elif echo "$1" | grep -qE '^LOG_(QUIET|DEBUG|INFO|ERROR|WARNING)$'; then _petlog_level="${!1}" else die_msg "the log level is not valid, please check\n" fi}# Set log formatfunction set_logfmt(){ if [ -z "$1" ]; then die_msg "the log format is empty, please check\n" fi _petlog_fmt="$1"}# Set the log date format# $1: the log date format, see `man data`function set_logdatefmt(){ if [ -z "$1" ]; then die_msg "the log data format is empty, please check\n" fi _petlog_datefmt="$1"}_prevent_sourced_again # Yeah, prevent to be sourced again
#!/bin/bash. ./log.shecho "1. use default log settings"echodebug_msg "hello,world\n"info_msg 'hello,world\n'warn_msg 'hello,world\n'warning_msg 'hello,world\n'error_msg 'hello,world\n'gechoecho "2. set loglevel to $LOG_INFO"echoset_loglevel $LOG_INFOdebug_msg "hello,world\n"info_msg 'hello,world\n'warn_msg 'hello,world\n'error_msg 'hello,world\n'echoecho "3. set log fmt to [
-
]
"echoset_logfmt "[
-
]
"info_msg 'hello,%s\n' worldwarn_msg 'hello,%s\n' petechoecho "4. set date fmt to %Y/%m/%d %H:%M:%S"echoset_logdatefmt "%Y/%m/%d %H:%M:%S"info_msg 'hello,world\n'warn_msg 'hello,world\n'echoecho '5. use colorful print'echored "hello,world\n"green "hello,world\n"yellow "hello,world\n"cyan "hello,world\n"purple "hello,world\n"blue "hello,world\n"echoecho '6. exit script'echodie_msg "exit script\n"

转载于:https://www.cnblogs.com/muahao/p/6252447.html

你可能感兴趣的文章
和积式
查看>>
你不能错过.net 并发解决方案
查看>>
[PHP] 超全局变量$_FILES上传文件
查看>>
linux如何添加telnet服务
查看>>
解决Windows对JDK默认版本切换问题
查看>>
HTML5本地存储localStorage与seesionStorage
查看>>
06笨小猴(1.9)
查看>>
UNIX网络编程——原始套接字的魔力【上】
查看>>
web应用开发技术(第二版)崔尚森第八章部分作业
查看>>
thinkCMF----列表页跳转
查看>>
VIM编辑器和VI编辑器的区别
查看>>
hdu 1693 : Eat the Trees 【插头dp 入门】
查看>>
nginx安装与fastdfs配置--阿里云
查看>>
wordpress通过代码禁用IE8, IE9,IE10等IE浏览器兼容视图模式(Compatibility View)
查看>>
This application failed to start because it could not find or load the Qt platform plugin "windows"
查看>>
CSS3展现精彩的动画效果 css3的动画属性
查看>>
JSON+JSONP(加量不加价)
查看>>
windows下安装ubuntu,并用win引导ubuntu启动
查看>>
java开发常用工具
查看>>
在VMware Vcenter添加一块网卡后,启动虚机找不到网卡,发现有一个ens38(redhat7.5)...
查看>>