File Coverage

blib/lib/Log/Any/Proxy.pm
Criterion Covered Total %
statement 72 78 92.3
branch 36 44 81.8
condition 18 26 69.2
subroutine 17 17 100.0
pod 0 3 0.0
total 143 168 85.1


line stmt bran cond sub pod time code
1 25     25   438 use 5.008001;
  25         87  
2 25     25   171 use strict;
  25         45  
  25         880  
3 25     25   138 use warnings;
  25         54  
  25         1714  
4              
5             package Log::Any::Proxy;
6              
7             # ABSTRACT: Log::Any generator proxy object
8             our $VERSION = '1.717';
9              
10 25     25   186 use Log::Any::Adapter::Util ();
  25         53  
  25         558  
11 25     25   29100 use overload;
  25         24011  
  25         149  
12              
13             sub _stringify_params {
14 49     49   102 my @params = @_;
15              
16             return map {
17 49 100       95 !defined($_)
  33 100       250  
    100          
18             ? ''
19             : ref($_) ? (
20             overload::OverloadedStringify($_)
21             ? "$_"
22             : Log::Any::Adapter::Util::dump_one_line($_)
23             )
24             : $_
25             } @params;
26             }
27              
28             sub _default_formatter {
29 42     42   104 my ( $cat, $lvl, $format, @params ) = @_;
30 42 100       119 return $format->() if ref($format) eq 'CODE';
31              
32 41         92 my @new_params = _stringify_params(@params);
33              
34             # Perl 5.22 adds a 'redundant' warning if the number parameters exceeds
35             # the number of sprintf placeholders. If a user does this, the warning
36             # is issued from here, which isn't very helpful. Doing something
37             # clever would be expensive, so instead we just disable warnings for
38             # the final line of this subroutine.
39 25     25   4451 no warnings;
  25         65  
  25         6504  
40 41         447 return sprintf( $format, @new_params );
41             }
42              
43             sub new {
44 65     65 0 125 my $class = shift;
45 65         264 my $self = { formatter => \&_default_formatter, @_ };
46 65 50       275 unless ( $self->{adapter} ) {
47 0         0 require Carp;
48 0         0 Carp::croak("$class requires an 'adapter' parameter");
49             }
50 65 50       182 unless ( defined $self->{category} ) {
51 0         0 require Carp;
52 0         0 Carp::croak("$class requires a 'category' parameter");
53             }
54 65 50       162 unless ( $self->{context} ) {
55 0         0 require Carp;
56 0         0 Carp::croak("$class requires a 'context' parameter");
57             }
58 65         115 bless $self, $class;
59 65         247 $self->init(@_);
60 65         235 return $self;
61             }
62              
63             sub clone {
64 1     1 0 22 my $self = shift;
65 1         3 return (ref $self)->new( %{ $self }, @_ );
  1         7  
66             }
67              
68       65 0   sub init { }
69              
70             for my $attr (qw/adapter category filter formatter prefix context/) {
71 25     25   190 no strict 'refs';
  25         57  
  25         4013  
72 51     51   3017 *{$attr} = sub { return $_[0]->{$attr} };
73             }
74              
75             my %aliases = Log::Any::Adapter::Util::log_level_aliases();
76              
77             # Set up methods/aliases and detection methods/aliases
78             foreach my $name ( Log::Any::Adapter::Util::logging_methods(), keys(%aliases) )
79             {
80             my $realname = $aliases{$name} || $name;
81             my $namef = $name . "f";
82             my $is_name = "is_$name";
83             my $is_realname = "is_$realname";
84             my $numeric = Log::Any::Adapter::Util::numeric_level($realname);
85 25     25   180 no strict 'refs';
  25         60  
  25         13919  
86             *{$is_name} = sub {
87 89     89   7458 my ($self) = @_;
88 89         500 return $self->{adapter}->$is_realname;
89             };
90             *{$name} = sub {
91 113     113   19124 my ( $self, @parts ) = @_;
92 113 100 100     738 return if !$self->{adapter}->$is_realname && !defined wantarray;
93              
94             my $structured_logging =
95 108   66     743 $self->{adapter}->can('structured') && !$self->{filter};
96              
97 108 100 100     773 my $data_from_parts = pop @parts
      66        
98             if ( @parts && ( ( ref $parts[-1] || '' ) eq ref {} ) );
99 108         328 my $data_from_context = $self->{context};
100             my $data =
101 108 100       233 { map {%$_} grep {$_ && %$_} $data_from_context, $data_from_parts };
  23         96  
  216         697  
102              
103 108 100       329 if ($structured_logging) {
104 15 50       35 unshift @parts, $self->{prefix} if $self->{prefix};
105             $self->{adapter}
106 15         31 ->structured( $realname, $self->{category}, @parts, grep {%$_} $data );
  15         54  
107 15 50       453 return unless defined wantarray;
108             }
109              
110 93 50       170 @parts = grep { defined($_) && length($_) } @parts;
  99         439  
111 93 100       228 push @parts, _stringify_params($data) if %$data;
112              
113 93         620 my $message = join( " ", @parts );
114 93 100 66     372 if ( length $message && !$structured_logging ) {
115             $message =
116             $self->{filter}->( $self->{category}, $numeric, $message )
117 66 100       201 if defined $self->{filter};
118 66 50 33     333 if ( defined $message and length $message ) {
119             $message = "$self->{prefix}$message"
120 66 100 66     223 if defined $self->{prefix} && length $self->{prefix};
121 66         385 $self->{adapter}->$realname($message);
122             }
123             }
124 93 100       510 return $message if defined wantarray;
125             };
126             *{$namef} = sub {
127 46     46   354 my ( $self, @args ) = @_;
128 46 50 66     183 return if !$self->{adapter}->$is_realname && !defined wantarray;
129             my $message =
130 46         141 $self->{formatter}->( $self->{category}, $numeric, @args );
131 46 100 66     247 return unless defined $message and length $message;
132 19         88 return $self->$name($message);
133             };
134             }
135              
136             1;
137              
138              
139             # vim: ts=4 sts=4 sw=4 et tw=75:
140              
141             __END__