File Coverage

blib/lib/Log/ger/Output/Perl.pm
Criterion Covered Total %
statement 33 33 100.0
branch 13 16 81.2
condition 2 5 40.0
subroutine 6 6 100.0
pod 0 1 0.0
total 54 61 88.5


line stmt bran cond sub pod time code
1             package Log::ger::Output::Perl;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-03-07'; # DATE
5             our $DIST = 'Log-ger-Output-Perl'; # DIST
6             our $VERSION = '0.002'; # VERSION
7              
8 1     1   3317 use 5.010001;
  1         3  
9 1     1   4 use strict;
  1         1  
  1         16  
10 1     1   4 use warnings;
  1         1  
  1         17  
11 1     1   4 use Log::ger::Util ();
  1         2  
  1         265  
12              
13             sub get_hooks {
14 1     1 0 13 my %plugin_conf = @_;
15              
16 1   50     4 my $action = delete($plugin_conf{action}) || {
17             warn => 'warn',
18             error => 'warn',
19             fatal => 'die',
20             };
21 1 50       3 keys %plugin_conf and die "Unknown configuration: ".
22             join(", ", sort keys %plugin_conf);
23              
24             return {
25             create_outputter => [
26             __PACKAGE__, # key
27             # we want to handle all levels, thus we need to be higher priority
28             # than default Log::ger hooks (10) which will install null loggers
29             # for less severe levels.
30             9, # priority
31             sub { # hook
32 6     6   651 my %hook_args = @_;
33              
34             my $outputter = sub {
35 6         5295 my ($per_target_conf, $msg, $per_msg_conf) = @_;
36 6   33     26 my $lvl = $per_msg_conf->{level} // $hook_args{level};
37 6 50       15 if (my $act =
38             $action->{Log::ger::Util::string_level($lvl)}) {
39 6 50       107 @_ = ref $msg eq 'ARRAY' ? @$msg : ($msg);
40 6 100       18 if ($act eq 'warn') {
    100          
    100          
    100          
    100          
41 1         12 warn @_;
42             } elsif ($act eq 'carp') {
43 1         5 require Carp;
44 1         17 goto &Carp::carp;
45             } elsif ($act eq 'cluck') {
46 1         5 require Carp;
47 1         12 goto &Carp::cluck;
48             } elsif ($act eq 'croak') {
49 1         5 require Carp;
50 1         12 goto &Carp::croak;
51             } elsif ($act eq 'confess') {
52 1         27 require Carp;
53 1         15 goto &Carp::confess;
54             } else {
55             # die is the default action if unknown
56 1         9 die @_;
57             }
58             }
59 6         30 };
60 6         14 [$outputter];
61 1         6 }],
62             };
63             }
64              
65             1;
66             # ABSTRACT: Log to Perl's standard facility (warn, die, etc)
67              
68             __END__