Created
October 1, 2014 07:57
-
-
Save soren/00298f7fcf61fcceefa2 to your computer and use it in GitHub Desktop.
Simple Perl script that adds colors to e.g. log files using ANSI escape codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
=head1 NAME | |
colorize.pl - adds colors to e.g. log files using ANSI escape codes | |
=head1 SYNOPSIS | |
colorize.pl LOGFILE | |
some_command | colorize.pl | |
Some examples: | |
$ colorize.pl /var/log/apache/request.log | |
$ tail -f /var/log/apache/request.log | colorize.pl | |
=head1 DESCRIPTION | |
The colorize.pl script will add colors to a file or stdin. The rules | |
for adding colors are based on some simple keywords appearing on a | |
line, e.g. a line containing the string "ERROR" will be red. | |
=for text | |
=encoding utf-8 | |
=end | |
=head1 AUTHOR | |
Søren Lund, C<< <soren at lund dot org> >> | |
=head1 SEE ALSO | |
L<https://en.wikipedia.org/wiki/ANSI_escape_code> | |
=head1 COPYRIGHT | |
This file is made available under the Creative Commons CC0 1.0 | |
Universal Public Domain Dedication. | |
See L<https://creativecommons.org/publicdomain/zero/1.0/deed.en> | |
=cut | |
use warnings; | |
use strict; | |
use utf8; | |
$|=1; # no buffering | |
while(<>) { | |
if (/SUCCESS/) { | |
print "[32m$_[0m"; | |
} elsif (/\d+ errors?|ERROR|FATAL|FAILURE/) { | |
print "[31m$_[0m"; | |
} elsif (/WARN|SKIPPED/) { | |
print "[33m$_[0m"; | |
} elsif (/INFO/) { | |
print "[3;34m$_[0m"; | |
} elsif (/(?:^Downloading:|Downloaded:|^Loading source files for)/) { | |
print "[3;30;1m$_[0m"; | |
} else { | |
print $_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment