bash 和 stdout 中的日志功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3524978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:36:14  来源:igfitidea点击:

Logging functions in bash and stdout

bashloggingstdout

提问by Adam

I'd like to be able to put log messages in the middle of bash functions, without affecting the output of those very functions. For example, consider the following functions log()and get_animals():

我希望能够将日志消息放在 bash 函数的中间,而不会影响这些函数的输出。例如,考虑以下函数log()get_animals()

# print a log a message
log ()
{
    echo "Log message: "
}

get_animals()
{
    log "Fetching animals"
    echo "cat dog mouse"
}

values=`get_animals`
echo $values

After which $valuescontains the string "Log message: Fetching animals cat dog mouse".

之后$values包含字符串"Log message: Fetching animals cat dog mouse"

How should I modify this script so that "Log message: Fetching animals"is outputted to the terminal, and $valuescontains "cat dog mouse"?

我应该如何修改这个脚本,以便"Log message: Fetching animals"输出到终端,并$values包含"cat dog mouse"

采纳答案by Peter Tillemans

You can redirect the output to the sdterr error file on file handle 2 using >&2

您可以使用 >&2 将输出重定向到文件句柄 2 上的 sdterr 错误文件

example :

例子 :

# print a log a message
log ()
{
echo "Log message: " >&2
}

get_animals()
{
log "Fetching animals"
echo "cat dog mouse"
}

values=`get_animals`
echo $values

the `` only take the output on stdout, not on stderr. The console on the other hand displays both.

`` 只在 stdout 上输出,而不在 stderr 上输出。另一方面,控制台显示两者。

If you reallywant the Log message on the stdout you can redirect error back to stdout after assigning to the variable :

如果您真的想要标准输出上的日志消息,您可以在分配给变量后将错误重定向回标准输出:

# print a log a message
log ()
{
    echo "Log message: " >&2
}

get_animals()
{
    log "Fetching animals"
    echo "cat dog mouse"
}

values=`get_animals` 2>&1
echo $values

回答by David T. Pierson

choroba's solution to another questionshows how to use exec to open a new file descriptor.

choroba 对另一个问题的解决方案展示了如何使用 exec 打开一个新的文件描述符。

Translating that solution to this question gives something like:

将这个问题的解决方案翻译成这样:

# Open a new file descriptor that redirects to stdout:
exec 3>&1

log ()
{
    echo "Log message: " 1>&3
}

get_animals()
{
    log "Fetching animals"
    echo "cat dog mouse"
}

animals=`get_animals`
echo Animals: $animals

Executing the above produces:

执行上述产生:

Log message: Fetching animals
Animals: cat dog mouse

More information about using I/O redirection and file descriptors in Bash can be found at:

有关在 Bash 中使用 I/O 重定向和文件描述符的更多信息,请访问:

回答by Yordan Georgiev

    #
    #------------------------------------------------------------------------------
    # echo pass params and print them to a log file and terminal
    # with timestamp and $host_name and 
log()
{
    echo 1>&2 "Log message: "
}
PID # usage: # doLog "INFO some info message" # doLog "DEBUG some debug message" # doLog "WARN some warning message" # doLog "ERROR some really ERROR message" # doLog "FATAL some really fatal message" #------------------------------------------------------------------------------ doLog(){ type_of_msg=$(echo $*|cut -d" " -f1) msg=$(echo "$*"|cut -d" " -f2-) [[ $type_of_msg == DEBUG ]] && [[ $do_print_debug_msgs -ne 1 ]] && return [[ $type_of_msg == INFO ]] && type_of_msg="INFO " # one space for aligning [[ $type_of_msg == WARN ]] && type_of_msg="WARN " # as well # print to the terminal if we have one test -t 1 && echo " [$type_of_msg] `date "+%Y.%m.%d-%H:%M:%S %Z"` [$run_unit][@$host_name] [$$] ""$msg" # define default log file none specified in cnf file test -z $log_file && \ mkdir -p $product_instance_dir/dat/log/bash && \ log_file="$product_instance_dir/dat/log/bash/$run_unit.`date "+%Y%m"`.log" echo " [$type_of_msg] `date "+%Y.%m.%d-%H:%M:%S %Z"` [$run_unit][@$host_name] [$$] ""$msg" >> $log_file } #eof func doLog

回答by Steve Emmerson

You could redirect log output to the standard error stream:

您可以将日志输出重定向到标准错误流:

##代码##