blib/lib/Dancer/Logger/Abstract.pm

filter: Statementstmt 95.2% 81 / 85 Branchbran 89.4% 34 / 38 Conditioncond 30.7% 4 / 13 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 93.5% 29 / 31 Podpod 100% 6 / 6 totaltotal 86.5% 155 / 179 CC 26 SCAR 11.4
CC
26
Cov
88%
__ANON__
10.8
__ANON__
6.0
__ANON__
5.9
CRAP 3.1
« lib/Dancer/Object.pm ↑ Summary lib/Dancer/Exception.pm »
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.3522';
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 42 ▶sub _log_format {
Subroutine _log_format: called CC=3 SCAR=11.0
39 42 my $config = setting('logger_format');
40  
41 42 ▶ if ( !$config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config) 35 7 42
42 35 return $log_formats->{simple};
43 }
44  
45 exists $log_formats->{$config}
46 7 ▶ ? return $log_formats->{$config}
Branch: 1 branch
BranchTrueFalseTotal
exists $log_formats->{$config} ? : 1 6 7
47 : return $config;
48 }
49  
50 3054 ▶sub _should {
Subroutine _should: called CC=3 SCAR=11.0
51 3054 my ($self, $msg_level) = @_;
52 3054 ▶ my $conf_level = setting('log') || 'debug';
Condition100%2/2
setting('log') || 'debug'
l!l
302628
MC/DC100%1/1
setting('log') || 'debug'
setting('log')
Truth table100%2/2
A
A= setting('log')
Aresult
11
00
53  
54 3054 ▶ if (!exists $levels->{$conf_level}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $levels->{$conf_level}) 1 3053 3054
55 1 setting('log' => 'debug');
56 1 $conf_level = 'debug';
57 }
58  
59 3054 return $levels->{$conf_level} <= $levels->{$msg_level};
60 }
61  
62 39 ▶sub format_message {
Subroutine format_message: called CC=4 SCAR=14.3
Pod documented
63 39 my ($self, $level, $message) = @_;
64 39 chomp $message;
65  
66 39 ▶ $message = Encode::encode(setting('charset'), $message)
Branch: 1 branch
BranchTrueFalseTotal
if setting("charset") 3 36 39
67 if setting('charset');
68  
69 39 ▶ $level = 'warn' if $level eq 'warning';
Branch: 1 branch
BranchTrueFalseTotal
if $level eq "warning" 10 29 39
70 39 $level = sprintf('%5s', $level);
71  
72 39 my $r = Dancer::SharedData->request;
73 39 my @stack = caller(3);
74  
75 2 ▶ my $block_handler = sub {
Subroutine __ANON__: called CC=5 SCAR=17.8
76 2 my ( $block, $type ) = @_;
77 2 ▶ if ( $type eq 't' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 't') { } 1 1 2
elsif ($type eq 'h') { } 1 0 1
78 1 return "[" . strftime( $block, localtime ) . "]";
79 }
80 elsif ( $type eq 'h' ) {
81 1 ▶ return '-' unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 1 1
82 1 ▶ return scalar $r->header($block) || '-';
Condition50%1/2
scalar $r->header($block) || '-'
l!l
10
MC/DC0%0/1
scalar $r->header($block) || '-'
scalar $r->header($block)
Truth table50%1/2
A
A= scalar $r->header($block)
Aresult
11
00
83 }
84 else {
85 0 Carp::carp("{$block}$type not supported");
86 0 return "-";
87 }
88 39 };
89  
90 my $chars_mapping = {
91 1 ▶ h => sub {
Subroutine __ANON__: called CC=4 SCAR=23.7
92 defined $r
93 1 ▶ ? $r->env->{'HTTP_X_REAL_IP'} || $r->env->{'REMOTE_ADDR'} || '-'
Branch: 1 branch
BranchTrueFalseTotal
defined $r ? : 0 1 1
Condition0%0/5
$r->env->{"HTTP_X_REAL_IP"} or $r->env->{"REMOTE_ADDR"} or "-"
l!l
00
$r->env->{"HTTP_X_REAL_IP"} or $r->env->{"REMOTE_ADDR"}
l!l&&r!l&&!r
000
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"}
ABresult
1X1
011
000
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'
l!l
10
MC/DC0%0/1
setting('charset') || 'utf8'
setting('charset')
Truth table50%1/2
A
A= setting('charset')
Aresult
11
00
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'
l!l
00
MC/DC0%0/1
setting('charset') || 'utf8'
setting('charset')
Truth table0%0/2
A
A= setting('charset')
Aresult
11
00
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 34 ▶ D => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
105 34 my $time = Dancer::SharedData->timer->tick;
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
BranchTrueFalseTotal
$stack[1] or "-" 18 16 34
110 34 ▶ l => sub { $stack[2] || '-' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$stack[2] or "-" 18 16 34
111 34 ▶ i => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
112 34 ▶ defined $r ? "[hit #" . $r->id . "]" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $r ? : 2 32 34
113 },
114 39 };
115  
116 246 ▶ my $char_mapping = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
117 246 my $char = shift;
118  
119 246 my $cb = $chars_mapping->{$char};
120 246 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 1 245 246
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
BranchTrueFalseTotal
$1 ? : 2 246 248
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
BranchTrueFalseTotal
$_[0]->_should("core") and $_[0]->_log("core", $_[1]) 1 2965 2966
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
BranchTrueFalseTotal
$_[0]->_should("debug") and $_[0]->_log("debug", $_[1]) 11 4 15
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
BranchTrueFalseTotal
$_[0]->_should("info") and $_[0]->_log("info", $_[1]) 6 2 8
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
BranchTrueFalseTotal
$_[0]->_should("warning") and $_[0]->_log("warning", $_[1]) 16 1 17
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
BranchTrueFalseTotal
$_[0]->_should("error") and $_[0]->_log("error", $_[1]) 48 0 48
144  
145 108 1;
146  
147 __END__
« lib/Dancer/Object.pm ↑ Summary lib/Dancer/Exception.pm »