Crane/Logger.pm

filter: Statementstmt 93.4% 71 / 76 Branchbran 57.8% 22 / 38 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 75.1% 112 / 149 CC 15 SCAR 12.2
CC
15
Cov
77%
write_to_fh
7.9
END
3.3
log_fatal
2.0
CRAP 3.4
« Crane/Config.pm ↑ Summary Crane/Base.pm »
1 # -*- coding: utf-8 -*-
2  
3  
4 package Crane::Logger;
5  
6  
7 2 ▶use Crane::Base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Crane::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Crane::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Fcntl qw( :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 2 our @EXPORT = qw(
17 &log_fatal
18 &log_error
19 &log_warning
20 &log_info
21 &log_debug
22 &log_verbose
23 );
24  
25 2 our @EXPORT_LEVELS = qw(
26 $LOG_FATAL
27 $LOG_ERROR
28 $LOG_WARNING
29 $LOG_INFO
30 $LOG_DEBUG
31 $LOG_VERBOSE
32 );
33  
34 2 our @EXPORT_OK = ( @EXPORT_LEVELS );
35  
36 2 our %EXPORT_TAGS = (
37 'levels' => \@EXPORT_LEVELS,
38 );
39  
40  
41 2 Readonly::Scalar(our $LOG_FATAL => 1);
42 2 Readonly::Scalar(our $LOG_ERROR => 2);
43 2 Readonly::Scalar(our $LOG_WARNING => 3);
44 2 Readonly::Scalar(our $LOG_INFO => 4);
45 2 Readonly::Scalar(our $LOG_DEBUG => 5);
46 2 Readonly::Scalar(our $LOG_VERBOSE => 6);
47  
48 2 Readonly::Hash(our %LOG_LEVELS => (
49 $LOG_FATAL => $LOG_FATAL, 'fatal' => $LOG_FATAL,
50 $LOG_ERROR => $LOG_ERROR, 'error' => $LOG_ERROR, 'err' => $LOG_ERROR,
51 $LOG_WARNING => $LOG_WARNING, 'warning' => $LOG_WARNING, 'warn' => $LOG_WARNING,
52 $LOG_INFO => $LOG_INFO, 'info' => $LOG_INFO,
53 $LOG_DEBUG => $LOG_DEBUG, 'debug' => $LOG_DEBUG,
54 $LOG_VERBOSE => $LOG_VERBOSE, 'verbose' => $LOG_VERBOSE
55 ));
56  
57  
58 # Log level
59 2 ▶our $LOG_LEVEL = $LOG_LEVELS{ config->{'log'}->{'level'} // $LOG_INFO };
Condition33%1/3
config()->{"log"}{"level"} // $My::Logger::LOG_INFO
l!l&&r!l&&!r
020
MC/DC0%0/2
config()->{"log"}{"level"} // $My::Logger::LOG_INFO
config()->{"log"}{"level"} $My::Logger::LOG_INFO
Truth table33%1/3
A // B
A= config()->{"log"}{"level"}B= $My::Logger::LOG_INFO
ABresult
1X1
011
000
60  
61 2 ▶if ( options->{'debug'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (options()->{'debug'}) { } 0 2 2
elsif (options()->{'verbose'}) { } 0 2 2
62 0 $LOG_LEVEL = $LOG_DEBUG;
63 } elsif ( options->{'verbose'} ) {
64 0 $LOG_LEVEL = $LOG_VERBOSE;
65 }
66  
67  
68 # Log file handle
69 2 our $MESSAGES_FH = *STDOUT;
70  
71 2 ▶if ( my $log_filename = options->{'log'} // config->{'log'}->{'filename'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log_filename = options()->{"log"} // config()->{"log"}{"filename"}) 2 0 2
Condition33%1/3
options()->{"log"} // config()->{"log"}{"filename"}
l!l&&r!l&&!r
020
MC/DC0%0/2
options()->{"log"} // config()->{"log"}{"filename"}
options()->{"log"} config()->{"log"}{"filename"}
Truth table33%1/3
A // B
A= options()->{"log"}B= config()->{"log"}{"filename"}
ABresult
1X1
011
000
72 2 ▶ if ( -e $log_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $log_filename) 0 2 2
73 0 ▶ open $MESSAGES_FH, '>>:encoding(UTF-8)', $log_filename or confess("Unable to update log '$log_filename': $OS_ERROR");
Branch: 1 branch
BranchTrueFalseTotal
open $My::Logger::MESSAGES_FH, ">>:encoding(UTF-8)", $log_filename or confess("Unable to update log '${log_filename}': $My::Logger::OS_ERROR") 0 0 0
74 }
75 }
76  
77  
78 # Error log file handle
79 2 our $ERRORS_FH = *STDERR;
80  
81 2 ▶if ( my $log_error_filename = options->{'log-error'} // config->{'log'}->{'error_filename'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log_error_filename = options()->{"log-error"} // config()->{"log"}{"error_filename"}) 2 0 2
Condition33%1/3
options()->{"log-error"} // config()->{"log"}{"error_filename"}
l!l&&r!l&&!r
020
MC/DC0%0/2
options()->{"log-error"} // config()->{"log"}{"error_filename"}
options()->{"log-error"} config()->{"log"}{"error_filename"}
Truth table33%1/3
A // B
A= options()->{"log-error"}B= config()->{"log"}{"error_filename"}
ABresult
1X1
011
000
82 2 ▶ if ( -e $log_error_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $log_error_filename) 0 2 2
83 0 ▶ open $ERRORS_FH, '>>:encoding(UTF-8)', $log_error_filename or confess("Unable to update log '$log_error_filename': $OS_ERROR");
Branch: 1 branch
BranchTrueFalseTotal
open $My::Logger::ERRORS_FH, ">>:encoding(UTF-8)", $log_error_filename or confess("Unable to update log '${log_error_filename}': $My::Logger::OS_ERROR") 0 0 0
84 }
85 }
86  
87  
88 # Close file handles on exit
89 2 ▶END {
Subroutine END: called CC=3 SCAR=12.0
90
91 2 ▶ close $MESSAGES_FH or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
close $My::Logger::MESSAGES_FH or confess($My::Logger::OS_ERROR) 2 0 2
92 2 ▶ close $ERRORS_FH or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
close $My::Logger::ERRORS_FH or confess($My::Logger::OS_ERROR) 2 0 2
93
94 }
95  
96  
97 12 ▶sub log_fatal {
Subroutine log_fatal: called CC=2 SCAR=7.1
98
99 12 ▶ if ( $LOG_LEVEL >= $LOG_FATAL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($My::Logger::LOG_LEVEL >= $My::Logger::LOG_FATAL) 12 0 12
100 12 write_to_fh($ERRORS_FH, @_);
101 }
102
103 12 return;
104
105 }
106  
107  
108 12 ▶sub log_error {
Subroutine log_error: called CC=2 SCAR=6.9
109
110 12 ▶ if ( $LOG_LEVEL >= $LOG_ERROR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($My::Logger::LOG_LEVEL >= $My::Logger::LOG_ERROR) 10 2 12
111 10 write_to_fh($ERRORS_FH, @_);
112 }
113
114 12 return;
115
116 }
117  
118  
119 12 ▶sub log_warning {
Subroutine log_warning: called CC=2 SCAR=6.9
120
121 12 ▶ if ( $LOG_LEVEL >= $LOG_WARNING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($My::Logger::LOG_LEVEL >= $My::Logger::LOG_WARNING) 8 4 12
122 8 write_to_fh($ERRORS_FH, @_);
123 }
124
125 12 return;
126
127 }
128  
129  
130 12 ▶sub log_info {
Subroutine log_info: called CC=2 SCAR=6.9
131
132 12 ▶ if ( $LOG_LEVEL >= $LOG_INFO ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($My::Logger::LOG_LEVEL >= $My::Logger::LOG_INFO) 6 6 12
133 6 write_to_fh($MESSAGES_FH, @_);
134 }
135
136 12 return;
137
138 }
139  
140  
141 12 ▶sub log_debug {
Subroutine log_debug: called CC=2 SCAR=6.9
142
143 12 ▶ if ( $LOG_LEVEL >= $LOG_DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($My::Logger::LOG_LEVEL >= $My::Logger::LOG_DEBUG) 4 8 12
144 4 write_to_fh($MESSAGES_FH, @_);
145 }
146
147 12 return;
148
149 }
150  
151  
152 12 ▶sub log_verbose {
Subroutine log_verbose: called CC=2 SCAR=6.9
153
154 12 ▶ if ( $LOG_LEVEL >= $LOG_VERBOSE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($My::Logger::LOG_LEVEL >= $My::Logger::LOG_VERBOSE) 2 10 12
155 2 write_to_fh($MESSAGES_FH, @_);
156 }
157
158 12 return;
159
160 }
161  
162  
163 42 ▶sub write_to_fh {
Subroutine write_to_fh: called CC=7 SCAR=20.6
Pod documented
164
165 42 my ( $fh, @messages ) = @_;
166
167 42 ▶ if ( not defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 42 42
168 0 confess('Invalid file handle');
169 }
170
171 42 local $Data::Dumper::Indent = 1;
172 42 local $Data::Dumper::Purity = 0;
173 42 local $Data::Dumper::Terse = 1;
174
175 42 flock $fh, LOCK_EX;
176
177 42 my $datetime = strftime(q{%Y-%m-%d %H:%M:%S %z %s}, localtime);
178
179 42 foreach my $message ( @messages ) {
180 42 ▶ foreach my $line ( split m{$INPUT_RECORD_SEPARATOR}osi, ( not defined $message or ref $message ) ? Dumper($message) : $message ) {
Branch: 1 branch
BranchTrueFalseTotal
!defined($message) || ref $message ? : 0 42 42
Condition33%1/3
not defined $message or ref $message
l!l&&r!l&&!r
0042
MC/DC0%0/2
not defined $message or ref $message
not defined $message ref $message
Truth table33%1/3
not(A) or B
A= not defined $messageB= ref $message
ABresult
1X1
011
000
181 42 ▶ print { $fh } "[$datetime] $line\n" or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} "[$datetime] $line\n" or confess($My::Logger::OS_ERROR) 42 0 42
182 }
183 }
184
185 42 flock $fh, LOCK_UN;
186
187 42 return;
188
189 }
190  
191  
192 2 1;
193  
194  
195 =head1 NAME
196  
197 Crane::Logger - Log manager
198  
199  
200 =head1 SYNOPSIS
201  
202 use Crane::Logger;
203
204 log_fatal('Fatal message', caller);
205 log_error('Error message');
206 log_warning('Warning message', $ref);
207 log_info("First line\nSecond line\n");
208 log_debug($ref);
209 log_verbose('First line', 'Second line');
210  
211  
212 =head1 DESCRIPTION
213  
214 Simple log manager with six log levels. Supports auto split messages by "end of
215 line" and dump references using L<Data::Dumper|Data::Dumper>.
216  
217  
218 =head2 Log entry
219  
220 Each log entry looks like ...
221  
222 [2013-12-30 02:36:22 +0400 1388356582] Hello, world!
223  
224 ... and contains:
225  
226 =over
227  
228 =item Date
229  
230 Date in ISO format: YYYY-MM-DD.
231  
232 2013-12-30
233  
234 =item Time
235  
236 Time in ISO format: hh:mm:ss.
237  
238 02:36:22
239  
240 =item Time zone
241  
242 Time zone in ISO format: ±hhmm.
243  
244 +0400
245  
246 =item Unix time
247  
248 Unix time.
249  
250 1388356582
251  
252 =item Message
253  
254 Log message.
255  
256 Hello, world!
257  
258 =back
259  
260 In case of log reference, each line will contain "header" (date and times):
261  
262 [2013-12-30 02:36:22 +0400 1388356582] {
263 [2013-12-30 02:36:22 +0400 1388356582] 'room' => 'Sitting room',
264 [2013-12-30 02:36:22 +0400 1388356582] 'colors' => [
265 [2013-12-30 02:36:22 +0400 1388356582] 'orange',
266 [2013-12-30 02:36:22 +0400 1388356582] 'purple',
267 [2013-12-30 02:36:22 +0400 1388356582] 'black'
268 [2013-12-30 02:36:22 +0400 1388356582] ]
269 [2013-12-30 02:36:22 +0400 1388356582] }
270  
271  
272 =head2 Log levels
273  
274 =over
275  
276 =item B<FATAL>
277  
278 Logs messages at a B<FATAL> level only.
279  
280 =item B<ERROR>
281  
282 Logs messages classified as B<ERROR> and B<FATAL>.
283  
284 =item B<WARNING>
285  
286 Logs messages classified as B<WARNING>, B<ERROR> and B<FATAL>.
287  
288 =item B<INFO>
289  
290 Logs messages classified as B<INFO>, B<WARNING>, B<ERROR> and B<FATAL>.
291  
292 =item B<DEBUG>
293  
294 Logs messages classified as B<DEBUG>, B<INFO>, B<WARNING>, B<ERROR> and
295 B<FATAL>.
296  
297 =item B<VERBOSE>
298  
299 Logs messages classified as B<VERBOSE>, B<DEBUG>, B<INFO>, B<WARNING>, B<ERROR>
300 and B<FATAL>.
301  
302 =back
303  
304 Messages on levels: B<FATAL>, B<ERROR> and B<WARNING> go to error log; B<INFO>,
305 B<DEBUG> and B<VERBOSE> go to messages log.
306  
307  
308 =head1 EXPORTED FUNCTIONS
309  
310 =over
311  
312 =item B<log_fatal> (I<@messages>)
313  
314 Logs I<@messages> with level L<FATAL|/"FATAL">.
315  
316 =item B<log_error> (I<@messages>)
317  
318 Logs I<@messages> with level L<ERROR|/"ERROR">.
319  
320 =item B<log_warning> (I<@messages>)
321  
322 Logs I<@messages> with level L<WARNING|/"WARNING">.
323  
324 =item B<log_info> (I<@messages>)
325  
326 Logs I<@messages> with level L<INFO|/"INFO">.
327  
328 =item B<log_debug> (I<@messages>)
329  
330 Logs I<@messages> with level L<DEBUG|/"DEBUG">.
331  
332 =item B<log_verbose> (I<@messages>)
333  
334 Logs I<@messages> with level L<VERBOSE|/"VERBOSE">.
335  
336 =back
337  
338  
339 =head1 FUNCTIONS
340  
341 =over
342  
343 =item B<write_to_fh> (I<$fh>, I<@messages>)
344  
345 Write I<@messages> to file handle I<$fh>.
346  
347 =back
348  
349  
350 =head1 ERRORS
351  
352 =over
353  
354 =item Unable to update log 'I<%s>': I<%s>
355  
356 Where I<%s> is log filename and I<%s> is reason message.
357  
358 Fires when unable to open or write to log file.
359  
360 =item Invalid file handle
361  
362 Fires when call L<write_to_fh|/"write_to_fh ($fh, @messages)"> with invalid file
363 handle.
364  
365 =back
366  
367  
368 =head1 FILES
369  
370 =over
371  
372 =item F<log/messages.log>
373  
374 Default log file with messages.
375  
376 =item F<log/errors.log>
377  
378 Default log file with errors.
379  
380 =back
381  
382  
383 =head1 BUGS
384  
385 Please report any bugs or feature requests to
386 L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Crane> or to
387 L<https://github.com/temoon/crane/issues>.
388  
389  
390 =head1 AUTHOR
391  
392 Tema Novikov, <novikov.tema@gmail.com>
393  
394  
395 =head1 COPYRIGHT AND LICENSE
396  
397 Copyright (C) 2013-2014 Tema Novikov.
398  
399 This library is free software; you can redistribute it and/or modify it under
400 the terms of the Artistic License 2.0. For details, see the full text of the
401 license in the file LICENSE.
402  
403  
404 =head1 SEE ALSO
405  
406 =over
407  
408 =item * B<RT Cpan>
409  
410 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Crane>
411  
412 =item * B<Github>
413  
414 L<https://github.com/temoon/crane>
415  
416 =back
« Crane/Config.pm ↑ Summary Crane/Base.pm »