2011-03-19 45 views
0

我有一种情况,我想将错误和日志消息发送到同一个文件。到错误之间区分登录我追加这样的日志消息消息:正在修改错误和日志消息

文件应该是这样的:

===============log_and_error_msg.txt ========= 
ERR: This message is an error message 
INF: This is a log message 

,使任何人感兴趣的错误消息可以grep "ERR" log_and_error_msg.txt

想我执行一些这样的shell脚本

./shellscript 2>>log_and_error_msg.txt 1>>log_and_error_msg.txt 

如何在飞行中将ERR和INF添加到每个消息上?

回答

1

您可以使用sed。插入sed 's/^/TYPE /'在每个管道,与ERR:INF:更换TYPE

0

尝试将stderr重定向到临时文件。

./testscript.sh 2> err.tmp | (while read line ; do echo 'INFO: ' $line ; done) ; (while read line ; do echo 'ERR: ' $line ; done) <err.tmp 
2
#!/bin/bash 
exec 3> >(sed 's/^/INF: /' >> prepend.log) 
exec 4> >(sed 's/^/ERR: /' >> prepend.log) 
echo "some information" >&3 
echo "an error" >&4 
echo "more information" >&3 
echo "this goes to the screen" 
echo "this goes to stderr, bypassing the log" >&2 
echo "another error" >&4 
echo "yet more information" >&3 
echo "still information" >&3 
echo "oops" >&4 
+1

的'exec'重定向和进程替换+1好的示范 – 2011-03-19 15:07:00

0
 
{ { ./shellscript | sed 's/^/INF: /'; } 2>&1 1>&3 | sed 's/^/ERR: /'; } \ 
>> log_and_error_message.txt 3>&1