Skip to content

Instantly share code, notes, and snippets.

@soren
Created October 1, 2014 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soren/00298f7fcf61fcceefa2 to your computer and use it in GitHub Desktop.
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
#!/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 "$_";
} elsif (/\d+ errors?|ERROR|FATAL|FAILURE/) {
print "$_";
} elsif (/WARN|SKIPPED/) {
print "$_";
} elsif (/INFO/) {
print "$_";
} elsif (/(?:^Downloading:|Downloaded:|^Loading source files for)/) {
print "$_";
} else {
print $_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment