これは、標準エラー ストリームをソーシング スクリプトの標準出力ストリームにマージすることを除けば、うまく機能します。それを修正する方法について何か提案はありますか?
#!/usr/bin/env bash
# Source this from a script to capture and `tee` standard error and standard
# out to a log file. Calling script must source this script. For Example:
#
# . /usr/bin/logy /var/log/project/$0.log
#
# The logging uses >(process substitution). Process substitution is supported
# in shells like bash and zsh but is not supported in sh.
_LOG=${1?log file}
test $# -eq 1 || exit 1
mkdir -p "$(dirname "$_LOG")"
# Append stdout and stderr to log file
exec > >(
echo -e "START\t$(date)" >> "$_LOG"
tee -a "$_LOG"
echo -e "END\t$(date)" >> "$_LOG"
) 2>&1
以下に例を示します。
. /usr/bin/logy $0.log
echo stdout
echo stderr >&2
exit 1
スクリプトを実行します。
$ ./t
$ echo $? # $? is the return value
1
よかった、戻り値 1 が保持されました...
ログに記録された内容
$ cat t.log
START Thu, Feb 07, 2013 2:58:57 PM
stdout
stderr
END Thu, Feb 07, 2013 2:58:57 PM
アイデアは、単一のログ ファイルを作成し、logrotate
それらを維持するために使用することです。
これが問題です。標準出力とエラー ストリームがマージされました。これは、標準エラー ストリームが標準出力に送信されたことを示す出力を出力します。
./t > /dev/null
これは、両方が標準出力に送信されたことを示す echo ステートメントから両方の行を出力します。
./t 2> /dev/null
stdout/err ステートメントのログ ファイルの順序を維持しながら、ストリームを維持する良い方法はありますか? exec
そのため、2 つのステートメントはオプションではないと思います。