# cat echoLog.sh
######################################################################
#!/bin/bash
# A shell subroutine to echo log message to screen and a log file.
LOG_FILE="/var/log/install.log"
DATE_FORMAT=`date "+%Y-%m-%d %H:%M:%S"`
logInfo() {
local infoMsg="$*"
echo [Info] [line: `caller 0 |awk '{print $1}'` ] $DATE_FORMAT $infoMsg
echo [Info] [line: `caller 0 |awk '{print $1}'` ] $DATE_FORMAT $infoMsg >> $LOG_FILE 2>&1
}
logWarn() {
local infoMsg="$*"
echo [Warn] [line: `caller 0 |awk '{print $1}'` ] $DATE_FORMAT $infoMsg
echo [Warn] [line: `caller 0 |awk '{print $1}'` ] $DATE_FORMAT $infoMsg >> $LOG_FILE 2>&1
}
logError() {
local infoMsg="$*"
echo [Error] [line: `caller 0 |awk '{print $1}'` ] $DATE_FORMAT $infoMsg
echo [Error] [line: `caller 0 |awk '{print $1}'` ] $DATE_FORMAT $infoMsg >> $LOG_FILE 2>&1
exit 1
}
logInfo "It is an info level log."
logWarn "It is a warn level log."
logError "It is an error level log."
########################################################################
Shell脚本的模块化和脚本复用
https://blog.51cto.com/atong/1912179
simpleLog4sh,简单的shell日志框架
https://github.com/maoshuai/simpleLog4sh
How can I log to a specific file in linux using logger command?
https://stackoverflow.com/questions/13423303/how-can-i-log-to-a-specific-file-in-linux-using-logger-command
How can I fully log all bash scripts actions?
https://serverfault.com/questions/103501/how-can-i-fully-log-all-bash-scripts-actions
shell脚本实现分日志级别输出
https://www.linuxidc.com/Linux/2017-02/140381.htm
How can I log to a specific file in linux using logger command?
https://stackoverflow.com/questions/13423303/how-can-i-log-to-a-specific-file-in-linux-using-logger-command