File Coverage

blib/lib/Protocol/ACME/Logger.pm
Criterion Covered Total %
statement 25 27 92.5
branch 4 6 66.6
condition n/a
subroutine 8 9 88.8
pod 0 1 0.0
total 37 43 86.0


line stmt bran cond sub pod time code
1             package Protocol::ACME::Logger;
2              
3 5     5   3058 use strict;
  5         8  
  5         166  
4 5     5   25 use warnings;
  5         12  
  5         135  
5              
6 5     5   26 use Log::Any::Adapter;
  5         6  
  5         23  
7 5     5   86 use base qw/Log::Any::Adapter::Base/;
  5         9  
  5         2251  
8 5     5   4521 use Time::HiRes qw( gettimeofday );
  5         5057  
  5         18  
9              
10             our $VERSION = '0.16';
11              
12             my %LOG_LEVELS = (
13             emergency => 0,
14             alert => 1,
15             critical => 2,
16             fatal => 2,
17             crit => 2,
18             err => 2,
19             error => 3,
20             warn => 4,
21             warning => 4,
22             notice => 5,
23             inform => 6,
24             info => 6,
25             debug => 7,
26             trace => 8,
27             );
28              
29             sub init {
30 6     6 0 641 my ($self) = @_;
31 6 50       36 if ( exists $self->{log_level} ) {
32             $self->{log_level} = $LOG_LEVELS{lc($self->{log_level})}
33 6 50       54 unless $self->{log_level} =~ /^\d+$/;
34             }
35             else {
36 0         0 $self->{log_level} = $LOG_LEVELS{trace};
37             }
38             }
39              
40             foreach my $method (keys %LOG_LEVELS) {
41 5     5   1363 no strict 'refs';
  5         8  
  5         653  
42             my $method_level = $LOG_LEVELS{$method};
43             *{$method} = sub {
44 40     40   884 my ( $self, $text ) = @_;
45 40 100       256 return if $method_level > $self->{log_level};
46 27         118 my ( $sec, $usec ) = gettimeofday();
47 27         5354 printf STDOUT "# %d.%06d %s\n", $sec, $usec, $text;
48             };
49             my $detection_method = 'is_' . $method;
50             *{$detection_method} = sub {
51 0     0     return !!( $method_level <= $_[0]->{log_level} );
52             };
53             }
54              
55             1;