我使用tail和awk在我的日志文件上进行语法高亮显示。
这是我的荧光笔: 的 tailError.sh
tail -f error.log | awk '
/INFO/ {print "\033[1;37m" $0 "\033[39m"}
/ERROR/ {print "\033[31m" $0 "\033[39m"}
/WARN/ {print "\033[33m" $0 "\033[39m"}
/DEBUG/ {print "\033[0;37m" $0 "\033[39m"}
/at com.adobe.training/ {print "\033[31m" $0 "\033[39m"}
'
这非常接近 https://unix.stackexchange.com/questions/8414/how-to-have-tail-f-show-colored-output
我们说,这是我的日志文件( error.log ):
*INFO* Really?
*WARN* Take care
*ERROR* Doh!
Exception in thread "main" NullPointerException
at com.adobe.training.methodWithNPE (MyClass.java:123)
at somewhere.else (Servlet.java:234) --- This line should be printed, too --
--- This line and the next one are missing. ---
--- How do I output these lines? ---
如您所见,我的日志文件中的这些行丢失了:
at somewhere.else (Servlet.java:234) --- This line should be printed, too --
--- This line and the next one are missing. ---
--- How do I output these lines? ---
如何输出这些线?
我猜,应该有......比如我的awk声明中的 else :
awk '/INFO/ { print $0 } /ERROR/ { print $0 } /EVERYTHING_ELSE/ { print $0 }'
由于 哈德
更新: 这是更新的 tailError.sh:,它按预期工作。 谢谢,jas。
tail -f error.log | awk '
/INFO/ {print "\033[1;37m" $0 "\033[39m"; next}
/ERROR/ {print "\033[31m" $0 "\033[39m"; next}
/WARN/ {print "\033[33m" $0 "\033[39m"; next}
/DEBUG/ {print "\033[0;37m" $0 "\033[39m"; next}
/at com.adobe.training/ {print "\033[31m" $0 "\033[39m"; next}
{print $0}
'
答案 0 :(得分:4)
您可以引入一个状态变量,只要没有像“INFO”这样的特殊“标记”,您就可以将颜色重置为某些默认值。或者,只有存在“MARK”时才更改颜色状态。
tail -f error.log | awk ' BEGIN { colcode = 0 }
/INFO/ { colcode = "1;37" }
/ERROR/ { colcode = "31" }
/WARN/ { colcode = "33" }
/DEBUG/ { colcode = "0;37" }
{ print "\033[" colcode "m" $0 "\033[39m" ; colcode = 0 }
请注意,print语句没有选择RE,因此是“else”行。
答案 1 :(得分:1)
我在awk中使用next
来表示else-ish行为。
/INFO/ {print 1; next}
/ERROR/ {print 2; next}
{print 3}
下一个语句强制awk立即停止处理当前记录并继续下一条记录。这意味着不会对当前记录执行进一步的规则,并且不会执行当前规则的其余操作。
因此,这可以理解为:
If record matches /INPUT/ then print 1
else if record matches /ERROR/ then print 2
else print 3
答案 2 :(得分:0)
对于Java,log4j,我使用:
tail -f -n 400 /var/log/log.0 | \
awk '
{matched=0}
/INFO/ {matched=1; print "\033[0;34m" $0 "\033[0m"}
/INFORMACIÓN/ {matched=1; print "\033[0;34m" $0 "\033[0m"}
/WARNING/ {matched=1; print "\033[0;93m" $0 "\033[0m"}
/ADVERTENCIA/ {matched=1; print "\033[0;93m" $0 "\033[0m"}
/ERROR/ {matched=1; print "\033[0;91m" $0 "\033[0m"}
/SEVERE/ {matched=1; print "\033[0;91m" $0 "\033[0m"}
/failed/ {matched=1; print "\033[0;91m" $0 "\033[0m"}
/GRAVE/ {matched=1; print "\033[0;91m" $0 "\033[0m"}
/Caused by/ {matched=1; print "\033[0;96m" $0 "\033[0m"}
matched==0 {print "\033[0;36m" $0 "\033[0m"}
'