File Coverage

blib/lib/Log/Any/Plugin/ANSIColor.pm
Criterion Covered Total %
statement 30 30 100.0
branch 10 10 100.0
condition 5 6 83.3
subroutine 7 7 100.0
pod 0 1 0.0
total 52 54 96.3


line stmt bran cond sub pod time code
1             package Log::Any::Plugin::ANSIColor;
2 5     5   4961 use 5.008001;
  5         22  
3 5     5   34 use strict;
  5         14  
  5         137  
4 5     5   32 use warnings;
  5         11  
  5         282  
5              
6             our $VERSION = "0.01";
7              
8 5     5   329 use Log::Any::Plugin::Util qw( get_old_method set_new_method );
  5         9724  
  5         452  
9 5     5   871 use Term::ANSIColor qw( colored colorvalid );
  5         7204  
  5         2352  
10              
11             our %default = (
12             emergency => 'bold magenta',
13             alert => 'magenta',
14             critical => 'bold red',
15             error => 'red',
16             warning => 'yellow',
17             debug => 'cyan',
18             trace => 'blue',
19             );
20              
21             sub install {
22 4     4 0 203 my ($class, $adapter_class, %color_map) = @_;
23              
24 4 100 100     40 if ((delete $color_map{default}) || (keys %color_map == 0)) {
25             # Copy the default colors, leaving any the user has specified.
26 2         11 for my $method (keys %default) {
27 14   66     62 $color_map{$method} ||= $default{$method};
28             }
29             }
30              
31 4         31 for my $method_name (Log::Any->logging_methods) {
32 36 100       291 if (my $color = delete $color_map{$method_name}) {
33             # Specifying none as the color name disables colorisation for that
34             # method.
35 19 100       58 next if $color eq 'none';
36              
37 18 100       65 if (!colorvalid($color)) {
38 1         45 warn "Invalid color name \"$color\" for $method_name";
39 1         12 next;
40             }
41              
42 17         387 my $old_method = get_old_method($adapter_class, $method_name);
43             set_new_method($adapter_class, $method_name, sub {
44 17     17   22900 my $self = shift;
45 17         92 $self->$old_method(colored([$color], @_));
46 17         303 });
47             }
48             }
49              
50 4 100       63 if (my @remainder = sort keys %color_map) {
51 1         17 warn 'Unknown logging methods: ', join(', ', @remainder);
52             }
53             }
54              
55             1;
56             __END__