| 1 | package MooseX::Role::Loggable; | ||||||||||||||||||||||||||||
| 2 | 7 | $MooseX::Role::Loggable::VERSION = '0.114'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Extensive, yet simple, logging role using Log::Dispatchouli | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Safe::Isa; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use MooX::Types::MooseLike::Base qw<Bool Str>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Sub::Quote 'quote_sub'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use Log::Dispatchouli; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 7 | my %attr_meth_map = ( | |||||||||||||||||||||||||||
| 17 | 'logger_facility' => 'facility', | ||||||||||||||||||||||||||||
| 18 | 'logger_ident' => 'ident', | ||||||||||||||||||||||||||||
| 19 | 'log_to_file' => 'to_file', | ||||||||||||||||||||||||||||
| 20 | 'log_to_stdout' => 'to_stdout', | ||||||||||||||||||||||||||||
| 21 | 'log_to_stderr' => 'to_stderr', | ||||||||||||||||||||||||||||
| 22 | 'log_fail_fatal' => 'fail_fatal', | ||||||||||||||||||||||||||||
| 23 | 'log_muted' => 'muted', | ||||||||||||||||||||||||||||
| 24 | 'log_quiet_fatal' => 'quiet_fatal', | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | has 'debug' => ( | ||||||||||||||||||||||||||||
| 28 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 29 | 'isa' => Bool, | ||||||||||||||||||||||||||||
| 30 | 7 | ▶ | 'default' => sub {0}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 7 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | has 'logger_facility' => ( | ||||||||||||||||||||||||||||
| 34 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 35 | 'isa' => Str, | ||||||||||||||||||||||||||||
| 36 | 13 | ▶ | 'default' => sub {'local6'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 7 | ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | has 'logger_ident' => ( | ||||||||||||||||||||||||||||
| 40 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 41 | 'isa' => Str, | ||||||||||||||||||||||||||||
| 42 | 9 | ▶ | 'default' => sub { ref shift }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 7 | ); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | has 'log_to_file' => ( | ||||||||||||||||||||||||||||
| 46 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 47 | 'isa' => Bool, | ||||||||||||||||||||||||||||
| 48 | 13 | ▶ | 'default' => sub {0}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 7 | ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | has 'log_to_stdout' => ( | ||||||||||||||||||||||||||||
| 52 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 53 | 'isa' => Bool, | ||||||||||||||||||||||||||||
| 54 | 13 | ▶ | 'default' => sub {0}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 7 | ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | has 'log_to_stderr' => ( | ||||||||||||||||||||||||||||
| 58 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 59 | 'isa' => Bool, | ||||||||||||||||||||||||||||
| 60 | 13 | ▶ | 'default' => sub {0}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 7 | ); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 7 | has 'log_file' => ( | |||||||||||||||||||||||||||
| 64 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 65 | 'isa' => Str, | ||||||||||||||||||||||||||||
| 66 | 'predicate' => 'has_log_file', | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 7 | has 'log_path' => ( | |||||||||||||||||||||||||||
| 70 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 71 | 'isa' => Str, | ||||||||||||||||||||||||||||
| 72 | 'predicate' => 'has_log_path', | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | has 'log_pid' => ( | ||||||||||||||||||||||||||||
| 76 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 77 | 'isa' => Bool, | ||||||||||||||||||||||||||||
| 78 | 10 | ▶ | 'default' => sub {1}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | 7 | ); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | has 'log_fail_fatal' => ( | ||||||||||||||||||||||||||||
| 82 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 83 | 'isa' => Bool, | ||||||||||||||||||||||||||||
| 84 | 10 | ▶ | 'default' => sub {1}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 7 | ); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | has 'log_muted' => ( | ||||||||||||||||||||||||||||
| 88 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 89 | 'isa' => Bool, | ||||||||||||||||||||||||||||
| 90 | 10 | ▶ | 'default' => sub {0}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 7 | ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | has 'log_quiet_fatal' => ( | ||||||||||||||||||||||||||||
| 94 | 'is' => 'ro', | ||||||||||||||||||||||||||||
| 95 | 'isa' => quote_sub( | ||||||||||||||||||||||||||||
| 96 | q{ | ||||||||||||||||||||||||||||
| 97 | use Safe::Isa; | ||||||||||||||||||||||||||||
| 98 | $_[0] || $_[0]->$_isa( ref [] ) | ||||||||||||||||||||||||||||
| 99 | or die "$_[0] must be a string or arrayref" | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | ), | ||||||||||||||||||||||||||||
| 102 | 10 | ▶ | 'default' => sub {'stderr'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 7 | ); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 7 | has 'logger' => ( | |||||||||||||||||||||||||||
| 106 | 'is' => 'lazy', | ||||||||||||||||||||||||||||
| 107 | 'isa' => quote_sub( | ||||||||||||||||||||||||||||
| 108 | q{ | ||||||||||||||||||||||||||||
| 109 | use Safe::Isa; | ||||||||||||||||||||||||||||
| 110 | $_[0]->$_isa('Log::Dispatchouli') || | ||||||||||||||||||||||||||||
| 111 | $_[0]->$_isa('Log::Dispatchouli::Proxy') | ||||||||||||||||||||||||||||
| 112 | or die "$_[0] must be a Log::Dispatchouli object"; | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | ), | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 'handles' => [ | ||||||||||||||||||||||||||||
| 117 | qw/ | ||||||||||||||||||||||||||||
| 118 | log log_fatal log_debug | ||||||||||||||||||||||||||||
| 119 | set_debug clear_debug set_prefix clear_prefix set_muted clear_muted | ||||||||||||||||||||||||||||
| 120 | / | ||||||||||||||||||||||||||||
| 121 | ], | ||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 7 | ▶ | sub _build_logger { | ||||||||||||||||||||||||||
|
Subroutine
_build_logger: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 125 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | 7 | my %optional; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 7 | foreach my $option (qw<log_file log_path>) { | |||||||||||||||||||||||||||
| 129 | 14 | my $method = "has_$option"; | |||||||||||||||||||||||||||
| 130 | 14 | ▶ | if ( $self->$method ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | $optional{$option} = $self->$option; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 7 | my $logger = Log::Dispatchouli->new( | |||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||
| 137 | 'debug' => $self->debug, | ||||||||||||||||||||||||||||
| 138 | 'ident' => $self->logger_ident, | ||||||||||||||||||||||||||||
| 139 | 'facility' => $self->logger_facility, | ||||||||||||||||||||||||||||
| 140 | 'to_file' => $self->log_to_file, | ||||||||||||||||||||||||||||
| 141 | 'to_stdout' => $self->log_to_stdout, | ||||||||||||||||||||||||||||
| 142 | 'to_stderr' => $self->log_to_stderr, | ||||||||||||||||||||||||||||
| 143 | 'log_pid' => $self->log_pid, | ||||||||||||||||||||||||||||
| 144 | 'fail_fatal' => $self->log_fail_fatal, | ||||||||||||||||||||||||||||
| 145 | 'muted' => $self->log_muted, | ||||||||||||||||||||||||||||
| 146 | 'quiet_fatal' => $self->log_quiet_fatal, | ||||||||||||||||||||||||||||
| 147 | %optional, | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 7 | return $logger; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # if we already have a logger, use its values | ||||||||||||||||||||||||||||
| 155 | 13 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 13 | my $class = shift; | |||||||||||||||||||||||||||
| 157 | 13 | my %args = @_; | |||||||||||||||||||||||||||
| 158 | 13 | my @items = qw< | |||||||||||||||||||||||||||
| 159 | debug logger_facility logger_ident | ||||||||||||||||||||||||||||
| 160 | log_to_file log_to_stdout log_to_stderr log_file log_path | ||||||||||||||||||||||||||||
| 161 | log_pid log_fail_fatal log_muted log_quiet_fatal | ||||||||||||||||||||||||||||
| 162 | >; | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 13 | ▶ | if ( exists $args{'logger'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | $args{'logger'}->$_isa('Log::Dispatchouli') | ||||||||||||||||||||||||||||
| 166 | 5 | ▶ | || $args{'logger'}->$_isa('Log::Dispatchouli::Proxy') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"logger"}->$MooseX::Role::Loggable::_isa("Log::Dispatchouli") or $args{"logger"}->$MooseX::Role::Loggable::_isa("Log::Dispatchouli::Proxy")
MC/DC100%2/2
$args{"logger"}->$MooseX::Role::Loggable::_isa("Log::Dispatchouli") or $args{"logger"}->$MooseX::Role::Loggable::_isa("Log::Dispatchouli::Proxy")
$args{"logger"}->$MooseX::Role::Loggable::_isa("Log::Dispatchouli")
$args{"logger"}->$MooseX::Role::Loggable::_isa("Log::Dispatchouli::Proxy")
Truth table100%3/3
A or B
A= $args{"logger"}->$MooseX::Role::Loggable::_isa("Log::Dispatchouli")B= $args{"logger"}->$MooseX::Role::Loggable::_isa("Log::Dispatchouli::Proxy")
| |||||||||||||||||||||||||||||
| 167 | or Carp::croak('logger must be a Log::Dispatchouli object'); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 4 | foreach my $item (@items) { | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # if value is overridden, don't touch it | ||||||||||||||||||||||||||||
| 172 | my $attr | ||||||||||||||||||||||||||||
| 173 | = exists $attr_meth_map{$item} | ||||||||||||||||||||||||||||
| 174 | 48 | ▶ | ? $attr_meth_map{$item} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | : $item; | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 48 | ▶ | if ( exists $args{$item} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # override logger configuration | ||||||||||||||||||||||||||||
| 180 | 1 | $args{'logger'}{$attr} = $args{$item}; | |||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # override our attributes if it's in logger | ||||||||||||||||||||||||||||
| 184 | exists $args{'logger'}{$attr} | ||||||||||||||||||||||||||||
| 185 | 47 | ▶ | and $args{$item} = $args{'logger'}{$attr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 12 | return {%args}; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | ▶ | sub log_fields { | ||||||||||||||||||||||||||
|
Subroutine
log_fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 195 | 1 | my $warning | |||||||||||||||||||||||||||
| 196 | = '[MooseX::Role::Loggable] Calling ->log_fields() is deprecated, ' | ||||||||||||||||||||||||||||
| 197 | . 'it will be removed in the next version'; | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 1 | $self->log( { 'level' => 'warning' }, $warning ); | |||||||||||||||||||||||||||
| 200 | 1 | Carp::carp($warning); | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 1 | return ( 'logger' => $self->logger ); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 7 | 1; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | __END__ | ||||||||||||||||||||||||||||