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   15 use strict;
  2         5  
  2         61  
4 2     2   11 use warnings;
  2         4  
  2         66  
5              
6 2     2   10 use Amazon::S3::Constants qw{ :chars };
  2         4  
  2         277  
7              
8 2     2   1040 use English qw{-no_match_vars};
  2         5  
  2         14  
9 2     2   712 use POSIX;
  2         3  
  2         15  
10 2     2   7086 use Readonly;
  2         11  
  2         128  
11 2     2   32 use Scalar::Util qw{ reftype };
  2         3  
  2         202  
12              
13             our $VERSION = '0.64'; ## 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   14 no strict 'refs'; ## no critic (ProhibitNoStrict)
  2         2  
  2         871  
26              
27             foreach my $level (qw{fatal error warn info debug trace}) {
28              
29             *{ __PACKAGE__ . $DOUBLE_COLON . $level } = sub {
30 83     83   569 my ( $self, @message ) = @_;
31 83         141 $self->_log_message( $level, @message );
32             };
33             }
34             }
35              
36             ########################################################################
37             sub new {
38             ########################################################################
39 3     3 0 10 my ( $class, @args ) = @_;
40              
41 3 50       11 my $options = ref $args[0] ? $args[0] : {@args};
42              
43 3         10 return bless $options, $class;
44             }
45              
46             ########################################################################
47             sub level {
48             ########################################################################
49 23     23 0 142 my ( $self, @args ) = @_;
50              
51 23 100       40 if (@args) {
52 6         10 $self->{log_level} = $args[0];
53             }
54              
55 23         424 return $self->{log_level};
56             }
57              
58             ########################################################################
59             sub _log_message {
60             ########################################################################
61 83     83   122 my ( $self, $level, @message ) = @_;
62              
63 83 100       303 return if $LOG_LEVELS{ lc $level } > $LOG_LEVELS{ lc $self->{log_level} };
64 36 50       426 return if !@message;
65              
66 36         44 my $log_message;
67              
68 36 50 33     142 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         83 $log_message = join $EMPTY, @message;
75             }
76              
77 36         188 chomp $log_message;
78              
79 36         855 my @tm = localtime time;
80              
81 36         1036 my $timestamp = POSIX::strftime '%Y/%m/%d %H:%M:%S', @tm;
82              
83 36         113 return print {*STDERR} sprintf qq{%s: %s %s %s\n}, uc $level, $timestamp,
  36         807  
84             $PROCESS_ID, $log_message;
85             }
86              
87             1;