Subject: | [PATCH] Add configuration options to Appender::ScreenColoredLevels |
The attached patch for Log::Log4perl::Appender::ScreenColoredLevels
corrects a documentation error (the color for DEBUG messages is not
black, it is uncolored, so it appears as whatever the default color of
your terminal is.
Additionally, this patch adds configurability for the colors for each
level, so you can say:
log4perl.appender.Screen.color.TRACE=cyan
or
log4perl.appender.Screen.color.DEBUG=bold blue
or if you want to get really crazy, you can even say
log4perl.appender.Screen.color.FATAL=bold underline blink red on_white
The default settings for each level produce exactly the same output as
the current version, so if your configuration doesn't change any colors,
then you get identical behavior to the current version, making the patch
100% backwards-compatible.
Subject: | appender-screencoloredlevels.patch |
--- /usr/local/lib/perl5/site_perl/5.8.8/Log/Log4perl/Appender/ScreenColoredLevels.pm 2007-10-11 22:39:45.000000000 -0400
+++ ScreenColoredLevels.pm 2007-11-21 12:55:18.000000000 -0500
@@ -6,7 +6,7 @@
use warnings;
use strict;
-use Term::ANSIColor qw(:constants);
+use Term::ANSIColor qw();
use Log::Log4perl::Level;
##################################################
@@ -17,9 +17,24 @@
my $self = {
name => "unknown name",
stderr => 1,
+ color => {},
@options,
};
+ my %default_colors = (
+ TRACE => 'yellow',
+ DEBUG => '',
+ INFO => 'green',
+ WARN => 'blue',
+ ERROR => 'magenta',
+ FATAL => 'red',
+ );
+ for my $level ( keys %default_colors ) {
+ if ( ! exists $self->{ 'color' }->{ $level } ) {
+ $self->{ 'color' }->{ $level } = $default_colors{ $level };
+ }
+ }
+
bless $self, $class;
}
@@ -28,35 +43,16 @@
##################################################
my($self, %params) = @_;
- $params{message} = color($params{log4p_level}, $params{message});
+ my $msg = $params{ 'message' };
+
+ if ( my $color = $self->{ 'color' }->{ $params{ 'log4p_level' } } ) {
+ $msg = Term::ANSIColor::colored( $msg, $color );
+ }
if($self->{stderr}) {
- print STDERR $params{message};
- } else {
- print $params{message};
- }
-}
-
-##################################################
-sub color {
-##################################################
- my($level, $message) = @_;
-
- if(0) {
- } elsif($level eq "TRACE") {
- return YELLOW . $message . RESET;
- } elsif($level eq "DEBUG") {
- return $message;
- } elsif($level eq "INFO") {
- return GREEN . $message . RESET;
- } elsif($level eq "WARN") {
- return BLUE . $message . RESET;
- } elsif($level eq "ERROR") {
- return MAGENTA . $message . RESET;
- } elsif($level eq "FATAL") {
- return RED . $message . RESET;
+ print STDERR $msg;
} else {
- return $message;
+ print $msg;
}
}
@@ -102,13 +98,56 @@
This appender acts like Log::Log4perl::Appender::Screen, except that
it colorizes its output, based on the priority of the message sent.
-The color scheme is
+You can configure the colors and attributes used for the different
+levels, by specifying them in your configuration:
+
+ log4perl.appender.Screen.color.TRACE=cyan
+ log4perl.appender.Screen.color.DEBUG=bold blue
+
+You can also specify nothing, to indicate that level should not have
+coloring applied, which means the text will be whatever the default
+color for your terminal is. This is the default for debug messages.
+
+ log4perl.appender.Screen.color.DEBUG=
+
+You can use any attribute supported by L<Term::ANSIColor> as a configuration
+option.
+
+ log4perl.appender.Screen.color.FATAL=\
+ bold underline blink red on_white
+
+The commonly used colors and attributes are:
+
+=over 4
+
+=item attributes
+
+BOLD, DARK, UNDERLINE, UNDERSCORE, BLINK
+
+=item colors
+
+BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
+
+=item background colors
+
+ON_BLACK, ON_RED, ON_GREEN, ON_YELLOW, ON_BLUE, ON_MAGENTA, ON_CYAN, ON_WHITE
+
+=back
+
+See L<Term::ANSIColor> for a complete list, and information on which are
+supported by various common terminal emulators.
+
+The default values for these options are:
=over 4
+=item Trace
+
+Yellow
+
=item Debug
-Black
+None (whatever the terminal default is)
=item Info
@@ -132,9 +171,15 @@
if set to a true value, the appender will log to STDERR. If C<stderr>
is set to a false value, it will log to STDOUT. The default setting
for C<stderr> is 1, so messages will be logged to STDERR by default.
+The constructor can also take an optional parameter C<color>, whose
+value is a hashref of color configuration options, any levels that
+are not included in the hashref will be set to their default values.
=head1 AUTHOR
-Mike Schilli <log4perl@perlmeister.com>, 2004
+Mike Schilli C<< <log4perl@perlmeister.com> >>, 2004
+
+Color configuration and attribute support added 2007 by
+Jason Kohles C<< <email@jasonkohles.com> >>.
=cut