[Catalyst] Patch for ANSI colored logging

Rusty Conover rconover at infogears.com
Wed Aug 3 09:50:29 CEST 2005


Hi All,

Attached is a patch for enabling ANSI terminal colors for logging of 
Catalyst (using Term::ANSIColor), which is most helpful in debug mode.  
Apply it to Catalyst/Log.pm.

It's enabled just by setting the CATALYST_COLOR_LOG environment variable 
to a true value, otherwise logging is not effected.

Using this patch makes errors and info messages show up much more in 
between normal debug output.

I'm interested in any feedback.

Thanks,

Rusty

-- 
Rusty Conover
InfoGears Inc.
http://www.infogears.com
406-587-5432


-------------- next part --------------
--- lib/Catalyst/Log.pm	2005-07-04 04:51:47.000000000 -0600
+++ /home/rconover/colored-log2.pl	2005-08-03 01:42:38.000000000 -0600
@@ -5,9 +5,23 @@
 use Data::Dumper;
 
 our %LEVELS = ();
+our %LEVEL_COLORS = (
+	debug => 'green',
+	info => 'yellow bold',
+	warn => 'yellow',
+	error => 'red bold',
+	fatal => 'red bold'
+);
 
 __PACKAGE__->mk_accessors('level');
 
+BEGIN {
+	if($ENV{CATALYST_COLOR_LOG}) {
+		use Term::ANSIColor;
+	}
+};
+
+
 {
     my @levels = qw[ debug info warn error fatal ];
 
@@ -58,6 +72,7 @@
     $self->{level} &= ~$_ for map { $LEVELS{$_} } @levels;
 }
 
+
 sub _dump {
     my $self = shift;
     local $Data::Dumper::Terse = 1;
@@ -69,7 +84,13 @@
     my $level   = shift;
     my $time    = localtime(time);
     my $message = join( "\n", @_ );
-    printf( STDERR "[%s] [catalyst] [%s] %s\n", $time, $level, $message );
+
+    if($ENV{CATALYST_COLOR_LOG}) {
+      my $msg = sprintf("[%s] [catalyst] [%s] %s\n", $time, $level, $message );
+      print STDERR Term::ANSIColor::colored($msg, $LEVEL_COLORS{$level});
+    } else {	
+      printf( STDERR "[%s] [catalyst] [%s] %s\n", $time, $level, $message );
+    }
 }
 
 1;


More information about the Catalyst mailing list