| 1 | package Class::Usul::Log; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Class::Usul::Constants qw( FALSE LOG_LEVELS NUL SPC TRUE ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Class::Usul::Functions qw( is_hashref is_member merge_attributes | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | untaint_identifier ); | |||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Class::Usul::Types qw( Bool DataEncoding HashRef | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | LoadableClass Logger SimpleStr Undef ); | |||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Encode qw( encode ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use File::Basename qw( dirname ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use File::DataClass::Types qw( Path ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Sub::Install qw( install_sub ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Attribute constructors | ||||||||||||||||||||||||||||
| 18 | 6 | ▶ | my $_build__log = sub { | ||||||||||||||||||||||||||
|
Subroutine
_build__log: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 6 | return $_[ 0 ]->_log_class->new( %{ $_[ 0 ]->_log_attributes } ); | |||||||||||||||||||||||||||
| 20 | 4 | }; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | my $_build__log_class = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Subroutine
_build__log_class: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | return $_[ 0 ]->_logfile ? 'Log::Handler' : 'Class::Null'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 24 | 4 | }; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Private attributes | ||||||||||||||||||||||||||||
| 27 | 4 | has '_debug_flag' => is => 'ro', isa => Bool, default => FALSE, | |||||||||||||||||||||||||||
| 28 | init_arg => 'debug'; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 4 | has '_encoding' => is => 'ro', isa => DataEncoding | Undef, | |||||||||||||||||||||||||||
| 31 | init_arg => 'encoding'; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | has '_log' => is => 'lazy', isa => Logger, | |||||||||||||||||||||||||||
| 34 | builder => $_build__log, init_arg => 'log'; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | has '_log_attributes' => is => 'lazy', isa => HashRef, | ||||||||||||||||||||||||||||
| 37 | 2 | ▶ | builder => sub { {} }, init_arg => 'log_attributes'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 4 | has '_log_class' => is => 'lazy', isa => LoadableClass, coerce => TRUE, | |||||||||||||||||||||||||||
| 40 | builder => $_build__log_class, init_arg => 'log_class'; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 4 | has '_logfile' => is => 'ro', isa => Path | Undef, coerce => TRUE, | |||||||||||||||||||||||||||
| 43 | init_arg => 'logfile'; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Private class attributes | ||||||||||||||||||||||||||||
| 46 | 4 | my $loggers = {}; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Construction | ||||||||||||||||||||||||||||
| 49 | 8 | ▶ | around 'BUILDARGS' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 50 | 8 | my ($orig, $class, @args) = @_; my $attr = $orig->( $class, @args ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 8 | ▶ | my $builder = $attr->{builder} or return $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 6 | ▶ | my $config = $builder->can( 'config' ) ? $builder->config : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 6 | my $keys = [ qw( appclass encoding log_attributes log_class logfile ) ]; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 6 | merge_attributes $attr, $builder, [ 'debug' ]; | |||||||||||||||||||||||||||
| 57 | 6 | merge_attributes $attr, $config, $keys; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 6 | return $attr; | |||||||||||||||||||||||||||
| 60 | 4 | }; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 8 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 8 | my ($self, $attr) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 8 | ▶ | exists $attr->{appclass} and $loggers->{ $attr->{appclass} } = $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 8 | ▶ | exists $loggers->{default} or $loggers->{default} = $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 8 | return; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=21.0
| |||||||||||||||||||||||||||||
| 72 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | my $params = { (is_hashref $_[ 0 ]) ? %{+ shift } : () }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2 | my @wanted = @_; | |||||||||||||||||||||||||||
| 75 | 2 | my $target = caller; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | for my $wanted (grep { defined $_ } @wanted) { | |||||||||||||||||||||||||||
| 78 | 1 | ▶ | if ($wanted eq 'get_logger') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $subr = $params->{as} // 'get_logger'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$params->{"as"} // "get_logger"
MC/DC0%0/1
$params->{"as"} // "get_logger"
$params->{"as"}
Truth table0%0/2
A
A= $params->{"as"}
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | install_sub { into => $target, as => $subr, code => sub ($) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 82 | 0 | return $loggers->{ $_[ 0 ] }; | |||||||||||||||||||||||||||
| 83 | 0 | } }; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | 1 | ▶ | my $subr = $params->{as} // 'log'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"as"} // "log"
MC/DC0%0/1
$params->{"as"} // "log"
$params->{"as"}
Truth table50%1/2
A
A= $params->{"as"}
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 7 | ▶ | install_sub { into => $target, as => $subr, code => sub (;@) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 7 | return $loggers->{ $wanted }->log( @_ ); | |||||||||||||||||||||||||||
| 90 | 1 | } }; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | return; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 7 | ▶ | around '_log_attributes' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=24.4
| |||||||||||||||||||||||||||||
| 98 | 7 | my ($orig, $self) = @_; my $attr = $orig->( $self ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 7 | ▶ | $self->_log_class ne 'Log::Handler' and return $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 7 | ▶ | my $fattr = $attr->{file} //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$attr->{"file"} //= {}
MC/DC0%0/1
$attr->{"file"} //= {}
$attr->{"file"}
Truth table50%1/2
A
A= $attr->{"file"}
| |||||||||||||||||||||||||||||
| 103 | 7 | ▶ | my $logfile = $self->_logfile // $fattr->{filename}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->_logfile // $fattr->{"filename"}
MC/DC0%0/2
$self->_logfile // $fattr->{"filename"}
$self->_logfile
$fattr->{"filename"}
Truth table33%1/3
A // B
A= $self->_logfileB= $fattr->{"filename"}
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 7 | ▶ | ($logfile and -d dirname( "${logfile}" )) or return $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$logfile and -d dirname("$logfile")
MC/DC0%0/2
$logfile and -d dirname("$logfile")
$logfile
-d dirname("$logfile")
Truth table33%1/3
A and B
A= $logfileB= -d dirname("$logfile")
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 7 | $fattr->{alias} = 'file-out'; | |||||||||||||||||||||||||||
| 108 | 7 | $fattr->{filename} = "${logfile}"; | |||||||||||||||||||||||||||
| 109 | $fattr->{maxlevel} = $self->_debug_flag ? 'debug' | ||||||||||||||||||||||||||||
| 110 | 7 | ▶ | : untaint_identifier $fattr->{maxlevel} // 'info'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
&untaint_identifier($fattr->{"maxlevel"}) // "info"
MC/DC0%0/1
&untaint_identifier($fattr->{"maxlevel"}) // "info"
&untaint_identifier($fattr->{"maxlevel"})
Truth table50%1/2
A
A= &untaint_identifier($fattr->{"maxlevel"})
| |||||||||||||||||||||||||||||
| 111 | 7 | ▶ | $fattr->{mode } = untaint_identifier $fattr->{mode } // 'append'; | ||||||||||||||||||||||||||
|
Condition50%1/2
&untaint_identifier($fattr->{"mode"}) // "append"
MC/DC0%0/1
&untaint_identifier($fattr->{"mode"}) // "append"
&untaint_identifier($fattr->{"mode"})
Truth table50%1/2
A
A= &untaint_identifier($fattr->{"mode"})
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 7 | return $attr; | |||||||||||||||||||||||||||
| 114 | 4 | }; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Private functions | ||||||||||||||||||||||||||||
| 117 | 4 | ▶ | my $add_methods = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||
| 118 | 4 | my ($class, @methods) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 4 | for my $method (@methods) { | |||||||||||||||||||||||||||
| 121 | $class->can( $method ) or | ||||||||||||||||||||||||||||
| 122 | 20 | ▶ | install_sub { into => $class, as => $method, code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=24.8
| |||||||||||||||||||||||||||||
| 123 | 20 | ▶ | my ($self, $text, $opts) = @_; $text or return FALSE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 20 | $text = ucfirst "${text}"; chomp $text; $text .= "\n"; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 20 | ▶ | if (defined $opts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 1 | ▶ | my $lead = ucfirst $opts->{leader} // NUL; | ||||||||||||||||||||||||||
|
Condition50%1/2
ucfirst $opts->{"leader"} // ""
MC/DC0%0/1
ucfirst $opts->{"leader"} // ""
ucfirst $opts->{"leader"}
Truth table50%1/2
A
A= ucfirst $opts->{"leader"}
| |||||||||||||||||||||||||||||
| 129 | my $tag = $opts->{tag} | ||||||||||||||||||||||||||||
| 130 | 1 | ▶ | // ($opts->{user} ? $opts->{user}->username : NUL); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts->{"tag"} // ($opts->{"user"} ? $opts->{"user"}->username : "")
MC/DC0%0/2
$opts->{"tag"} // ($opts->{"user"} ? $opts->{"user"}->username : "")
$opts->{"tag"}
($opts->{"user"} ? $opts->{"user"}->username : "")
Truth table33%1/3
A // B
A= $opts->{"tag"}B= ($opts->{"user"} ? $opts->{"user"}->username : "")
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 1 | ▶ | $tag = $tag ? "[${tag}] " : $lead ? SPC : NUL; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 133 | 1 | $text = "${lead}${tag}${text}"; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 20 | ▶ | $self->_encoding and $text = encode( $self->_encoding, $text ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 20 | $self->_log->$method( $text ); | |||||||||||||||||||||||||||
| 138 | 20 | return TRUE; | |||||||||||||||||||||||||||
| 139 | 24 | ▶ | } }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 4 | return; | |||||||||||||||||||||||||||
| 143 | 4 | }; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 4 | $add_methods->( __PACKAGE__, LOG_LEVELS ); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | my @arg_names = qw( level message options ); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 4 | ▶ | my $inline_args = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | 4 | my $n = shift; return (map { $arg_names[ $_ ] => $_[ $_ ] } 0 .. $n - 1); | |||||||||||||||||||||||||||
| 151 | 4 | }; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Public methods | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub filehandle { | ||||||||||||||||||||||||||
|
Subroutine
filehandle: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my $self = shift; $self->_log_class ne 'Log::Handler' and return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | return $self->_log->output( 'file-out' )->{fh}; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 7 | ▶ | sub log { | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | 7 | ▶ | my ($self, @args) = @_; my $n = 0; $n++ while (defined $args[ $n ]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 7 | ▶ | my $args = ($n == 0 ) ? {} | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 164 | : (is_hashref $args[ 0 ]) ? $args[ 0 ] | ||||||||||||||||||||||||||||
| 165 | : ($n == 1 ) ? { $inline_args->( 2, 'info', @args ) } | ||||||||||||||||||||||||||||
| 166 | : ($n == 2 ) ? { $inline_args->( 2, @args ) } | ||||||||||||||||||||||||||||
| 167 | : ($n == 3 ) ? { $inline_args->( 3, @args ) } | ||||||||||||||||||||||||||||
| 168 | : { @args }; | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 7 | my $level = $args->{level}; $level and is_member $level, LOG_LEVELS | |||||||||||||||||||||||||||
| 171 | 7 | ▶ | and return $self->$level( $args->{message}, $args->{options} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$level and &is_member($level, &LOG_LEVELS())
MC/DC100%2/2
$level and &is_member($level, &LOG_LEVELS())
$level
&is_member($level, &LOG_LEVELS())
Truth table100%3/3
A and B
A= $levelB= &is_member($level, &LOG_LEVELS())
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 2 | return FALSE; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 4 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||