| 1 | package Dancer::Logger::Abstract; | ||||||||||||||||||||||||||||||||
| 2 | 108 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||||||
| 3 | #ABSTRACT: Abstract logging engine for Dancer | ||||||||||||||||||||||||||||||||
| 4 | 108 | $Dancer::Logger::Abstract::VERSION = '1.3520'; | |||||||||||||||||||||||||||||||
| 5 | 108 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 108 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 108 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 108 | ▶ | use base 'Dancer::Engine'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 108 | ▶ | use Dancer::SharedData; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 108 | ▶ | use Dancer::Timer; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 108 | ▶ | use Dancer::Config 'setting'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 108 | ▶ | use POSIX qw/strftime/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | # This is the only method to implement by logger engines. | ||||||||||||||||||||||||||||||||
| 16 | # It receives the following arguments: | ||||||||||||||||||||||||||||||||
| 17 | # $msg_level, $msg_content, it gets called only if the configuration allows | ||||||||||||||||||||||||||||||||
| 18 | # a message of the given level to be logged. | ||||||||||||||||||||||||||||||||
| 19 | 16 | ▶ | sub _log { confess "_log not implemented" } | ||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 108 | my $levels = { | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | # levels < 0 are for core only | ||||||||||||||||||||||||||||||||
| 24 | core => -10, | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # levels > 0 are for end-users only | ||||||||||||||||||||||||||||||||
| 27 | debug => 1, | ||||||||||||||||||||||||||||||||
| 28 | info => 2, | ||||||||||||||||||||||||||||||||
| 29 | warn => 3, | ||||||||||||||||||||||||||||||||
| 30 | warning => 3, | ||||||||||||||||||||||||||||||||
| 31 | error => 4, | ||||||||||||||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 108 | my $log_formats = { | |||||||||||||||||||||||||||||||
| 35 | simple => '[%P] %L @%D> %i%m in %f l. %l', | ||||||||||||||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | sub _log_format { | ||||||||||||||||||||||||||||||||
| 39 | 42 | ▶ | my $config = setting('logger_format'); | ||||||||||||||||||||||||||||||
|
Subroutine
_log_format: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 42 | ▶ | if ( !$config ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 35 | return $log_formats->{simple}; | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | exists $log_formats->{$config} | ||||||||||||||||||||||||||||||||
| 46 | 7 | ▶ | ? return $log_formats->{$config} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | : return $config; | ||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | sub _should { | ||||||||||||||||||||||||||||||||
| 51 | 3054 | ▶ | my ($self, $msg_level) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_should: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 52 | 3054 | ▶ | my $conf_level = setting('log') || 'debug'; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
setting('log') || 'debug'
MC/DC100%1/1
setting('log') || 'debug'
setting('log')
Truth table100%2/2
A
A= setting('log')
| |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 3054 | ▶ | if (!exists $levels->{$conf_level}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 1 | setting('log' => 'debug'); | |||||||||||||||||||||||||||||||
| 56 | 1 | $conf_level = 'debug'; | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 3054 | return $levels->{$conf_level} <= $levels->{$msg_level}; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | sub format_message { | ||||||||||||||||||||||||||||||||
| 63 | 39 | ▶ | my ($self, $level, $message) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
format_message: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 64 | 39 | chomp $message; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 39 | ▶ | $message = Encode::encode(setting('charset'), $message) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | if setting('charset'); | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 39 | ▶ | $level = 'warn' if $level eq 'warning'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 39 | $level = sprintf('%5s', $level); | |||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 39 | my $r = Dancer::SharedData->request; | |||||||||||||||||||||||||||||||
| 73 | 39 | my @stack = caller(3); | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | my $block_handler = sub { | ||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | my ( $block, $type ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | if ( $type eq 't' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 78 | 1 | return "[" . strftime( $block, localtime ) . "]"; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | elsif ( $type eq 'h' ) { | ||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | return '-' unless defined $r; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | return scalar $r->header($block) || '-'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
scalar $r->header($block) || '-'
MC/DC0%0/1
scalar $r->header($block) || '-'
scalar $r->header($block)
Truth table50%1/2
A
A= scalar $r->header($block)
| |||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||||||
| 85 | 0 | Carp::carp("{$block}$type not supported"); | |||||||||||||||||||||||||||||||
| 86 | 0 | return "-"; | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | 39 | }; | |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | my $chars_mapping = { | ||||||||||||||||||||||||||||||||
| 91 | h => sub { | ||||||||||||||||||||||||||||||||
| 92 | defined $r | ||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | ? $r->env->{'HTTP_X_REAL_IP'} || $r->env->{'REMOTE_ADDR'} || '-' | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=23.7
Branch: 1 branch
Condition0%0/5
$r->env->{"HTTP_X_REAL_IP"} or $r->env->{"REMOTE_ADDR"} or "-"
$r->env->{"HTTP_X_REAL_IP"} or $r->env->{"REMOTE_ADDR"}
MC/DC0%0/2
$r->env->{"HTTP_X_REAL_IP"} or $r->env->{"REMOTE_ADDR"} or "-"
$r->env->{"HTTP_X_REAL_IP"}
$r->env->{"REMOTE_ADDR"}
Truth table0%0/3
A or B
A= $r->env->{"HTTP_X_REAL_IP"}B= $r->env->{"REMOTE_ADDR"}
| |||||||||||||||||||||||||||||||||
| 94 | : '-'; | ||||||||||||||||||||||||||||||||
| 95 | }, | ||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | t => sub { Encode::decode(setting('charset') || 'utf8', | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
setting('charset') || 'utf8'
MC/DC0%0/1
setting('charset') || 'utf8'
setting('charset')
Truth table50%1/2
A
A= setting('charset')
| |||||||||||||||||||||||||||||||||
| 97 | POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime )) }, | ||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime ) }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | u => sub { Encode::decode(setting('charset') || 'utf8', | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
setting('charset') || 'utf8'
MC/DC0%0/1
setting('charset') || 'utf8'
setting('charset')
Truth table0%0/2
A
A= setting('charset')
| |||||||||||||||||||||||||||||||||
| 100 | POSIX::strftime( "%d/%b/%Y %H:%M:%S", gmtime )) }, | ||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | U => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", gmtime ) }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 102 | 34 | ▶ | P => sub { $$ }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 103 | 35 | ▶ | L => sub { $level }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 104 | D => sub { | ||||||||||||||||||||||||||||||||
| 105 | 34 | ▶ | my $time = Dancer::SharedData->timer->tick; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 106 | 34 | return $time; | |||||||||||||||||||||||||||||||
| 107 | }, | ||||||||||||||||||||||||||||||||
| 108 | 37 | ▶ | m => sub { $message }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 109 | 34 | ▶ | f => sub { $stack[1] || '-' }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 34 | ▶ | l => sub { $stack[2] || '-' }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | i => sub { | ||||||||||||||||||||||||||||||||
| 112 | 34 | ▶ | defined $r ? "[hit #" . $r->id . "]" : ""; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | }, | ||||||||||||||||||||||||||||||||
| 114 | 39 | }; | |||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | my $char_mapping = sub { | ||||||||||||||||||||||||||||||||
| 117 | 246 | ▶ | my $char = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 246 | my $cb = $chars_mapping->{$char}; | |||||||||||||||||||||||||||||||
| 120 | 246 | ▶ | unless ($cb) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | 1 | Carp::carp "\%$char not supported."; | |||||||||||||||||||||||||||||||
| 122 | 1 | return "-"; | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | 245 | $cb->($char); | |||||||||||||||||||||||||||||||
| 125 | 39 | }; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 39 | my $fmt = $self->_log_format(); | |||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 39 | $fmt =~ s^ | |||||||||||||||||||||||||||||||
| 130 | (?: | ||||||||||||||||||||||||||||||||
| 131 | \%\{(.+?)\}([a-z])| | ||||||||||||||||||||||||||||||||
| 132 | \%([a-zA-Z]) | ||||||||||||||||||||||||||||||||
| 133 | ) | ||||||||||||||||||||||||||||||||
| 134 | 248 | ▶ | ^ $1 ? $block_handler->($1, $2) : $char_mapping->($3) ^egx; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 39 | return $fmt."\n"; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 2966 | ▶ | sub core { $_[0]->_should('core') and $_[0]->_log('core', $_[1]) } | ||||||||||||||||||||||||||||||
|
Subroutine
core: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 140 | 15 | ▶ | sub debug { $_[0]->_should('debug') and $_[0]->_log('debug', $_[1]) } | ||||||||||||||||||||||||||||||
|
Subroutine
debug: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | 8 | ▶ | sub info { $_[0]->_should('info') and $_[0]->_log('info', $_[1]) } | ||||||||||||||||||||||||||||||
|
Subroutine
info: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | 17 | ▶ | sub warning { $_[0]->_should('warning') and $_[0]->_log('warning', $_[1]) } | ||||||||||||||||||||||||||||||
|
Subroutine
warning: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | 48 | ▶ | sub error { $_[0]->_should('error') and $_[0]->_log('error', $_[1]) } | ||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 108 | 1; | |||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||||||