Acrux/Log.pm

filter: Statementstmt 70.2% 106 / 151 Branchbran 53.4% 31 / 58 Conditioncond 54.3% 25 / 46 MC/DCmcdc 26.9% 7 / 26 Subroutinesub 69.7% 23 / 33 Podpod 100% 16 / 16 totaltotal 63.0% 208 / 330 CC 49 SCAR 34.2
CC
49
Cov
64%
new
72.5
_log
19.3
level
9.8
CRAP 30.7
« Acrux/FileLock.pm ↑ Summary Acrux/Config.pm »
1 package Acrux::Log;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 Acrux::Log - Acrux logger
10  
11 =head1 SYNOPSIS
12  
13 use Acrux::Log;
14  
15 # Logging to STDERR (by default)
16 my $log = Acrux::Log->new();
17 $log->error("My test error message to STDERR")
18  
19 # Using file
20 my $log = Acrux::Log->new(file => '/tmp/test.log');
21 $log->error("My test error message to /tmp/test.log")
22  
23 # Using STDOUT
24 my $log = Acrux::Log->new(file => 'stdout'); # or 'stdout:', ':stdout'
25 my $log = Acrux::Log->new(file => '-');
26  
27 # Using STDOUT (handle)
28 my $log = Acrux::Log->new(
29 handle => IO::Handle->new_from_fd(fileno(STDOUT), "w")
30 );
31  
32 # Using STDERR (default since 0.10)
33 my $log = Acrux::Log->new(file => 'stderr'); # or 'stderr:', ':stderr'
34 my $log = Acrux::Log->new(file => '=');
35  
36 # Using syslog
37 my $log = Acrux::Log->new(file => 'syslog'); # or 'syslog:', ':syslog'
38 my $log = Acrux::Log->new(file => '@');
39  
40 # Customize minimum log level
41 my $log = Acrux::Log->new(level => 'warn');
42  
43 # Log messages
44 $log->trace('Doing stuff');
45 $log->debug('Not sure what is happening here');
46 $log->info('FYI: it happened again');
47 $log->notice('Normal, but significant, condition...');
48 $log->warn('This might be a problem');
49 $log->error('Garden variety error');
50 $log->fatal('Boom');
51 $log->crit('Its over...');
52 $log->alert('Action must be taken immediately');
53 $log->emerg('System is unusable');
54  
55 =head1 DESCRIPTION
56  
57 Acrux::Log is a simple logger for Acrux logging
58  
59 =head2 new
60  
61 my $log = Acrux::Log->new(
62 logopt => 'ndelay,pid',
63 facility => 'user',
64 level => 'debug',
65 ident => 'test.pl',
66 autoclean => 1,
67 logopt => 'ndelay,pid',
68 );
69  
70 With default attributes
71  
72 use Mojo::Log;
73 my $log = Acrux::Log->new( logger => Mojo::Log->new );
74 $log->error("Test error message");
75  
76 This is example with external loggers
77  
78 =head1 ATTRIBUTES
79  
80 This class implements the following attributes
81  
82 =head2 autoclean
83  
84 autoclean => 1
85  
86 This attribute enables cleaning (closing file handler or syslog) on DESTROY
87  
88 =head2 color
89  
90 color => 1
91  
92 Colorize log messages with the available levels using L<Term::ANSIColor>, defaults to C<0>
93  
94 =head2 facility
95  
96 facility => 'user'
97  
98 This attribute sets facility for logging
99  
100 Available standard facilities: C<auth>, C<authpriv>, C<cron>, C<daemon>, C<ftp>,
101 C<kern>, C<local0>, C<local1>, C<local2>, C<local3>, C<local4>, C<local5>, C<local6>,
102 C<local7>, C<lpr>, C<mail>, C<news>, C<syslog>, C<user> and C<uucp>
103  
104 Default: C<user> (Sys::Syslog::LOG_USER)
105  
106 See also L<Sys::Syslog/Facilities>
107  
108 =head2 file
109  
110 file => '/var/log/myapp.log'
111 file => 'stdout' # 'stdout:', ':stdout', '-'
112 file => 'stderr' # 'stderr:', ':stderr', '='
113 file => 'syslog' # 'syslog:', ':syslog', '@'
114  
115 Log file path used by "handle"
116  
117 B<Compatibility note:>
118  
119 Prior to version 0.10, Acrux::Log implicitly used syslog when no logging
120 destination was specified.
121 Starting with version 0.10, the default destination is STDERR.
122  
123 To continue using syslog, configure it explicitly:
124  
125 file => "syslog:"
126  
127 or
128  
129 file => "@"
130  
131 =head2 format
132  
133 format => sub {...}
134  
135 A callback function for formatting log messages
136  
137 format => sub {
138 my ($time, $level, @lines) = @_;
139 return "[$time] [$level] " . join (' ', @lines) . "\n";
140 }
141  
142 This callback routine must return formatted string for the log line
143  
144 =head2 handle
145  
146 handle => IO::Handle->new_from_fd(fileno(STDOUT), "w")
147  
148 Log filehandle, defaults to opening "file" or uses syslog if file not specified
149  
150 =head2 ident
151  
152 ident => 'myapp'
153  
154 The B<ident> is prepended to every B<syslog> message
155  
156 Default: script name C<basename($0)>
157  
158 =head2 level
159  
160 level => 'debug'
161  
162 There are six predefined log levels: C<fatal>, C<error>, C<warn>, C<info>, C<debug>, and C<trace> (in descending priority).
163 The syslog supports followed additional log levels: C<emerg>, C<alert>, C<crit'> and C<notice> (in descending priority).
164 But we recommend not using them to maintain compatibility.
165 Your configured logging level has to at least match the priority of the logging message.
166  
167 If your configured logging level is C<warn>, then messages logged with info(), debug(), and trace()
168 will be suppressed; fatal(), error() and warn() will make their way through, because their
169 priority is higher or equal than the configured setting.
170  
171 Default: C<debug>
172  
173 See also L<Sys::Syslog/Levels>
174  
175 =head2 logger
176  
177 logger => Mojo::Log->new()
178  
179 This attribute perfoms to set predefined logger, eg. Mojo::Log
180  
181 Default: C<undef>
182  
183 =head2 logopt
184  
185 logopt => 'ndelay,pid'
186  
187 This attribute contains zero or more of the options detailed in L<Sys::Syslog/openlog>
188  
189 Default: C<'ndelay,pid'>
190  
191 =head2 prefix
192  
193 prefix => '>>>'
194  
195 The B<prefix> is prepended to every C<handled> log message
196  
197 Default: null
198  
199 =head2 short
200  
201 short => 1
202  
203 Generate short log messages without a timestamp but with log level prefix, defaults to C<0>
204  
205 =head1 METHODS
206  
207 This class implements the following methods
208  
209 =head2 alert
210  
211 $log->alert('Action must be taken immediately');
212 $log->alert('Real', 'problem');
213  
214 Log C<alert> message
215  
216 =head2 crit
217  
218 $log->crit('Its over...');
219 $log->crit('Bye', 'bye');
220  
221 Log C<crit> message (See L</fatal> method)
222  
223 =head2 debug
224  
225 $log->debug('You screwed up, but that is ok');
226 $log->debug('All', 'cool');
227  
228 Log C<debug> message
229  
230 =head2 emerg
231  
232 $log->emerg('System is unusable');
233 $log->emerg('To', 'die');
234  
235 Log C<emerg> message
236  
237 =head2 error
238  
239 $log->error('You really screwed up this time');
240 $log->error('Wow', 'seriously');
241  
242 Log C<error> message
243  
244 =head2 fatal
245  
246 $log->fatal('Its over...');
247 $log->fatal('Bye', 'bye');
248  
249 Log C<fatal> message
250  
251 =head2 file
252  
253 my $file = $log->file;
254  
255 This method returns file path
256  
257 =head2 info
258  
259 $log->info('You are bad, but you prolly know already');
260 $log->info('Ok', 'then');
261  
262 Log C<info> message
263  
264 =head2 level
265  
266 my $level = $log->level;
267 $log = $log->level('debug');
268  
269 Active log level, defaults to debug.
270 Available log levels are C<trace>, C<debug>, C<info>, C<notice>, C<warn>, C<error>,
271 C<fatal> (C<crit>), C<alert> and C<emerg>, in that order
272  
273 =head2 logger
274  
275 my $logger = $log->logger;
276  
277 This method returns the logger object or undef if not exists
278  
279 =head2 notice
280  
281 $log->notice('Normal, but significant, condition...');
282 $log->notice('Ok', 'then');
283  
284 Log C<notice> message
285  
286 =head2 provider
287  
288 print $log->provider;
289  
290 Returns provider name (C<external>, C<handle>, C<file> or C<syslog>)
291  
292 =head2 trace
293  
294 $log->trace('Whatever');
295 $log->trace('Who', 'cares');
296  
297 Log C<trace> message
298  
299 =head2 warn
300  
301 $log->warn('Dont do that Dave...');
302 $log->warn('No', 'really');
303  
304 Log C<warn> message
305  
306 =head1 HISTORY
307  
308 See C<Changes> file
309  
310 =head1 TO DO
311  
312 See C<TODO> file
313  
314 =head1 SEE ALSO
315  
316 L<Sys::Syslog>
317  
318 =head1 AUTHOR
319  
320 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
321  
322 =head1 COPYRIGHT
323  
324 Copyright (C) 1998-2026 D&D Corporation
325  
326 =head1 LICENSE
327  
328 This program is distributed under the terms of the Artistic License Version 2.0
329  
330 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
331  
332 =cut
333  
334 1 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
335 1 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
336 1 ▶use Sys::Syslog qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
337 1 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
338 1 ▶use IO::File qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
339 1 ▶use Fcntl qw/:flock/;
Subroutine BEGIN: called CC=1 SCAR=0.0
340 1 ▶use Encode qw/find_encoding/;
Subroutine BEGIN: called CC=1 SCAR=0.0
341 1 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
342 1 ▶use Acrux::Util qw/color/;
Subroutine BEGIN: called CC=1 SCAR=0.0
343  
344 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
345 1 LOGOPTS => 'ndelay,pid', # For Sys::Syslog
346 SEPARATOR => ' ',
347 LOGFORMAT => '%s',
348 1 };
349 1 my %LOGLEVELS = (
350 'trace' => Sys::Syslog::LOG_DEBUG, # 7 debug-level message
351 'debug' => Sys::Syslog::LOG_DEBUG, # 7 debug-level message
352 'info' => Sys::Syslog::LOG_INFO, # 6 informational message
353 'notice' => Sys::Syslog::LOG_NOTICE, # 5 normal, but significant, condition
354 'warn' => Sys::Syslog::LOG_WARNING, # 4 warning conditions
355 'error' => Sys::Syslog::LOG_ERR, # 3 error conditions
356 'fatal' => Sys::Syslog::LOG_CRIT, # 2 critical conditions
357 'crit' => Sys::Syslog::LOG_CRIT, # 2 critical conditions
358 'alert' => Sys::Syslog::LOG_ALERT, # 1 action must be taken immediately
359 'emerg' => Sys::Syslog::LOG_EMERG, # 0 system is unusable
360 );
361 1 my %MAGIC = (
362 'trace' => 8,
363 'debug' => 7,
364 'info' => 6,
365 'notice' => 5,
366 'warn' => 4, 'warning' => 4,
367 'error' => 3, 'err' => 3,
368 'fatal' => 2, 'crit' => 2, 'critical' => 2,
369 'alert' => 1,
370 'emerg' => 0, 'emergency' => 0,
371 );
372 1 my %COLORS = (
373 'trace' => 'white',
374 'debug' => 'bright_white',
375 'info' => 'cyan',
376 'notice' => 'green',
377 'warn' => 'yellow',
378 'error' => 'red',
379 'fatal' => 'bright_red', 'crit' => 'bright_magenta',
380 'alert' => 'white on_red',
381 'emerg' => 'bright_white on_red',
382 );
383 1 my %SHORT = ( # Log::Log4perl::Level notation
384 0 => 'fatal', 1 => 'fatal', 2 => 'fatal',
385 3 => 'error',
386 4 => 'warn',
387 5 => 'info', 6 => 'info',
388 7 => 'debug',
389 8 => 'trace',
390 );
391  
392 1 ▶my $ENCODING = find_encoding('UTF-8') or croak qq/Encoding "UTF-8" not found/;
Branch: 1 branch
BranchTrueFalseTotal
my $ENCODING = &find_encoding("UTF-8") or croak("Encoding \"UTF-8\" not found") 1 0 1
393  
394 5 ▶sub new {
Subroutine new: called CC=29 SCAR=42.8
Pod documented
395 5 my $class = shift;
396 0 ▶ my $args = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {};
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 5 0 5
@_ > 1 ? : 5 0 5
397 5 ▶ $args->{facility} ||= Sys::Syslog::LOG_USER;
Condition50%1/2
$args->{"facility"} ||= 8
l!l
05
MC/DC0%0/1
$args->{"facility"} ||= 8
$args->{"facility"}
Truth table50%1/2
A
A= $args->{"facility"}
Aresult
11
00
398 5 ▶ $args->{ident} ||= basename($0);
Condition33%1/3
$args->{"ident"} ||= basename($0)
l!l&&r!l&&!r
050
MC/DC0%0/2
$args->{"ident"} ||= basename($0)
$args->{"ident"} basename($0)
Truth table33%1/3
A ||= B
A= $args->{"ident"}B= basename($0)
ABresult
1X1
011
000
399 5 ▶ $args->{logopt} ||= LOGOPTS;
Condition50%1/2
$args->{"logopt"} ||= "ndelay,pid"
l!l
05
MC/DC0%0/1
$args->{"logopt"} ||= "ndelay,pid"
$args->{"logopt"}
Truth table50%1/2
A
A= $args->{"logopt"}
Aresult
11
00
400 5 ▶ $args->{logger} ||= undef;
Condition100%2/2
$args->{"logger"} ||= undef
l!l
14
MC/DC100%1/1
$args->{"logger"} ||= undef
$args->{"logger"}
Truth table100%2/2
A
A= $args->{"logger"}
Aresult
11
00
401 5 ▶ $args->{level} ||= 'debug';
Condition100%2/2
$args->{"level"} ||= "debug"
l!l
14
MC/DC100%1/1
$args->{"level"} ||= "debug"
$args->{"level"}
Truth table100%2/2
A
A= $args->{"level"}
Aresult
11
00
402 5 ▶ $args->{file} ||= undef;
Condition100%2/2
$args->{"file"} ||= undef
l!l
32
MC/DC100%1/1
$args->{"file"} ||= undef
$args->{"file"}
Truth table100%2/2
A
A= $args->{"file"}
Aresult
11
00
403 5 ▶ $args->{handle} ||= undef;
Condition100%2/2
$args->{"handle"} ||= undef
l!l
14
MC/DC100%1/1
$args->{"handle"} ||= undef
$args->{"handle"}
Truth table100%2/2
A
A= $args->{"handle"}
Aresult
11
00
404 5 $args->{provider} = 'unknown';
405 5 ▶ $args->{autoclean} ||= 0;
Condition50%1/2
$args->{"autoclean"} ||= 0
l!l
05
MC/DC0%0/1
$args->{"autoclean"} ||= 0
$args->{"autoclean"}
Truth table50%1/2
A
A= $args->{"autoclean"}
Aresult
11
00
406 5 ▶ $args->{prefix} ||= '';
Condition100%2/2
$args->{"prefix"} ||= ""
l!l
41
MC/DC100%1/1
$args->{"prefix"} ||= ""
$args->{"prefix"}
Truth table100%2/2
A
A= $args->{"prefix"}
Aresult
11
00
407 5 ▶ $args->{format} ||= undef;
Condition50%1/2
$args->{"format"} ||= undef
l!l
05
MC/DC0%0/1
$args->{"format"} ||= undef
$args->{"format"}
Truth table50%1/2
A
A= $args->{"format"}
Aresult
11
00
408 5 ▶ $args->{color} ||= 0;
Condition50%1/2
$args->{"color"} ||= 0
l!l
05
MC/DC0%0/1
$args->{"color"} ||= 0
$args->{"color"}
Truth table50%1/2
A
A= $args->{"color"}
Aresult
11
00
409  
410 # Check level
411 5 $args->{level} = lc($args->{level});
412 5 ▶ unless (exists $MAGIC{$args->{level}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $MAGIC{$args->{"level"}}) 0 5 5
413 0 carp "Incorrect log level specified. Well be used debug log level by default";
414 0 $args->{level} = 'debug';
415 }
416  
417 # Instance
418 5 my $self = bless {%$args}, $class;
419  
420 # Set formatter
421 5 ▶ $self->{format} ||= $self->{short} ? \&_short : $self->{color} ? \&_color : \&_default;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'short'} ? : 0 5 5
$self->{'color'} ? : 0 5 5
Condition33%1/3
$self->{"format"} ||= $self->{"short"} ? \&Acrux::Log::_short : ($self->{"color"} ? \&Acrux::Log::_color : \&Acrux::Log::_default)
l!l&&r!l&&!r
050
MC/DC0%0/2
$self->{"format"} ||= $self->{"short"} ? \&Acrux::Log::_short : ($self->{"color"} ? \&Acrux::Log::_color : \&Acrux::Log::_default)
$self->{"format"} $self->{"short"} ? \&Acrux::Log::_short : ($self->{"color"} ? \&Acrux::Log::_color : \&Acrux::Log::_default)
Truth table33%1/3
A ||= B
A= $self->{"format"}B= $self->{"short"} ? \&Acrux::Log::_short : ($self->{"color"} ? \&Acrux::Log::_color : \&Acrux::Log::_default)
ABresult
1X1
011
000
422  
423 # External logger object specified directly
424 5 ▶ if ($args->{logger}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args->{'logger'}) { } 1 4 5
elsif ($args->{'handle'}) { } 1 3 4
elsif ($args->{'file'}) { } 3 0 3
425 1 $self->{provider} = "external";
426 1 ▶ unless (blessed($args->{logger})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($args->{"logger"})) 0 1 1
427 0 printf STDERR "Blessed reference expected in \"logger\" attribute. Logging to STDERR instead.\n";
428 0 $self->{provider} = "handle";
429 0 $self->{handle} = IO::Handle->new_from_fd(fileno(STDERR), "w");
430 }
431 }
432  
433 # Handler specified directly
434 elsif ($args->{handle}) {
435 1 $self->{provider} = "handle";
436 1 return $self;
437 }
438  
439 # File rules
440 elsif ($args->{file}) { # File
441 3 my $file = $args->{file};
442  
443 # Open syslog socket
444 3 ▶ if ($file =~ /^\:?syslog\:?$/i or $file eq '@') {
Branch: 3 branches
BranchTrueFalseTotal
if ($file =~ /^\:?syslog\:?$/i or $file eq '@') { } 0 3 3
elsif ($file =~ /^\:?stdout\:?$/i or $file eq '-') { } 2 1 3
elsif ($file =~ /^\:?stderr\:?$/i or $file eq '=') { } 0 1 1
Condition33%1/3
$file =~ /^\:?syslog\:?$/i or $file eq "\@"
l!l&&r!l&&!r
003
MC/DC0%0/2
$file =~ /^\:?syslog\:?$/i or $file eq "\@"
$file =~ /^\:?syslog\:?$/i $file eq "\@"
Truth table33%1/3
A or B
A= $file =~ /^\:?syslog\:?$/iB= $file eq "\@"
ABresult
1X1
011
000
445 0 Sys::Syslog::openlog($args->{ident}, $args->{logopt}, $args->{facility});
446 0 $self->{provider} = "syslog";
447 0 $self->{file} = "syslog";
448 }
449  
450 # Use STDOUT handle
451 ▶ elsif ($file =~ /^\:?stdout\:?$/i or $file eq '-') {
Condition66%2/3
$file =~ /^\:?stdout\:?$/i or $file eq "-"
l!l&&r!l&&!r
021
MC/DC50%1/2
$file =~ /^\:?stdout\:?$/i or $file eq "-"
$file =~ /^\:?stdout\:?$/i $file eq "-"
Truth table66%2/3
A or B
A= $file =~ /^\:?stdout\:?$/iB= $file eq "-"
ABresult
1X1
011
000
452 2 $self->{provider} = "handle";
453 2 $self->{handle} = IO::Handle->new_from_fd(fileno(STDOUT), "w");
454 2 $self->{file} = "stdout";
455 }
456  
457 # Use STDERR handle
458 ▶ elsif ($file =~ /^\:?stderr\:?$/i or $file eq '=') {
Condition33%1/3
$file =~ /^\:?stderr\:?$/i or $file eq "="
l!l&&r!l&&!r
001
MC/DC0%0/2
$file =~ /^\:?stderr\:?$/i or $file eq "="
$file =~ /^\:?stderr\:?$/i $file eq "="
Truth table33%1/3
A or B
A= $file =~ /^\:?stderr\:?$/iB= $file eq "="
ABresult
1X1
011
000
459 0 $self->{provider} = "handle";
460 0 $self->{handle} = IO::Handle->new_from_fd(fileno(STDERR), "w");
461 0 $self->{file} = "stderr";
462 }
463  
464 # Open log file handle
465 else {
466 1 $self->{provider} = "file";
467 1 $self->{handle} = IO::File->new($file, ">>");
468 1 ▶ unless (defined $self->{handle}) { # Error
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"handle"}) 0 1 1
469 0 printf STDERR "Can't open log file \"%s\" for writing (%s). Logging to STDERR instead.\n",
470 $file, $!;
471 0 $self->{provider} = "handle";
472 0 $self->{handle} = IO::Handle->new_from_fd(fileno(STDERR), "w");
473 }
474 }
475 }
476  
477 # Default: STDERR (since 0.10)
478 else {
479 0 $self->{provider} = "handle";
480 0 $self->{handle} = IO::Handle->new_from_fd(fileno(STDERR), "w");
481 }
482  
483 4 return $self;
484 }
485 0 ▶sub file { shift->{file} }
Subroutine file: not called CC=1 SCAR=6.9
Pod documented
486 11 ▶sub level {
Subroutine level: called CC=4 SCAR=22.9
Pod documented
487 11 my $self = shift;
488 11 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) 0 11 11
489 0 ▶ my $level = lc(shift // '');
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
490 0 ▶ if (exists $MAGIC{$level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $MAGIC{$level}) { } 0 0 0
491 0 $self->{level} = $level;
492 } else {
493 0 carp "Incorrect log level specified";
494 }
495 0 return $self;
496 }
497 11 return $self->{level};
498 }
499 8 ▶sub logger { shift->{logger} }
Subroutine logger: called CC=1 SCAR=0.0
Pod documented
500 6 ▶sub handle { shift->{handle} }
Subroutine handle: called CC=1 SCAR=0.0
Pod documented
501 0 ▶sub provider { shift->{provider} }
Subroutine provider: not called CC=1 SCAR=6.9
Pod documented
502  
503 0 ▶sub trace { shift->_log('trace', @_) }
Subroutine trace: not called CC=1 SCAR=6.9
Pod documented
504 0 ▶sub debug { shift->_log('debug', @_) }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
505 3 ▶sub info { shift->_log('info', @_) }
Subroutine info: called CC=1 SCAR=0.0
Pod documented
506 0 ▶sub notice { shift->_log('notice', @_) }
Subroutine notice: not called CC=1 SCAR=6.9
Pod documented
507 1 ▶sub warn { shift->_log('warn', @_) }
Subroutine warn: called CC=1 SCAR=0.0
Pod documented
508 4 ▶sub error { shift->_log('error', @_) }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
509 1 ▶sub fatal { shift->_log('fatal', @_) }
Subroutine fatal: called CC=1 SCAR=0.0
Pod documented
510 0 ▶sub crit { shift->_log('crit', @_) }
Subroutine crit: not called CC=1 SCAR=6.9
Pod documented
511 0 ▶sub alert { shift->_log('alert', @_) }
Subroutine alert: not called CC=1 SCAR=6.9
Pod documented
512 0 ▶sub emerg { shift->_log('emerg', @_) }
Subroutine emerg: not called CC=1 SCAR=6.9
Pod documented
513  
514 9 ▶sub _log {
Subroutine _log: called CC=13 SCAR=29.6
515 9 my ($self, $level, @msg) = @_;
516 9 my $req = $MAGIC{$self->level};
517 9 ▶ my $mag = $MAGIC{$level} // 7;
Condition50%1/2
$MAGIC{$level} // 7
l!l
90
MC/DC0%0/1
$MAGIC{$level} // 7
$MAGIC{$level}
Truth table50%1/2
A
A= $MAGIC{$level}
Aresult
11
00
518 9 ▶ return 0 unless $mag <= $req;
Branch: 1 branch
BranchTrueFalseTotal
unless $mag <= $req 1 8 9
519  
520 # External logger
521 8 ▶ if (my $logger = $self->logger) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $self->logger) 2 6 8
522 2 my $name = $SHORT{$mag};
523 2 ▶ if (my $code = $logger->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $logger->can($name)) { } 2 0 2
524 2 return $logger->$code(@msg);
525 } else {
526 0 carp(sprintf("Can't found '%s' method in '%s' package", $name, ref($logger)));
527 }
528 0 return 0;
529 }
530  
531 # Handle
532 6 ▶ if (my $handle = $self->handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $self->handle) 6 0 6
533 # Set message
534 6 ▶ my $pfx = (defined($self->{prefix}) && length($self->{prefix})) ? $self->{prefix} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'prefix'} && length $self->{'prefix'} ? : 3 3 6
Condition66%2/3
defined $self->{"prefix"} and length $self->{"prefix"}
!ll&&!rl&&r
033
MC/DC50%1/2
defined $self->{"prefix"} and length $self->{"prefix"}
defined $self->{"prefix"} length $self->{"prefix"}
Truth table66%2/3
A and B
A= defined $self->{"prefix"}B= length $self->{"prefix"}
ABresult
0X0
100
111
535 6 my $_msg = $ENCODING->encode($pfx . $self->{format}->(time, $level, @msg), 0);
536  
537 # Flush
538 6 ▶ if ($self->{provider} eq "file") { # Flush to file
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'provider'} eq 'file') { } 3 3 6
elsif ($self->{'provider'} eq 'handle') { } 3 0 3
539 3 flock $handle, LOCK_EX;
540 3 ▶ $handle->print($_msg) or croak "Can't write to log file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$handle->print($_msg) or croak("Can't write to log file: $!") 3 0 3
541 3 flock $handle, LOCK_UN;
542 } elsif ($self->{provider} eq "handle") { # Flush to handle
543 3 print $handle $_msg;
544 } else {
545 0 return 0;
546 }
547 6 return 1;
548 }
549  
550 # Syslog
551 0 ▶ return 0 if $self->provider ne "syslog";
Branch: 1 branch
BranchTrueFalseTotal
if $self->provider ne "syslog" 0 0 0
552 0 ▶ my $lvl = $LOGLEVELS{$level} // Sys::Syslog::LOG_DEBUG;
Condition0%0/2
$LOGLEVELS{$level} // 7
l!l
00
MC/DC0%0/1
$LOGLEVELS{$level} // 7
$LOGLEVELS{$level}
Truth table0%0/2
A
A= $LOGLEVELS{$level}
Aresult
11
00
553 0 Sys::Syslog::syslog($lvl, LOGFORMAT, join(SEPARATOR, @msg));
554 }
555  
556 6 ▶sub _default {
Subroutine _default: called CC=2 SCAR=7.0
557 6 my ($tm, $l, @msg) = @_;
558 6 my ($s, $m, $h, $day, $month, $year) = localtime $tm;
559 6 ▶ my $time = sprintf '%04d-%02d-%02d %02d:%02d:%08.5f', $year + 1900, $month + 1, $day, $h, $m,
Condition50%1/2
(split(/\./, $tm, 0))[1] // 0
l!l
60
MC/DC0%0/1
(split(/\./, $tm, 0))[1] // 0
(split(/\./, $tm, 0))[1]
Truth table50%1/2
A
A= (split(/\./, $tm, 0))[1]
Aresult
11
00
560 "$s." . ((split /\./, $tm)[1] // 0);
561 6 return "[$time] [$$] [$l] " . join(SEPARATOR, @msg) . "\n";
562 }
563 0 ▶sub _short {
Subroutine _short: not called CC=1 SCAR=6.9
564 0 my ($tm, $l, @msg) = @_;
565 0 my $short = substr($l, 0, 1);
566 0 return "[$$] [$short] " . join(SEPARATOR, @msg) . "\n";
567 }
568 0 ▶sub _color {
Subroutine _color: not called CC=2 SCAR=17.9
569 0 my $msg = _default(shift, my $level = shift, @_);
570 0 ▶ return $msg unless $COLORS{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless $COLORS{$level} 0 0 0
571 0 chomp $msg;
572 0 return color($COLORS{$level}, $msg) . "\n";
573 }
574  
575 5 ▶DESTROY {
Subroutine DESTROY: called CC=4 SCAR=22.5
576 5 my $self = shift;
577 5 ▶ if ($self->{autoclean}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autoclean"}) 0 5 5
578 0 ▶ undef $self->{handle} if $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"} 0 0 0
579 0 ▶ Sys::Syslog::closelog() if $self->{provider} eq "syslog";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"provider"} eq "syslog" 0 0 0
580 }
581 }
582  
583 1 1;
584  
585 __END__
« Acrux/FileLock.pm ↑ Summary Acrux/Config.pm »