Log4perl/Appender.pm

filter: Statementstmt 94.6% 107 / 113 Branchbran 87.0% 47 / 54 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 20 / 20 Podpod 20.0% 2 / 10 totaltotal 88.4% 183 / 207 CC 32 SCAR 19.0
CC
32
Cov
92%
log
13.1
new
11.2
threshold
3.0
CRAP 6.7
« Log4perl/Logger.pm ↑ Summary Log4perl/Appender/Synchronized.pm »
1 ##################################################
2 package Log::Log4perl::Appender;
3 ##################################################
4  
5 54 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 54 ▶use Log::Log4perl::Level;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 54 ▶use constant _INTERNAL_DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 54 our $unique_counter = 0;
15  
16 ##################################################
17 243 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
18 ##################################################
19 243 $unique_counter = 0;
20 }
21  
22 ##################################################
23 9 ▶sub unique_name {
Subroutine unique_name: called CC=1 SCAR=0.0
Pod undocumented
24 ##################################################
25 # THREADS: Need to lock here to make it thread safe
26 9 $unique_counter++;
27 9 my $unique_name = sprintf("app%03d", $unique_counter);
28 # THREADS: Need to unlock here to make it thread safe
29 9 return $unique_name;
30 }
31  
32 ##################################################
33 239 ▶sub new {
Subroutine new: called CC=11 SCAR=24.1
Pod documented
34 ##################################################
35 239 my($class, $appenderclass, %params) = @_;
36  
37 # Pull in the specified Log::Log4perl::Appender object
38 239 eval {
39  
40 # Eval erroneously succeeds on unknown appender classes if
41 # the eval string just consists of valid perl code (e.g. an
42 # appended ';' in $appenderclass variable). Fail if we see
43 # anything in there that can't be class name.
44 239 ▶ die "'$appenderclass' not a valid class name " if
Branch: 1 branch
BranchTrueFalseTotal
if $appenderclass =~ /[^:\w]/ 1 238 239
45 $appenderclass =~ /[^:\w]/;
46  
47 # Check if the class/package is already available because
48 # something like Class::Prototyped injected it previously.
49  
50 # Use UNIVERSAL::can to check the appender's new() method
51 # [RT 28987]
52 238 ▶ if( ! $appenderclass->can('new') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($appenderclass->can("new")) 26 212 238
53 # Not available yet, try to pull it in.
54 # see 'perldoc -f require' for why two evals
55 26 eval "require $appenderclass";
56 #unless ${$appenderclass.'::IS_LOADED'}; #for unit tests,
57 #see 004Config
58 26 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 25 26
59 }
60 };
61  
62 239 ▶ $@ and die "ERROR: can't load appenderclass '$appenderclass'\n$@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "ERROR: can't load appenderclass '${appenderclass}'\n$@" 2 237 239
63 237 print "Appender class $appenderclass loaded OK ($@)\n" if _INTERNAL_DEBUG;
64  
65 237 ▶ $params{name} = unique_name() unless exists $params{name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"name"} 9 228 237
66  
67 # If it's a Log::Dispatch::File appender, default to append
68 # mode (Log::Dispatch::File defaults to 'clobber') -- consensus 9/2002
69 # (Log::Log4perl::Appender::File already defaults to 'append')
70 237 ▶ if ($appenderclass eq 'Log::Dispatch::File' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($appenderclass eq "Log::Dispatch::File" and not exists $params{"mode"}) 0 237 237
Condition66%2/3
$appenderclass eq "Log::Dispatch::File" and not exists $params{"mode"}
!ll&&!rl&&r
23160
MC/DC0%0/2
$appenderclass eq "Log::Dispatch::File" and not exists $params{"mode"}
$appenderclass eq "Log::Dispatch::File" not exists $params{"mode"}
Truth table66%2/3
A and not(B)
A= $appenderclass eq "Log::Dispatch::File"B= not exists $params{"mode"}
ABresult
0X0
100
111
71 ! exists $params{mode}) {
72 0 $params{mode} = 'append';
73 }
74  
75 237 print "Calling $appenderclass new\n" if _INTERNAL_DEBUG;
76  
77 my $appender = $appenderclass->new(
78 # Set min_level to the lowest setting. *we* are
79 # controlling this now, the appender should just
80 # log it with no questions asked.
81 min_level => 'debug',
82 # Set 'name' and other parameters
83 237 map { $_ => $params{$_} } keys %params,
84 );
85  
86 237 print "Calling $appenderclass new returned OK\n" if _INTERNAL_DEBUG;
87  
88 my $self = {
89 appender => $appender,
90 name => $params{name},
91 237 layout => undef,
92 level => $ALL,
93 composite => 0,
94 };
95  
96 #whether to collapse arrays, etc.
97 237 $self->{warp_message} = $params{warp_message};
98  
99 237 ▶ if (!$INC{'Log/Log4perl/Config.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{"Log/Log4perl/Config.pm"}) 0 237 237
100 0 require Log::Log4perl::Config;
101 }
102  
103 237 ▶ if($self->{warp_message} and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"warp_message"} and my $cref = Log::Log4perl::Config::compile_if_perl($self->{"warp_message"})) 1 236 237
Condition100%3/3
$self->{"warp_message"} and my $cref = Log::Log4perl::Config::compile_if_perl($self->{"warp_message"})
!ll&&!rl&&r
23511
MC/DC100%2/2
$self->{"warp_message"} and my $cref = Log::Log4perl::Config::compile_if_perl($self->{"warp_message"})
$self->{"warp_message"} my $cref = Log::Log4perl::Config::compile_if_perl($self->{"warp_message"})
Truth table100%3/3
A and B
A= $self->{"warp_message"}B= my $cref = Log::Log4perl::Config::compile_if_perl($self->{"warp_message"})
ABresult
0X0
100
111
104 my $cref =
105 Log::Log4perl::Config::compile_if_perl($self->{warp_message})) {
106 1 $self->{warp_message} = $cref;
107 }
108
109 237 bless $self, $class;
110  
111 237 return $self;
112 }
113  
114 ##################################################
115 548 ▶sub composite { # Set/Get the composite flag
Subroutine composite: called CC=2 SCAR=6.9
Pod undocumented
116 ##################################################
117 548 my ($self, $flag) = @_;
118  
119 548 ▶ $self->{composite} = $flag if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 12 536 548
120 548 return $self->{composite};
121 }
122  
123 ##################################################
124 29 ▶sub threshold { # Set/Get the appender threshold
Subroutine threshold: called CC=3 SCAR=11.0
Pod undocumented
125 ##################################################
126 29 my ($self, $level) = @_;
127  
128 29 print "Setting threshold to $level\n" if _INTERNAL_DEBUG;
129  
130 29 ▶ if(defined $level) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level) 24 5 29
131 # Checking for \d makes for a faster regex(p)
132 24 ▶ $self->{level} = ($level =~ /^(\d+)$/) ? $level :
Branch: 1 branch
BranchTrueFalseTotal
$level =~ /^(\d+)$/ ? : 23 1 24
133 # Take advantage of &to_priority's error reporting
134 Log::Log4perl::Level::to_priority($level);
135 }
136  
137 29 return $self->{level};
138 }
139  
140 ##################################################
141 402 ▶sub log {
Subroutine log: called CC=13 SCAR=25.7
Pod undocumented
142 ##################################################
143 # Relay this call to Log::Log4perl::Appender:* or
144 # Log::Dispatch::*
145 ##################################################
146 402 my ($self, $p, $category, $level, $cache) = @_;
147  
148 # Check if the appender has a last-minute veto in form
149 # of an "appender threshold"
150 402 ▶ if($self->{level} > $
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"level"} > $Log::Log4perl::Level::PRIORITY{$level}) 22 380 402
151 Log::Log4perl::Level::PRIORITY{$level}) {
152 22 print "$self->{level} > $level, aborting\n" if _INTERNAL_DEBUG;
153 22 return undef;
154 }
155  
156 # Run against the (yes only one) customized filter (which in turn
157 # might call other filters via the Boolean filter) and check if its
158 # ok() method approves the message or blocks it.
159 380 ▶ if($self->{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filter"}) 30 350 380
160 30 ▶ if($self->{filter}->ok(%$p,
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'filter'}->ok(%$p, 'log4p_category', $category, 'log4p_level', $level)) { } 15 15 30
161 log4p_category => $category,
162 log4p_level => $level )) {
163 15 print "Filter $self->{filter}->{name} passes\n" if _INTERNAL_DEBUG;
164 } else {
165 15 print "Filter $self->{filter}->{name} blocks\n" if _INTERNAL_DEBUG;
166 15 return undef;
167 }
168 }
169  
170 365 ▶ unless($self->composite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->composite) 329 36 365
171  
172 #not defined, the normal case
173 329 ▶ if (! defined $self->{warp_message} ){
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $self->{'warp_message'}) { } 316 13 329
elsif (not $self->{'warp_message'}) { } 11 2 13
elsif (ref $self->{'warp_message'} eq 'CODE') { } 1 1 2
174 #join any message elements
175 316 ▶ if (ref $p->{message} eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p->{"message"} eq "ARRAY") 310 6 316
176 310 for my $i (0..$#{$p->{message}}) {
177 336 ▶ if( !defined $p->{message}->[ $i ] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p->{"message"}[$i]) 0 336 336
178 0 local $Carp::CarpLevel =
179 $Carp::CarpLevel + $Log::Log4perl::caller_depth + 1;
180 0 carp "Warning: Log message argument #" .
181 ($i+1) . " undefined";
182 }
183 }
184 $p->{message} =
185 join($Log::Log4perl::JOIN_MSG_ARRAY_CHAR,
186 310 @{$p->{message}}
187 );
188 }
189
190 #defined but false, e.g. Appender::DBI
191 } elsif (! $self->{warp_message}) {
192 ; #leave the message alone
193
194 } elsif (ref($self->{warp_message}) eq "CODE") {
195 #defined and a subref
196 $p->{message} =
197 1 [$self->{warp_message}->(@{$p->{message}})];
198 } else {
199 #defined and a function name?
200 54 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
201 $p->{message} =
202 1 [$self->{warp_message}->(@{$p->{message}})];
203 }
204  
205 $p->{message} = $self->{layout}->render($p->{message},
206 329 ▶ $category,
Branch: 1 branch
BranchTrueFalseTotal
if $self->layout 329 0 329
207 $level,
208 3 + $Log::Log4perl::caller_depth,
209 ) if $self->layout();
210 }
211  
212 365 my $args = [%$p, log4p_category => $category, log4p_level => $level];
213  
214 365 ▶ if(defined $cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache) { } 6 359 365
215 6 $$cache = $args;
216 } else {
217 359 $self->{appender}->log(@$args);
218 }
219  
220 365 return 1;
221 }
222  
223 ###########################################
224 6 ▶sub log_cached {
Subroutine log_cached: called CC=1 SCAR=0.0
Pod undocumented
225 ###########################################
226 6 my ($self, $cache) = @_;
227  
228 6 $self->{appender}->log(@$cache);
229 }
230  
231 ##################################################
232 180 ▶sub name { # Set/Get the name
Subroutine name: called CC=2 SCAR=7.6
Pod undocumented
233 ##################################################
234 180 my($self, $name) = @_;
235  
236 # Somebody wants to *set* the name?
237 180 ▶ if($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 180 180
238 0 $self->{name} = $name;
239 }
240  
241 180 return $self->{name};
242 }
243  
244 ###########################################
245 572 ▶sub layout { # Set/Get the layout object
Subroutine layout: called CC=3 SCAR=11.0
Pod documented
246 # associated with this appender
247 ###########################################
248 572 my($self, $layout) = @_;
249  
250 # Somebody wants to *set* the layout?
251 572 ▶ if($layout) {
Branch: 2 branches
BranchTrueFalseTotal
if ($layout) { } 243 329 572
elsif (not $self->{'layout'}) { } 6 323 329
252 243 $self->{layout} = $layout;
253  
254 # somebody wants a layout, but not set yet, so give 'em default
255 }elsif (! $self->{layout}) {
256 $self->{layout} = Log::Log4perl::Layout::SimpleLayout
257 6 ->new($self->{name});
258  
259 }
260  
261 572 return $self->{layout};
262 }
263  
264 ##################################################
265 12 ▶sub filter { # Set filter
Subroutine filter: called CC=2 SCAR=7.0
Pod undocumented
266 ##################################################
267 12 my ($self, $filter) = @_;
268  
269 12 ▶ if($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 12 0 12
270 12 print "Setting filter to $filter->{name}\n" if _INTERNAL_DEBUG;
271 12 $self->{filter} = $filter;
272 }
273  
274 12 return $self->{filter};
275 }
276  
277 ##################################################
278 138 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.1
279 ##################################################
280 # Relay everything else to the underlying
281 # Log::Log4perl::Appender::* or Log::Dispatch::*
282 # object
283 ##################################################
284 138 my $self = shift;
285  
286 54 ▶ no strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
287  
288 138 $AUTOLOAD =~ s/.*:://;
289  
290 138 ▶ if(! defined $self->{appender}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"appender"}) 0 138 138
291 0 die "Can't locate object method $AUTOLOAD() in ", __PACKAGE__;
292 }
293  
294 138 return $self->{appender}->$AUTOLOAD(@_);
295 }
296  
297 ##################################################
298 179 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
299 ##################################################
300 179 foreach my $key (keys %{$_[0]}) {
301 # print "deleting $key\n";
302 1091 delete $_[0]->{$key};
303 }
304 }
305  
306 54 1;
307  
308 __END__
« Log4perl/Logger.pm ↑ Summary Log4perl/Appender/Synchronized.pm »