File Coverage

blib/lib/Log/Any/Plugin/Encode.pm
Criterion Covered Total %
statement 25 25 100.0
branch 1 2 50.0
condition 2 2 100.0
subroutine 7 7 100.0
pod 1 1 100.0
total 36 37 97.3


line stmt bran cond sub pod time code
1             package Log::Any::Plugin::Encode;
2             # ABSTRACT: Output message encoding for log adapters
3             $Log::Any::Plugin::Encode::VERSION = '0.012';
4 3     3   2041 use strict;
  3         6  
  3         79  
5 3     3   15 use warnings;
  3         5  
  3         74  
6              
7 3     3   13 use Carp qw( croak );
  3         4  
  3         165  
8              
9 3     3   546 use Encode qw( find_encoding );
  3         8966  
  3         172  
10              
11 3     3   29 use Log::Any::Plugin::Util qw( get_old_method set_new_method );
  3         9  
  3         595  
12              
13              
14             sub install {
15 3     3 1 9 my ($class, $adapter_class, %args) = @_;
16              
17 3   100     12 my $encoding = $args{encoding} || 'utf8';
18 3 50       11 my $encoder = find_encoding($encoding) or
19             croak "Could not find encoder for encoding[$encoding], check encoding value for typos, or codec installed";
20              
21 3         73 for my $method_name ( Log::Any->logging_methods() ) {
22 27         65 my $old_method = get_old_method($adapter_class, $method_name);
23              
24             set_new_method($adapter_class, $method_name, sub {
25 3     3   1797 my $self = shift;
26              
27 3         6 my @encoded_msgs = map { $encoder->encode($_, Encode::FB_WARN) } @_;
  3         25  
28              
29 3         12 return $self->$old_method(@encoded_msgs);
30 27         103 });
31             }
32             }
33              
34              
35             1;
36              
37             __END__