我有一个非常长的bash脚本,它调用了许多外部命令(git clone
,wget
,apt-get
等),将许多内容打印到标准输出。
我希望脚本具有一些详细的选项,因此它从外部命令中打印出所有内容,例如它的摘要版本(例如"安装依赖关系...","编译..."等,或什么都没有全部。但是我该怎么做,而不会使我所有的代码混乱?
我已经考虑过解决此问题的可能解决方案:一个是创建一个运行外部命令并打印标准输出所需的包装函数,具体取决于开始时设置的选项。这些似乎更容易实现,但这意味着在代码中添加很多额外的混乱。
另一个解决方案是将所有输出发送到几个外部文件,并且在脚本开头解析参数时,如果指定了详细信息,请在该文件上运行tail -f
。这将非常容易实施,但对我来说似乎很骇人听闻,我担心它的性能影响。
哪个更好?我也向其他解决方案开放。
在 @弗雷德的想法上进行改进,我们可以这样构建一个小记录库:
declare -A _log_levels=([FATAL]=0 [ERROR]=1 [WARN]=2 [INFO]=3 [DEBUG]=4 [VERBOSE]=5)
declare -i _log_level=3
set_log_level() {
level="${1:-INFO}"
_log_level="${_log_levels[$level]}"
}
log_execute() {
level=${1:-INFO}
if (( $1 >= ${_log_levels[$level]} )); then
"${@:2}" >/dev/null
else
"${@:2}"
fi
}
log_fatal() { (( _log_level >= ${_log_levels[FATAL]} )) && echo "$(date) FATAL $*"; }
log_error() { (( _log_level >= ${_log_levels[ERROR]} )) && echo "$(date) ERROR $*"; }
log_warning() { (( _log_level >= ${_log_levels[WARNING]} )) && echo "$(date) WARNING $*"; }
log_info() { (( _log_level >= ${_log_levels[INFO]} )) && echo "$(date) INFO $*"; }
log_debug() { (( _log_level >= ${_log_levels[DEBUG]} )) && echo "$(date) DEBUG $*"; }
log_verbose() { (( _log_level >= ${_log_levels[VERBOSE]} )) && echo "$(date) VERBOSE $*"; }
# functions for logging command output
log_debug_file() { (( _log_level >= ${_log_levels[DEBUG]} )) && [[ -f $1 ]] && echo "=== command output start ===" && cat "$1" && echo "=== command output end ==="; }
log_verbose_file() { (( _log_level >= ${_log_levels[VERBOSE]} )) && [[ -f $1 ]] && echo "=== command output start ===" && cat "$1" && echo "=== command output end ==="; }
假设上面的源位于名为logging_lib.sh的库文件中,我们可以在常规的shell脚本中使用它:
#!/bin/bash
source /path/to/lib/logging_lib.sh
set_log_level DEBUG
log_info "Starting the script..."
# method 1 of controlling a command's output based on log level
log_execute INFO date
# method 2 of controlling the output based on log level
date &> date.out
log_debug_file date.out
log_debug "This is a debug statement"
...
log_error "This is an error"
...
log_warning "This is a warning"
...
log_fatal "This is a fatal error"
...
log_verbose "This is a verbose log!"
将导致此输出:
Fri Feb 24 06:48:18 UTC 2017 INFO Starting the script...
Fri Feb 24 06:48:18 UTC 2017
=== command output start ===
Fri Feb 24 06:48:18 UTC 2017
=== command output end ===
Fri Feb 24 06:48:18 UTC 2017 DEBUG This is a debug statement
Fri Feb 24 06:48:18 UTC 2017 ERROR This is an error
Fri Feb 24 06:48:18 UTC 2017 WARNING This is a warning
Fri Feb 24 06:48:18 UTC 2017 FATAL This is a fatal error
正如我们所看到的,log_verbose
没有产生任何输出,因为日志级别在调试中,低于详细的一个级别。但是,log_debug_file date.out
确实产生了输出,log_execute INFO
也是如此,因为日志级别设置为调试,它是> = info。
以此为基础,如果我们需要更多的微调,我们也可以编写命令包装器:
git_wrapper() {
# run git command and print the output based on log level
}
将这些脚本可以增强,以采用一个可以确定其应运行的日志冗长的参数--log-level level
。
这是bash的日志记录的完整实现,有多个记录仪:
https://github.com/codeforester/base/blob/master/lib/stdlib.sh
如果有人好奇某些变量为何在上面的代码中命名为领先的下划线,请参见此帖子:
- 正确的bash和shell脚本变量大写
您已经拥有问题中最干净的想法(包装器功能),但是您似乎认为这会很混乱。我建议您重新考虑。它看起来像以下内容(不一定是一个成熟的解决方案,只是为了给您基本想法):
#!/bin/bash
# Argument 1 : Logging level for that command
# Arguments 2... : Command to execute
# Output suppressed if command level >= current logging level
log()
{
if
(($1 >= logging_level))
then
"${@:2}" >/dev/null 2>&1
else
"${@:2}"
fi
}
logging_level=2
log 1 command1 and its args
log 2 command2 and its args
log 3 command4 and its args
您可以在包装器函数中安排任何必需的重定向(如果需要的话,则使用文件描述符),以便脚本的其余部分保持可读性,并取决于所选的记录级别。p>
解决方案1。考虑使用其他文件描述符。根据选定的详细性,将所需的文件描述符重定向到stdout或/dev/null。将脚本中每个语句的输出重定向到与其重要性相对应的文件描述符。看看https://unix.stackexchange.com/a/218355。
解决方案2。
将您脚本中每个语句的每个语句的$ quilter_verbosity和pipe stdout设置为带有两个参数的辅助脚本,类似的内容:
语句|logger actual_verbosity $ quilter_verbosity
在logger脚本中回波stdin到stdout(或log file,withing),如果$ actual_verbosity> = $ quilter_verbosity。