blib/lib/Dancer2/Core/Role/Logger.pm

filter: Statementstmt 98.9% 93 / 94 Branchbran 94.1% 32 / 34 Conditioncond 68.7% 11 / 16 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 97.3% 36 / 37 Podpod 77.7% 7 / 9 totaltotal 92.4% 183 / 198 CC 24 SCAR 8.0
CC
24
Cov
94%
__ANON__
5.0
__ANON__
4.6
__ANON__
3.0
CRAP 2.2
« blib/lib/Dancer2/Core/Role/DSL.pm ↑ Summary blib/lib/Dancer2/Core/Role/HasEnvironment.pm »
1 package Dancer2::Core::Role::Logger;
2 # ABSTRACT: Role for logger engines
3 148 $Dancer2::Core::Role::Logger::VERSION = '2.2.2';
4 148 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 148 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 148 ▶use POSIX 'strftime';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 148 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 148 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 148 with 'Dancer2::Core::Role::Engine';
12  
13 258 ▶sub hook_aliases { +{} }
Subroutine hook_aliases: called CC=1 SCAR=0.0
Pod undocumented
14 62 ▶sub supported_hooks {
Subroutine supported_hooks: called CC=1 SCAR=0.0
Pod undocumented
15 62 qw(
16 engine.logger.before
17 engine.logger.after
18 );
19 }
20  
21 0 ▶sub _build_type {'Logger'}
Subroutine _build_type: not called CC=1 SCAR=6.9
22  
23 # This is the only method to implement by logger engines.
24 # It receives the following arguments:
25 # $msg_level, $msg_content, it gets called only if the configuration allows
26 # a message of the given level to be logged.
27 148 requires 'log';
28  
29 148 has auto_encoding_charset => (
30 is => 'ro',
31 isa => Str,
32 );
33  
34 has app_name => (
35 is => 'ro',
36 isa => Str,
37 33 ▶ default => sub {'-'},
Subroutine __ANON__: called CC=1 SCAR=0.0
38 148 );
39  
40 has log_format => (
41 is => 'rw',
42 isa => Str,
43 243 ▶ default => sub {'[%a:%P] %L @%T> %m in %f l. %l'},
Subroutine __ANON__: called CC=1 SCAR=0.0
44 148 );
45  
46 has caller_stack_size => (
47 is => 'rw',
48 isa => Int,
49 288 ▶ default => sub { 9; },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 148 );
51  
52 148 my $_levels = {
53  
54 # levels < 0 are for core only
55 core => -10,
56  
57 # levels > 0 are for end-users only
58 debug => 1,
59 info => 2,
60 warn => 3,
61 warning => 3,
62 error => 4,
63 };
64  
65 has log_level => (
66 is => 'rw',
67 148 isa => Enum[keys %{$_levels}],
68 264 ▶ default => sub {'debug'},
Subroutine __ANON__: called CC=1 SCAR=0.0
69 148 );
70  
71 3669 ▶sub _should {
Subroutine _should: called CC=1 SCAR=0.0
72 3669 my ( $self, $msg_level ) = @_;
73 3669 my $conf_level = $self->log_level;
74 3669 return $_levels->{$conf_level} <= $_levels->{$msg_level};
75 }
76  
77 148 ▶sub format_message {
Subroutine format_message: called CC=3 SCAR=11.0
Pod documented
78 148 my ( $self, $level, $message ) = @_;
79 148 chomp $message;
80  
81 148 ▶ $message = Encode::encode( $self->auto_encoding_charset, $message )
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_encoding_charset 0 148 148
82 if $self->auto_encoding_charset;
83  
84 148 my $request = $self->request;
85 148 my $config = $self->config;
86  
87 14 ▶ my $block_handler = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
88 14 my ( $block, $type ) = @_;
89 14 ▶ if ( $type eq 't' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 't') { } 3 11 14
elsif ($type eq 'h') { } 10 1 11
90 3 return POSIX::strftime( $block, localtime(time) );
91 }
92 elsif ( $type eq 'h' ) {
93 10 ▶ return ( $request && $request->header($block) ) || '-';
Condition100%5/5
$request && $request->header($block) || '-'
l!l
37
$request and $request->header($block)
!ll&&!rl&&r
163
MC/DC100%2/2
$request && $request->header($block) || '-'
$request $request->header($block)
Truth table100%3/3
A and B
A= $requestB= $request->header($block)
ABresult
111
0X0
100
94 }
95 else {
96 1 Carp::carp("{$block}$type not supported");
97 1 return "-";
98 }
99 148 };
100  
101 148 my $chars_mapping = $self->map_chars_to_subs($level, $message, );
102  
103 703 ▶ my $char_mapping = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
104 703 my $char = shift;
105  
106 703 my $cb = $chars_mapping->{$char};
107 703 ▶ if ( !$cb ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 6 697 703
108 6 Carp::carp "%$char not supported.";
109 6 return "-";
110 }
111 697 $cb->($char);
112 148 };
113  
114 148 my $fmt = $self->log_format;
115  
116 148 $fmt =~ s/
117 (?:
118 \%\{(.+?)\}([a-z])|
119 \%([a-zA-Z])
120 )
121 717 ▶ / $1 ? $block_handler->($1, $2) : $char_mapping->($3) /egx;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 14 703 717
122  
123 148 return $fmt . "\n";
124 }
125  
126 148 ▶sub map_chars_to_subs {
Subroutine map_chars_to_subs: called CC=2 SCAR=7.0
Pod documented
127 148 my ( $self, $level, $message, $caller_delta ) = @_;
128 148 ▶ my @stack = caller($self->caller_stack_size + ($caller_delta // 0));
Condition50%1/2
$caller_delta // 0
l!l
0148
MC/DC0%0/1
$caller_delta // 0
$caller_delta
Truth table50%1/2
A
A= $caller_delta
Aresult
11
00
129 148 my $request = $self->request;
130 return {
131 96 ▶ a => sub { $self->app_name },
Subroutine __ANON__: called CC=1 SCAR=0.0
132 2 ▶ t => sub { POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
133 94 ▶ T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
134 2 ▶ u => sub { POSIX::strftime( "%d/%b/%Y %H:%M:%S", gmtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
135 2 ▶ U => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", gmtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
136 94 ▶ P => sub {$$},
Subroutine __ANON__: called CC=1 SCAR=0.0
137 100 ▶ L => sub {$level},
Subroutine __ANON__: called CC=1 SCAR=0.0
138 103 ▶ m => sub {$message},
Subroutine __ANON__: called CC=1 SCAR=0.0
139 3 ▶ p => sub { $stack[0] || '-' }, # package
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$stack[0] or "-" 1 2 3
140 95 ▶ f => sub { $stack[1] || '-' }, # filepath
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$stack[1] or "-" 92 3 95
141 95 ▶ l => sub { $stack[2] || '-' }, # line number
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$stack[2] or "-" 92 3 95
142 3 ▶ h => sub {
Subroutine __ANON__: called CC=4 SCAR=15.2
143 3 ▶ ( $request && ( $request->remote_host || $request->address ) ) || '-'
Branch: 1 branch
BranchTrueFalseTotal
$request and $request->remote_host || $request->address or "-" 2 1 3
Condition50%3/6
$request and $request->remote_host || $request->address
!ll&&!rl&&r
102
$request->remote_host or $request->address
l!l&&r!l&&!r
020
MC/DC33%1/3
$request and $request->remote_host || $request->address
$request $request->remote_host $request->address
Truth table50%2/4
A and B or C
A= $requestB= $request->remote_hostC= $request->address
ABCresult
0XX0
1000
11X1
1011
144 },
145 8 ▶ i => sub { ( $request && $request->id ) || '-' },
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$request and $request->id or "-" 7 1 8
Condition66%2/3
$request and $request->id
!ll&&!rl&&r
107
MC/DC50%1/2
$request and $request->id
$request $request->id
Truth table66%2/3
A and B
A= $requestB= $request->id
ABresult
0X0
100
111
146 148 };
147 };
148  
149 121 ▶sub _serialize {
Subroutine _serialize: called CC=3 SCAR=11.0
150 121 my @vars = @_;
151  
152 121 ▶ return join q{}, map +(
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 13 110 123
defined $_ ? : 109 1 110
153 ref $_
154 ? Data::Dumper->new( [$_] )->Terse(1)->Purity(1)->Indent(0)
155 ->Sortkeys(1)->Dump()
156 : ( defined($_) ? $_ : 'undef' )
157 ), @vars;
158 }
159  
160 129 ▶around 'log' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
161 129 my ($orig, $self, @args) = @_;
162  
163 129 $self->execute_hook( 'engine.logger.before', $self, @args );
164 129 $self->$orig( @args );
165 129 $self->execute_hook( 'engine.logger.after', $self, @args );
166 148 };
167  
168 3537 ▶sub core {
Subroutine core: called CC=2 SCAR=6.9
Pod documented
169 3537 my ( $self, @args ) = @_;
170 3537 ▶ $self->_should('core') and $self->log( 'core', _serialize(@args) );
Branch: 1 branch
BranchTrueFalseTotal
$self->_should("core") and $self->log("core", _serialize(@args)) 13 3524 3537
171 }
172  
173 37 ▶sub debug {
Subroutine debug: called CC=2 SCAR=6.9
Pod documented
174 37 my ( $self, @args ) = @_;
175 37 ▶ $self->_should('debug') and $self->log( 'debug', _serialize(@args) );
Branch: 1 branch
BranchTrueFalseTotal
$self->_should("debug") and $self->log("debug", _serialize(@args)) 19 18 37
176 }
177  
178 20 ▶sub info {
Subroutine info: called CC=2 SCAR=6.9
Pod documented
179 20 my ( $self, @args ) = @_;
180 20 ▶ $self->_should('info') and $self->log( 'info', _serialize(@args) );
Branch: 1 branch
BranchTrueFalseTotal
$self->_should("info") and $self->log("info", _serialize(@args)) 16 4 20
181 }
182  
183 20 ▶sub warning {
Subroutine warning: called CC=2 SCAR=6.9
Pod documented
184 20 my ( $self, @args ) = @_;
185 20 ▶ $self->_should('warning') and $self->log( 'warning', _serialize(@args) );
Branch: 1 branch
BranchTrueFalseTotal
$self->_should("warning") and $self->log("warning", _serialize(@args)) 18 2 20
186 }
187  
188 55 ▶sub error {
Subroutine error: called CC=2 SCAR=7.2
Pod documented
189 55 my ( $self, @args ) = @_;
190 55 ▶ $self->_should('error') and $self->log( 'error', _serialize(@args) );
Branch: 1 branch
BranchTrueFalseTotal
$self->_should("error") and $self->log("error", _serialize(@args)) 55 0 55
191 }
192  
193 148 1;
194  
195 __END__
« blib/lib/Dancer2/Core/Role/DSL.pm ↑ Summary blib/lib/Dancer2/Core/Role/HasEnvironment.pm »