lib/Catalyst/Log/Log4perl.pm

filter: Statementstmt 80.3% 102 / 127 Branchbran 53.8% 14 / 26 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 75.0% 18 / 24 Podpod 100% 5 / 5 totaltotal 73.5% 142 / 193 CC 19 SCAR 17.2
CC
19
Cov
74%
new
12.6
_flush
9.5
__ANON__
3.0
CRAP 5.6
↑ Summary
1 package Catalyst::Log::Log4perl;
2  
3 =head1 NAME
4  
5 Catalyst::Log::Log4perl - DEPRECATED (see Log::Log4perl::Catalyst)
6  
7 =head1 SYNOPSIS
8  
9 In MyApp.pm:
10  
11 use Catalyst::Log::Log4perl;
12  
13 # then we create a custom logger object for catalyst to use.
14 # If we don't supply any arguments to new, it will work almost
15 # like the default catalyst-logger.
16
17 __PACKAGE__->log(Catalyst::Log::Log4perl->new());
18  
19 # But the real power of Log4perl lies in the configuration, so
20 # lets try that. example.conf is included in the distribution,
21 # alongside the README and Changes.
22
23 __PACKAGE__->log(Catalyst::Log::Log4perl->new('example.conf'));
24
25 And later...
26  
27 $c->log->debug("This is using log4perl!");
28  
29 =head1 DESCRIPTION
30  
31 This module provides a L<Catalyst::Log> implementation that uses
32 L<Log::Log4perl> as the underlying log mechanism. It provides all
33 the methods listed in L<Catalyst::Log>, with the exception of:
34  
35 levels
36 enable
37 disable
38  
39 These methods simply return 0 and do nothing, as similar functionality
40 is already provided by L<Log::Log4perl>.
41  
42 These methods will all instantiate a logger with the component set to
43 the package who called it. For example, if you were in the
44 MyApp::C::Main package, the following:
45  
46 package MyApp::C::Main;
47  
48 sub default : Private {
49 my ( $self, $c ) = @_;
50 my $logger = $c->log;
51 $logger->debug("Woot!");
52 }
53  
54 Would send a message to the Myapp.C.Main L<Log::Log4perl> component.
55  
56 See L<Log::Log4perl> for more information on how to configure different
57 logging mechanisms based on the component.
58  
59 =head1 METHODS
60  
61 =over 4
62  
63 =cut
64  
65 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 5 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 5 ▶use Log::Log4perl::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 5 ▶use Log::Log4perl::Level;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 5 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 5 ▶use Data::Dump;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 5 ▶use Carp 'carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 5 our $VERSION = '1.06';
74  
75 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
76 5 carp 'Catalyst::Log::Log4perl is DEPRECATED, update your app to use Log::Log4perl::Catalyst';
77 }
78  
79 {
80 5 my @levels = qw[ debug info warn error fatal ];
81  
82 5 ▶ for ( my $i = 0; $i < @levels; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @levels) 25 5 30
83  
84 25 my $name = $levels[$i];
85 25 my $level = 1 << $i;
86  
87 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 25 ▶ *{$name} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
89 8 my ( $self, @message ) = @_;
90 8 my ( $package, $filename, $line ) = caller;
91 8 my $depth = $Log::Log4perl::caller_depth;
92 8 ▶ unless ( $depth > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($depth > 0) 8 0 8
93 8 $depth = 1;
94 }
95 8 my @info = ( $package, $name, $depth, \@message );
96 8 ▶ if ( $self->{override_cspecs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"override_cspecs"}) 8 0 8
97 8 my %caller;
98 8 @caller{qw/package filename line/} = caller;
99  
100 # I really have no idea why the correct subroutine
101 # is on a different call stack
102 8 $caller{subroutine} = ( caller(1) )[3]; #wtf?
103  
104 8 push @info, \%caller;
105 }
106 8 $self->_log( \@info );
107 8 return 1;
108 25 };
109  
110 25 ▶ *{"is_$name"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=4.6
111 0 my ( $self, @message ) = @_;
112 0 my ( $package, $filename, $line ) = caller;
113 0 my $logger = Log::Log4perl->get_logger($package);
114 0 my $func = "is_" . $name;
115 0 return $logger->$func;
116 25 };
117 }
118 }
119  
120 8 ▶sub _log {
Subroutine _log: called CC=1 SCAR=0.0
121 8 my $self = shift;
122 8 push @{ $self->{log4perl_stack} }, @_;
123 }
124  
125 0 ▶sub _dump {
Subroutine _dump: not called CC=1 SCAR=6.9
126 0 my $self = shift;
127 0 $self->debug( Data::Dump::dump(@_) );
128 }
129  
130 =item new($config, [%options])
131  
132 This builds a new L<Catalyst::Log::Log4perl> object. If you provide an argument
133 to new(), it will be passed directly to Log::Log4perl::init.
134  
135 The second (optional) parameter is a hash with extra options. Currently
136 three additional parameters are defined:
137  
138 'autoflush' - Set it to a true value to disable abort(1) support.
139 'watch_delay' - Set it to a true value to use L<Log::Log4perl>'s init_and_watch
140  
141 'override_cspecs' - EXPERIMENTAL
142 Set it to a true value to locally override some parts of
143 L<Log::Log4perl::Layout::PatternLayout>. See L<OVERRIDING CSPECS> below
144  
145 Without any arguments, new() will initialize a root logger with a single appender,
146 L<Log::Log4perl::Appender::Screen>, configured to have an identical layout to
147 the default L<Catalyst::Log> object.
148  
149 =cut
150  
151 4 ▶sub new {
Subroutine new: called CC=9 SCAR=25.3
Pod documented
152 4 my $self = shift;
153 4 my $config = shift;
154 4 my %options = @_;
155  
156 4 my %foo;
157 4 my $ref = \%foo;
158  
159 4 my $watch_delay = 0;
160 4 ▶ if ( exists( $options{'watch_delay'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"watch_delay"}) 0 4 4
161 0 ▶ if ( $options{'watch_delay'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"watch_delay"}) 0 0 0
162 0 $watch_delay = $options{'watch_delay'};
163 }
164 }
165 4 ▶ unless ( Log::Log4perl->initialized ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Log::Log4perl"->initialized) 4 0 4
166 4 ▶ if ( defined($config) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) { } 4 0 4
167 4 ▶ if ($watch_delay) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watch_delay) { } 0 4 4
168 0 Log::Log4perl::init_and_watch( $config, $watch_delay );
169 } else {
170 4 Log::Log4perl::init($config);
171 }
172 } else {
173 0 my $log = Log::Log4perl->get_logger("");
174 0 my $layout =
175 Log::Log4perl::Layout::PatternLayout->new(
176 "[%d] [catalyst] [%p] %m%n");
177 0 my $appender = Log::Log4perl::Appender->new(
178 "Log::Log4perl::Appender::Screen",
179 'name' => 'screenlog',
180 'stderr' => 1,
181 );
182 0 $appender->layout($layout);
183 0 $log->add_appender($appender);
184 0 $log->level($DEBUG);
185 }
186 }
187  
188 4 ▶ $ref->{autoflush} = $options{autoflush} || 0;
Condition50%1/2
$options{'autoflush'} || 0
l!l
04
MC/DC0%0/1
$options{'autoflush'} || 0
$options{'autoflush'}
Truth table50%1/2
A
A= $options{'autoflush'}
Aresult
11
00
189  
190 4 ▶ $ref->{override_cspecs} = $options{override_cspecs} || 0;
Condition50%1/2
$options{'override_cspecs'} || 0
l!l
40
MC/DC0%0/1
$options{'override_cspecs'} || 0
$options{'override_cspecs'}
Truth table50%1/2
A
A= $options{'override_cspecs'}
Aresult
11
00
191  
192 4 ▶ if ( $ref->{override_cspecs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"override_cspecs"}) 4 0 4
193 4 @{ $ref->{local_cspecs} }{qw/L F C M l/} = (
194 2 ▶ sub { $ref->{context}->{line} },
Subroutine __ANON__: called CC=1 SCAR=0.0
195 1 ▶ sub { $ref->{context}->{filename} },
Subroutine __ANON__: called CC=1 SCAR=0.0
196 1 ▶ sub { $ref->{context}->{package} },
Subroutine __ANON__: called CC=1 SCAR=0.0
197 1 ▶ sub { $ref->{context}->{subroutine} },
Subroutine __ANON__: called CC=1 SCAR=0.0
198 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
199 sprintf '%s %s (%d)',
200 1 @{ $ref->{context} }{qw/subroutine filename line/};
201 }
202 4 );
203 }
204  
205 4 $ref->{abort} = 0;
206 4 $ref->{log4perl_stack} = [];
207  
208 4 bless $ref, $self;
209  
210 4 return $ref;
211 }
212  
213 =item _flush()
214  
215 Flushes the cache. Much like the way Catalyst::Log does it.
216  
217 =cut
218  
219 12 ▶sub _flush {
Subroutine _flush: called CC=9 SCAR=22.6
220 12 my ($self) = @_;
221  
222 12 local $SIG{CHLD} = 'DEFAULT'; # Some log backends spawn processes, and
223 # don't play nicely unless we do this.
224  
225 12 my @stack = @{ $self->{log4perl_stack} };
226 12 $self->{log4perl_stack} = [];
227 12 ▶ if ( !$self->{autoflush} and $self->{abort} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"autoflush"} and $self->{"abort"}) 0 12 12
Condition33%1/3
not $self->{"autoflush"} and $self->{"abort"}
!ll&&!rl&&r
0120
MC/DC0%0/2
not $self->{"autoflush"} and $self->{"abort"}
not $self->{"autoflush"} $self->{"abort"}
Truth table33%1/3
not(A) and B
A= not $self->{"autoflush"}B= $self->{"abort"}
ABresult
0X0
100
111
228 0 $self->{abort} = 0;
229 0 return 0;
230 }
231  
232 12 foreach my $logmsg (@stack) {
233 8 my ( $package, $type, $depth, $message ) = @{$logmsg}[ 0 .. 3 ];
234 8 ▶ $self->{context} = $logmsg->[-1] if $self->{override_cspecs};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"override_cspecs"} 8 0 8
235  
236 # fetch all instances of pattern layouts
237 8 my @patterns;
238 8 ▶ if ( $self->{override_cspecs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"override_cspecs"}) 8 0 8
239 @patterns =
240 8 grep { $_->isa('Log::Log4perl::Layout::PatternLayout') }
241 8 map { $_->layout } values %{ Log::Log4perl->appenders() };
242 }
243  
244 # localize the cspecs so we don't disturb modules that
245 # directly operate on Log4perl
246 8 local $_->{USER_DEFINED_CSPECS} for @patterns;
247  
248 8 for my $layout (@patterns) {
249 8 ▶ while ( my ( $cspec, $subref ) = each %{ $self->{local_cspecs} } )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"local_cspecs"};}) 40 8 48
250 {
251  
252 # overriding USER_DEFINED_CSPECS relies on an missing internal
253 # check in Log4perl: cspecs that collide with a predefined one
254 # can't be added via the API but are executed nonetheless
255 # and override the originals. This behaviour is only verified
256 # with version 1.08 of Log::Log4perl
257 40 $layout->{USER_DEFINED_CSPECS}->{$cspec} = $subref;
258 }
259 }
260  
261 8 local $Log::Log4perl::caller_depth = $depth;
262  
263 8 my $logger = Log::Log4perl->get_logger($package);
264 8 $logger->$type(@$message);
265 }
266 }
267  
268 =item abort($abort)
269  
270 Causes the current log-object to not log anything, effectivly shutting
271 up this request, making it disapear from the logs.
272  
273 =cut
274  
275 0 ▶sub abort {
Subroutine abort: not called CC=1 SCAR=6.9
Pod documented
276 0 my $self = shift;
277 0 my $abort = shift;
278 0 $self->{abort} = $abort;
279 0 return $self->{abort};
280 }
281  
282 =item debug($message)
283  
284 Passes it's arguments to $logger->debug.
285  
286 =item info($message)
287  
288 Passes it's arguments to $logger->info.
289  
290 =item warn($message)
291  
292 Passes it's arguments to $logger->warn.
293  
294 =item error($message)
295  
296 Passes it's arguments to $logger->error.
297  
298 =item fatal($message)
299  
300 Passes it's arguments to $logger->fatal.
301  
302 =item is_debug()
303  
304 Calls $logger->is_debug.
305  
306 =item is_info()
307  
308 Calls $logger->is_info.
309  
310 =item is_warn()
311  
312 Calls $logger->is_warn.
313  
314 =item is_error()
315  
316 Calls $logger->is_error.
317  
318 =item is_fatal()
319  
320 Calls $logger->is_fatal.
321  
322 =item levels()
323  
324 This method does nothing but return "0". You should use L<Log::Log4perl>'s
325 built in mechanisms for setting up log levels.
326  
327 =cut
328  
329 0 ▶sub levels {
Subroutine levels: not called CC=1 SCAR=6.9
Pod documented
330 0 return 0;
331 }
332  
333 =item enable()
334  
335 This method does nothing but return "0". You should use L<Log::Log4perl>'s
336 built in mechanisms for enabling log levels.
337  
338 =cut
339  
340 0 ▶sub enable {
Subroutine enable: not called CC=1 SCAR=6.9
Pod documented
341 0 return 0;
342 }
343  
344 =item disable()
345  
346 This method does nothing but return "0". You should use L<Log::Log4perl>'s
347 built in mechanisms for disabling log levels.
348  
349 =cut
350  
351 0 ▶sub disable {
Subroutine disable: not called CC=1 SCAR=6.9
Pod documented
352 0 return 0;
353 }
354  
355 5 1;
356  
357 __END__
↑ Summary