Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
colorize keywords of logs.
#!/usr/bin/env perl
=head1 Examples
$ tail -f access_log | perl colorize.pl
$ plackup app.psgi 2>&1 | perl colorize.pl
=cut
use strict;
use warnings;
use Term::ANSIColor qw(colored);
# XXX customize it.
my %config = (
' (2\d{2}) ' => 'cyan',
' (3\d{2}) ' => 'green',
' (4\d{2}) ' => 'red on_bright_yellow',
' (5\d{2}) ' => 'red',
);
=head1 COLORS
The available foreground colors are:
black red green yellow
blue magenta cyan white
bright_black bright_red bright_green bright_yellow
bright_blue bright_magenta bright_cyan bright_white
The available background colors are:
on_black on_red on_green on yellow
on_blue on_magenta on_cyan on_white
on_bright_black on_bright_red on_bright_green on_bright_yellow
on_bright_blue on_bright_magenta on_bright_cyan on_bright_white
=cut
while (my $input = readline STDIN) {
for my $re (keys %config) {
if (my ($m) = $input =~ /$re/) {
$input =~ s/$m/colored($m, $config{$re})/ge;
}
}
print STDOUT $input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.