File Coverage

blib/lib/OIDC/Client/Role/LoggerWrapper.pm
Criterion Covered Total %
statement 31 31 100.0
branch 2 8 25.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 43 49 87.7


line stmt bran cond sub pod time code
1             package OIDC::Client::Role::LoggerWrapper;
2 5     5   236702 use utf8;
  5         386  
  5         45  
3 5     5   3189 use Moose::Role;
  5         717070  
  5         102  
4 5     5   39508 use Moose::Util::TypeConstraints;
  5         14  
  5         35  
5 5     5   15848 use namespace::autoclean;
  5         11745  
  5         60  
6 5     5   601 use feature 'signatures';
  5         15  
  5         916  
7 5     5   84 no warnings 'experimental::signatures';
  5         14  
  5         311  
8 5     5   39 use Carp qw(croak);
  5         11  
  5         486  
9 5     5   43 use List::Util qw(all);
  5         11  
  5         2875  
10              
11             =encoding utf8
12              
13             =head1 NAME
14              
15             OIDC::Client::Role::LoggerWrapper - Logger wrapper
16              
17             =head1 DESCRIPTION
18              
19             This Moose role is used to manage different loggers, mainly because some
20             only accept the C<warn> method, while others only the C<warning> method.
21              
22             =cut
23              
24             subtype 'LoggerObject',
25             as 'Object',
26             where {
27             my $obj = $_;
28             (all { $obj->can($_) } qw/debug info error/)
29             && ($obj->can('warning') || $obj->can('warn'));
30             },
31             message { "The 'logger' attribute you provided is not a logger object" };
32              
33             has 'log' => (
34             is => 'ro',
35             isa => 'LoggerObject',
36             required => 1,
37             );
38              
39             =head1 METHODS
40              
41             =head2 log_msg( $level, $message )
42              
43             $self->log_msg(info => "my log message");
44              
45             Log the message.
46              
47             $level only accepts C<debug>, C<info>, C<warning>, C<warn> or C<error> level.
48              
49             =cut
50              
51 160     160 1 312 sub log_msg ($self, $level, $message) {
  160         275  
  160         316  
  160         226  
  160         220  
52              
53 160 0       1014 my $method = $level =~ /^(debug|info|error)$/ ? $level
    0          
    50          
    50          
54             : $level =~ /^warn(ing)?$/ ? $self->log->can('warning') ? 'warning' : 'warn'
55             : undef
56             or croak("log_msg() only accepts debug, info, warning, warn or error level, not '$level'");
57              
58 160         5790 $self->log->$method($message);
59             }
60              
61             1;