File Coverage

lib/Amazon/S3/Logger.pm
Criterion Covered Total %
statement 44 45 97.7
branch 7 10 70.0
condition 2 6 33.3
subroutine 12 12 100.0
pod 0 2 0.0
total 65 75 86.6


line stmt bran cond sub pod time code
1             package Amazon::S3::Logger;
2              
3 2     2   13 use strict;
  2         5  
  2         58  
4 2     2   8 use warnings;
  2         4  
  2         110  
5              
6 2     2   12 use Amazon::S3::Constants qw{ :chars };
  2         4  
  2         304  
7              
8 2     2   19 use English qw{-no_match_vars};
  2         4  
  2         15  
9 2     2   763 use POSIX;
  2         5  
  2         19  
10 2     2   7791 use Readonly;
  2         3  
  2         128  
11 2     2   13 use Scalar::Util qw{ reftype };
  2         6  
  2         219  
12              
13             our $VERSION = '0.63'; ## no critic (RequireInterpolationOfMetachars)
14              
15             Readonly::Hash our %LOG_LEVELS => (
16             trace => 5,
17             debug => 4,
18             info => 3,
19             warn => 2,
20             error => 1,
21             fatal => 0,
22             );
23              
24             {
25 2     2   12 no strict 'refs'; ## no critic (ProhibitNoStrict)
  2         3  
  2         884  
26              
27             foreach my $level (qw{fatal error warn info debug trace}) {
28              
29             *{ __PACKAGE__ . $DOUBLE_COLON . $level } = sub {
30 83     83   549 my ( $self, @message ) = @_;
31 83         145 $self->_log_message( $level, @message );
32             };
33             }
34             }
35              
36             ########################################################################
37             sub new {
38             ########################################################################
39 3     3 0 9 my ( $class, @args ) = @_;
40              
41 3 50       10 my $options = ref $args[0] ? $args[0] : {@args};
42              
43 3         9 return bless $options, $class;
44             }
45              
46             ########################################################################
47             sub level {
48             ########################################################################
49 23     23 0 130 my ( $self, @args ) = @_;
50              
51 23 100       41 if (@args) {
52 6         9 $self->{log_level} = $args[0];
53             }
54              
55 23         448 return $self->{log_level};
56             }
57              
58             ########################################################################
59             sub _log_message {
60             ########################################################################
61 83     83   116 my ( $self, $level, @message ) = @_;
62              
63 83 100       295 return if $LOG_LEVELS{ lc $level } > $LOG_LEVELS{ lc $self->{log_level} };
64 36 50       429 return if !@message;
65              
66 36         44 my $log_message;
67              
68 36 50 33     149 if ( defined $message[0]
      33        
69             && ref $message[0]
70             && reftype( $message[0] ) eq 'CODE' ) {
71 0         0 $log_message = $message[0]->();
72             }
73             else {
74 36         90 $log_message = join $EMPTY, @message;
75             }
76              
77 36         178 chomp $log_message;
78              
79 36         772 my @tm = localtime time;
80              
81 36         987 my $timestamp = POSIX::strftime '%Y/%m/%d %H:%M:%S', @tm;
82              
83 36         82 return print {*STDERR} sprintf qq{%s: %s %s %s\n}, uc $level, $timestamp,
  36         849  
84             $PROCESS_ID, $log_message;
85             }
86              
87             1;