Log4perl/Appender/ScreenColoredLevels.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 72 CC 7 SCAR 26.5
CC
7
Cov
0%
log
30.0
new
12.0
BEGIN
2.0
CRAP 14.1
1 ##################################################
2 package Log::Log4perl::Appender::ScreenColoredLevels;
3 ##################################################
4 use Log::Log4perl::Appender::Screen;
5 our @ISA = qw(Log::Log4perl::Appender::Screen);
6  
7 use warnings;
8 use strict;
9  
10 use Term::ANSIColor qw();
11 use Log::Log4perl::Level;
12  
13 BEGIN {
14 $Term::ANSIColor::EACHLINE="\n";
15 }
16  
17 ##################################################
18 sub new {
19 ##################################################
20 my($class, %options) = @_;
21  
22 my %specific_options = ( color => {} );
23  
24 for my $option ( keys %specific_options ) {
25 $specific_options{ $option } = delete $options{ $option } if
26 exists $options{ $option };
27 }
28  
29 my $self = $class->SUPER::new( %options );
30 @$self{ keys %specific_options } = values %specific_options;
31 bless $self, __PACKAGE__; # rebless
32  
33 # also accept lower/mixed case levels in config
34 for my $level ( keys %{ $self->{color} } ) {
35 my $uclevel = uc($level);
36 $self->{color}->{$uclevel} = $self->{color}->{$level};
37 }
38  
39 my %default_colors = (
40 TRACE => 'yellow',
41 DEBUG => '',
42 INFO => 'green',
43 WARN => 'blue',
44 ERROR => 'magenta',
45 FATAL => 'red',
46 );
47 for my $level ( keys %default_colors ) {
48 if ( ! exists $self->{ 'color' }->{ $level } ) {
49 $self->{ 'color' }->{ $level } = $default_colors{ $level };
50 }
51 }
52  
53 bless $self, $class;
54 }
55
56 ##################################################
57 sub log {
58 ##################################################
59 my($self, %params) = @_;
60  
61 my $msg = $params{ 'message' };
62  
63 if ( my $color = $self->{ 'color' }->{ $params{ 'log4p_level' } } ) {
64 $msg = Term::ANSIColor::colored( $msg, $color );
65 }
66  
67 my $fh = \*STDOUT;
68 if (ref $self->{stderr}) {
69 $fh = \*STDERR if $self->{stderr}{ $params{'log4p_level'} }
70 || $self->{stderr}{ lc $params{'log4p_level'} };
71 } elsif ($self->{stderr}) {
72 $fh = \*STDERR;
73 }
74  
75 print $fh $msg;
76 }
77  
78 1;
79  
80 __END__