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   435 use 5.008001;
  25         81  
2 25     25   163 use strict;
  25         45  
  25         763  
3 25     25   132 use warnings;
  25         73  
  25         1379  
4              
5             package Log::Any::Proxy;
6              
7             # ABSTRACT: Log::Any generator proxy object
8             our $VERSION = '1.715';
9              
10 25     25   162 use Log::Any::Adapter::Util ();
  25         58  
  25         563  
11 25     25   28131 use overload;
  25         23125  
  25         147  
12              
13             sub _stringify_params {
14 49     49   92 my @params = @_;
15              
16             return map {
17 49 100       98 !defined($_)
  33 100       249  
    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   110 my ( $cat, $lvl, $format, @params ) = @_;
30 42 100       107 return $format->() if ref($format) eq 'CODE';
31              
32 41         88 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   4363 no warnings;
  25         54  
  25         6325  
40 41         465 return sprintf( $format, @new_params );
41             }
42              
43             sub new {
44 65     65 0 135 my $class = shift;
45 65         264 my $self = { formatter => \&_default_formatter, @_ };
46 65 50       282 unless ( $self->{adapter} ) {
47 0         0 require Carp;
48 0         0 Carp::croak("$class requires an 'adapter' parameter");
49             }
50 65 50       173 unless ( defined $self->{category} ) {
51 0         0 require Carp;
52 0         0 Carp::croak("$class requires a 'category' parameter");
53             }
54 65 50       152 unless ( $self->{context} ) {
55 0         0 require Carp;
56 0         0 Carp::croak("$class requires a 'context' parameter");
57             }
58 65         110 bless $self, $class;
59 65         246 $self->init(@_);
60 65         246 return $self;
61             }
62              
63             sub clone {
64 1     1 0 14 my $self = shift;
65 1         2 return (ref $self)->new( %{ $self }, @_ );
  1         5  
66             }
67              
68       65 0   sub init { }
69              
70             for my $attr (qw/adapter category filter formatter prefix context/) {
71 25     25   181 no strict 'refs';
  25         57  
  25         3955  
72 51     51   3018 *{$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   171 no strict 'refs';
  25         66  
  25         13571  
86             *{$is_name} = sub {
87 89     89   11081 my ($self) = @_;
88 89         525 return $self->{adapter}->$is_realname;
89             };
90             *{$name} = sub {
91 113     113   19030 my ( $self, @parts ) = @_;
92 113 100 100     733 return if !$self->{adapter}->$is_realname && !defined wantarray;
93              
94             my $structured_logging =
95 108   66     781 $self->{adapter}->can('structured') && !$self->{filter};
96              
97 108 100 100     748 my $data_from_parts = pop @parts
      66        
98             if ( @parts && ( ( ref $parts[-1] || '' ) eq ref {} ) );
99 108         321 my $data_from_context = $self->{context};
100             my $data =
101 108 100       226 { map {%$_} grep {$_ && %$_} $data_from_context, $data_from_parts };
  23         98  
  216         709  
102              
103 108 100       331 if ($structured_logging) {
104 15 50       45 unshift @parts, $self->{prefix} if $self->{prefix};
105             $self->{adapter}
106 15         42 ->structured( $realname, $self->{category}, @parts, grep {%$_} $data );
  15         53  
107 15 50       441 return unless defined wantarray;
108             }
109              
110 93 50       223 @parts = grep { defined($_) && length($_) } @parts;
  99         431  
111 93 100       254 push @parts, _stringify_params($data) if %$data;
112              
113 93         557 my $message = join( " ", @parts );
114 93 100 66     342 if ( length $message && !$structured_logging ) {
115             $message =
116             $self->{filter}->( $self->{category}, $numeric, $message )
117 66 100       192 if defined $self->{filter};
118 66 50 33     260 if ( defined $message and length $message ) {
119             $message = "$self->{prefix}$message"
120 66 100 66     203 if defined $self->{prefix} && length $self->{prefix};
121 66         326 $self->{adapter}->$realname($message);
122             }
123             }
124 93 100       520 return $message if defined wantarray;
125             };
126             *{$namef} = sub {
127 46     46   344 my ( $self, @args ) = @_;
128 46 50 66     189 return if !$self->{adapter}->$is_realname && !defined wantarray;
129             my $message =
130 46         187 $self->{formatter}->( $self->{category}, $numeric, @args );
131 46 100 66     257 return unless defined $message and length $message;
132 19         87 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__