File Coverage

blib/lib/Log/Any/Proxy.pm
Criterion Covered Total %
statement 72 78 92.3
branch 38 44 86.3
condition 18 23 78.2
subroutine 16 16 100.0
pod 0 3 0.0
total 144 164 87.8


line stmt bran cond sub pod time code
1 40     40   155462 use 5.008001;
  40         154  
2 40     40   227 use strict;
  40         169  
  40         1004  
3 40     40   179 use warnings;
  40         46  
  40         2541  
4              
5             package Log::Any::Proxy;
6              
7             # ABSTRACT: Log::Any generator proxy object
8             our $VERSION = '1.720';
9              
10 40     40   919 use Log::Any::Adapter::Util ();
  40         75  
  40         831  
11 40     40   12163 use overload;
  40         33771  
  40         240  
12              
13             sub _stringify_params {
14 54     54   84 my @params = @_;
15              
16             return map {
17 54 100       92 !defined($_)
  38 100       246  
    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   87 my ( $cat, $lvl, $format, @params ) = @_;
30 42 100       85 return $format->() if ref($format) eq 'CODE';
31              
32 41         77 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 40     40   6255 no warnings;
  40         58  
  40         9195  
40 41         373 return sprintf( $format, @new_params );
41             }
42              
43             sub new {
44 69     69 0 97 my $class = shift;
45 69         283 my $self = { formatter => \&_default_formatter, @_ };
46 69 50       294 unless ( $self->{adapter} ) {
47 0         0 require Carp;
48 0         0 Carp::croak("$class requires an 'adapter' parameter");
49             }
50 69 50       138 unless ( defined $self->{category} ) {
51 0         0 require Carp;
52 0         0 Carp::croak("$class requires a 'category' parameter");
53             }
54 69 50       142 unless ( $self->{context} ) {
55 0         0 require Carp;
56 0         0 Carp::croak("$class requires a 'context' parameter");
57             }
58 69         106 bless $self, $class;
59 69         193 $self->init(@_);
60 69         228 return $self;
61             }
62              
63             sub clone {
64 1     1 0 8 my $self = shift;
65 1         3 return (ref $self)->new( %{ $self }, @_ );
  1         3  
66             }
67              
68       69 0   sub init { }
69              
70             for my $attr (qw/adapter category filter formatter prefix context/) {
71 40     40   209 no strict 'refs';
  40         65  
  40         5243  
72 54     54   341039 *{$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 $is_name = "is_$name";
82             my $is_realname = "is_$realname";
83             my $numeric = Log::Any::Adapter::Util::numeric_level($realname);
84 40     40   222 no strict 'refs';
  40         58  
  40         17669  
85             *{$is_name} = sub {
86 89     89   482315 my ($self) = @_;
87 89         429 return $self->{adapter}->$is_realname;
88             };
89             for my $f ( '', 'f' ) {
90             *{"$name$f"} = sub {
91 161     161   523937 my ( $self, @parts ) = @_;
92 161         562 my $adapter = $self->{adapter};
93 161 100 100     539 return if !$adapter->$is_realname && !defined wantarray;
94              
95 156 100       473 if ($f eq 'f') {
96             my $message =
97 46         130 $self->{formatter}->( $self->{category}, $numeric, @parts );
98 46 100 66     244 return unless defined $message and length $message;
99 19         39 @parts = ( $message );
100             }
101              
102             my $structured_logging =
103 129   100     722 !$self->{filter} && $adapter->can('structured');
104              
105 129 100 100     669 my $data_from_parts = pop @parts
      66        
106             if ( @parts && ( ( ref $parts[-1] || '' ) eq ref {} ) );
107 129         239 my $data_from_context = $self->{context};
108             my $data =
109 129 100       228 { map {%$_} grep {$_ && %$_} $data_from_context, $data_from_parts };
  30         101  
  258         798  
110              
111 129 100       251 if ($structured_logging) {
112 34 50       59 unshift @parts, $self->{prefix} if $self->{prefix};
113 34         59 $adapter->$structured_logging( $realname, $self->{category}, @parts, grep {%$_} $data );
  34         89  
114 34 100       2275 return unless defined wantarray;
115             }
116              
117 107 50       154 @parts = grep { defined($_) && length($_) } @parts;
  117         461  
118 107 100       218 push @parts, _stringify_params($data) if %$data;
119              
120 107         706 my $message = join( " ", @parts );
121 107 100 100     371 if ( length $message && !$structured_logging ) {
122             $message =
123             $self->{filter}->( $self->{category}, $numeric, $message )
124 68 100       148 if defined $self->{filter};
125 68 50 33     240 if ( defined $message and length $message ) {
126             $message = "$self->{prefix}$message"
127 68 100 66     215 if defined $self->{prefix} && length $self->{prefix};
128 68         233 $adapter->$realname($message);
129             }
130             }
131 107 100       477 return $message if defined wantarray;
132             };
133             }
134             }
135              
136             1;
137              
138              
139             # vim: ts=4 sts=4 sw=4 et tw=75:
140              
141             __END__