2008年12月20日星期六

彩色日誌 Coloring Your Logs

系統無時無刻都在產生 log,動則幾千幾萬行的 log 讓你看的眼花暸亂嗎?試試這個好用的 perl 程式吧!
偶然在 Linux Server Hacks 書中看到的小程式,可以把自己想看的關鍵字都標上顏色,非常實用且美觀 XD

因為怕忘記所以就在此記錄一下…

使用方法:把它存成 "rcg" 弄成可執行檔並放在它應該去的地方,然後…

# cat /var/log/some_dxxn_logs | rcg "sexy" "RED" "girl" "GREEN" "OOXX" "YELLOW"
There is a sexy girl blah
and the girl blah blah
blah blah blah OOXX

#!/usr/bin/perl -w

use strict;
use Term::ANSIColor qw(:constants);

my %target = ( );

while (my $arg = shift) {
my $clr = shift;
if(($arg =~ /^-/) | (!$clr)) {
print "Usage: rcg [regex] [color] [regex] [color] ...\n";
exit;
}
$target{$arg} = eval($clr);
}

my $rst = RESET;

while(<>) {
foreach my $x (keys(%target)) {
s/($x)/$target{$x}$1$rst/g;
}
print;
}

上面的 Code Block 是參考 "Spin's Blog - [Blogger]美觀的Code Block" 弄的,正!

0 回應 :

張貼意見