Skip to content

Instantly share code, notes, and snippets.

@kazuph
Forked from Cside/colorize.pl
Created November 12, 2012 01:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kazuph/4057054 to your computer and use it in GitHub Desktop.
Save kazuph/4057054 to your computer and use it in GitHub Desktop.
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}) ' => 'magenta',
' (5\d{2}) ' => 'red',
'(info)' => 'cyan',
'(warn)' => 'green',
'(debug)' => 'magenta',
'(error)' => '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