File Coverage

blib/lib/Log/Any/Plugin/Stringify.pm
Criterion Covered Total %
statement 28 28 100.0
branch 4 4 100.0
condition 2 2 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1             package Log::Any::Plugin::Stringify;
2             # ABSTRACT: Custom argument stringification plugin for log adapters
3             $Log::Any::Plugin::Stringify::VERSION = '0.012';
4 2     2   1393 use strict;
  2         4  
  2         54  
5 2     2   8 use warnings;
  2         2  
  2         60  
6              
7 2         100 use Log::Any::Plugin::Util qw(
8             all_logging_methods get_old_method set_new_method
9 2     2   9 );
  2         4  
10              
11 2     2   901 use Data::Dumper;
  2         5561  
  2         482  
12              
13             my $separator;
14              
15              
16             sub install {
17 3     3 1 10 my ($class, $adapter_class, %args) = @_;
18              
19 3 100       8 $separator = defined $args{separator} ? $args{separator} : '';
20 3   100     16 my $stringifier = $args{stringifier} || \&default_stringifier;
21              
22             # Inject the stringifier into the existing logging methods
23             #
24 3         8 for my $method_name ( all_logging_methods() ) {
25 57         121 my $old_method = get_old_method($adapter_class, $method_name);
26             set_new_method($adapter_class, $method_name, sub {
27 11     11   4244 my $self = shift;
28 11         23 $self->$old_method($stringifier->(@_));
29 57         195 });
30             }
31             }
32              
33             sub default_stringifier {
34 10     10 1 21 my @args = @_;
35              
36 10         14 local $Data::Dumper::Indent = 0;
37 10         13 local $Data::Dumper::Pair = '=';
38 10         14 local $Data::Dumper::Quotekeys = 0;
39 10         12 local $Data::Dumper::Sortkeys = 1;
40 10         13 local $Data::Dumper::Terse = 1;
41              
42 10 100       15 return join($separator, map { ref $_ ? Dumper($_) : $_ } @args);
  23         107  
43             }
44              
45             1;
46              
47             __END__