lib/Dancer2/Core/Role/Logger.pm

filter: Statementstmt 99.0% 104 / 105 Branchbran 94.4% 34 / 36 Conditioncond 63.6% 14 / 22 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 97.5% 39 / 40 Podpod 77.7% 7 / 9 totaltotal 91.0% 203 / 223 CC 27 SCAR 8.4
CC
27
Cov
93%
__ANON__
5.0
__ANON__
4.6
__ANON__
4.2
CRAP 2.3
« lib/Dancer2/Core/Role/ConfigReader.pm ↑ Summary lib/Dancer2/Core/Role/DSL.pm »
1 package Dancer2::Core::Role::Logger;
2 # ABSTRACT: Role for logger engines
3 150 $Dancer2::Core::Role::Logger::VERSION = '2.3.0';
4 150 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 150 ▶use Dancer2::Core::Time ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 150 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 150 ▶use POSIX 'strftime';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 150 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 150 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 150 with 'Dancer2::Core::Role::Engine';
13  
14 266 ▶sub hook_aliases { +{} }
Subroutine hook_aliases: called CC=1 SCAR=0.0
Pod undocumented
15 63 ▶sub supported_hooks {
Subroutine supported_hooks: called CC=1 SCAR=0.0
Pod undocumented
16 63 qw(
17 engine.logger.before
18 engine.logger.after
19 );
20 }
21  
22 0 ▶sub _build_type {'Logger'}
Subroutine _build_type: not called CC=1 SCAR=6.9
23  
24 # This is the only method to implement by logger engines.
25 # It receives the following arguments:
26 # $msg_level, $msg_content, it gets called only if the configuration allows
27 # a message of the given level to be logged.
28 150 requires 'log';
29  
30 150 has auto_encoding_charset => (
31 is => 'ro',
32 isa => Str,
33 );
34  
35 has app_name => (
36 is => 'ro',
37 isa => Str,
38 33 ▶ default => sub {'-'},
Subroutine __ANON__: called CC=1 SCAR=0.0
39 150 );
40  
41 has log_format => (
42 is => 'rw',
43 isa => Str,
44 246 ▶ default => sub {'[%a:%P] %L @%T> %m in %f l. %l'},
Subroutine __ANON__: called CC=1 SCAR=0.0
45 150 );
46  
47 has caller_stack_size => (
48 is => 'rw',
49 isa => Int,
50 293 ▶ default => sub { 9; },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 150 );
52  
53 150 my $_levels = {
54  
55 # levels < 0 are for core only
56 core => -10,
57  
58 # levels > 0 are for end-users only
59 debug => 1,
60 info => 2,
61 warn => 3,
62 warning => 3,
63 error => 4,
64 };
65  
66 has log_level => (
67 is => 'rw',
68 150 isa => Enum[keys %{$_levels}],
69 269 ▶ default => sub {'debug'},
Subroutine __ANON__: called CC=1 SCAR=0.0
70 150 );
71  
72 3713 ▶sub _should {
Subroutine _should: called CC=1 SCAR=0.0
73 3713 my ( $self, $msg_level ) = @_;
74 3713 my $conf_level = $self->log_level;
75 3713 return $_levels->{$conf_level} <= $_levels->{$msg_level};
76 }
77  
78 154 ▶sub format_message {
Subroutine format_message: called CC=3 SCAR=11.0
Pod documented
79 154 my ( $self, $level, $message ) = @_;
80 154 chomp $message;
81  
82 154 ▶ $message = Encode::encode( $self->auto_encoding_charset, $message )
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_encoding_charset 0 154 154
83 if $self->auto_encoding_charset;
84  
85 154 my $request = $self->request;
86 154 my $config = $self->config;
87  
88 14 ▶ my $block_handler = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
89 14 my ( $block, $type ) = @_;
90 14 ▶ if ( $type eq 't' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 't') { } 3 11 14
elsif ($type eq 'h') { } 10 1 11
91 3 return POSIX::strftime( $block, localtime(time) );
92 }
93 elsif ( $type eq 'h' ) {
94 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
95 }
96 else {
97 1 Carp::carp("{$block}$type not supported");
98 1 return "-";
99 }
100 154 };
101  
102 154 my $chars_mapping = $self->map_chars_to_subs($level, $message, );
103  
104 733 ▶ my $char_mapping = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
105 733 my $char = shift;
106  
107 733 my $cb = $chars_mapping->{$char};
108 733 ▶ if ( !$cb ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 6 727 733
109 6 Carp::carp "%$char not supported.";
110 6 return "-";
111 }
112 727 $cb->($char);
113 154 };
114  
115 154 my $fmt = $self->log_format;
116  
117 154 $fmt =~ s/
118 (?:
119 \%\{(.+?)\}([a-z])|
120 \%([a-zA-Z])
121 )
122 747 ▶ / $1 ? $block_handler->($1, $2) : $char_mapping->($3) /egx;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 14 733 747
123  
124 154 return $fmt . "\n";
125 }
126  
127 # The rendering behind %t/%u: "dd/Mon/yyyy hh:mm:ss", local time or UTC.
128 #
129 # This used to be a single strftime format string. %b is what "dd/Mon/yyyy"
130 # has always meant here -- the locale's abbreviated month name -- so strftime
131 # is still asked for it, and a localised name is what gets logged wherever the
132 # platform can give one. Not every platform can, though: on some Windows perls
133 # %b comes back as the month *number*, and a September log line rendered
134 # "29/9/2026 23:59:25" on the machines in the cpantesters report, where this
135 # format promises 29/Sep/2026. Where strftime does not answer with a name, the
136 # fixed English abbreviation takes over, so the documented format is produced
137 # everywhere without costing anyone the locale-aware behaviour they had.
138 #
139 # (strftime is still used unchanged for the %{...}t block code, where the
140 # format is the caller's own and whatever their C library makes of it is what
141 # they asked for.)
142 #
143 # These are lexical coderefs rather than named subs: Role::Tiny composes every
144 # named sub in this package into each logger class, which is not something a
145 # private helper should do.
146 6 ▶my $month_name = sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
147 6 my @time = @_;
148 6 my $name = POSIX::strftime( '%b', @time );
149 6 ▶ return $name if defined $name && length $name && $name !~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and length $name and not $name =~ /\A\d+\z/ 4 2 6
Condition50%3/6
defined $name and length $name and not $name =~ /\A\d+\z/
!ll&&!rl&&r
024
defined $name and length $name
!ll&&!rl&&r
006
MC/DC33%1/3
defined $name and length $name and not $name =~ /\A\d+\z/
defined $name length $name not $name =~ /\A\d+\z/
Truth table50%2/4
A and B and not(C)
A= defined $nameB= length $nameC= not $name =~ /\A\d+\z/
ABCresult
0XX0
10X0
1100
1111
150 2 return Dancer2::Core::Time::month_abbrev( $time[4] );
151 150 };
152  
153 6 ▶my $date_slashes = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
154 6 my @time = @_;
155 6 return sprintf '%02d/%s/%04d %02d:%02d:%02d',
156 $time[3], $month_name->(@time), $time[5] + 1900,
157 $time[2], $time[1], $time[0];
158 150 };
159  
160 154 ▶sub map_chars_to_subs {
Subroutine map_chars_to_subs: called CC=2 SCAR=7.0
Pod documented
161 154 my ( $self, $level, $message, $caller_delta ) = @_;
162 154 ▶ my @stack = caller($self->caller_stack_size + ($caller_delta // 0));
Condition50%1/2
$caller_delta // 0
l!l
0154
MC/DC0%0/1
$caller_delta // 0
$caller_delta
Truth table50%1/2
A
A= $caller_delta
Aresult
11
00
163 154 my $request = $self->request;
164 return {
165 100 ▶ a => sub { $self->app_name },
Subroutine __ANON__: called CC=1 SCAR=0.0
166 3 ▶ t => sub { $date_slashes->( localtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
167 98 ▶ T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
168 3 ▶ u => sub { $date_slashes->( gmtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
169 2 ▶ U => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", gmtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
170 98 ▶ P => sub {$$},
Subroutine __ANON__: called CC=1 SCAR=0.0
171 104 ▶ L => sub {$level},
Subroutine __ANON__: called CC=1 SCAR=0.0
172 107 ▶ m => sub {$message},
Subroutine __ANON__: called CC=1 SCAR=0.0
173 3 ▶ p => sub { $stack[0] || '-' }, # package
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$stack[0] or "-" 1 2 3
174 99 ▶ f => sub { $stack[1] || '-' }, # filepath
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$stack[1] or "-" 96 3 99
175 99 ▶ l => sub { $stack[2] || '-' }, # line number
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$stack[2] or "-" 96 3 99
176 3 ▶ h => sub {
Subroutine __ANON__: called CC=4 SCAR=15.2
177 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
178 },
179 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
180 154 };
181 };
182  
183 125 ▶sub _serialize {
Subroutine _serialize: called CC=3 SCAR=11.0
184 125 my @vars = @_;
185  
186 125 ▶ return join q{}, map +(
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 13 114 127
defined $_ ? : 113 1 114
187 ref $_
188 ? Data::Dumper->new( [$_] )->Terse(1)->Purity(1)->Indent(0)
189 ->Sortkeys(1)->Dump()
190 : ( defined($_) ? $_ : 'undef' )
191 ), @vars;
192 }
193  
194 133 ▶around 'log' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 133 my ($orig, $self, @args) = @_;
196  
197 133 $self->execute_hook( 'engine.logger.before', $self, @args );
198 133 $self->$orig( @args );
199 133 $self->execute_hook( 'engine.logger.after', $self, @args );
200 150 };
201  
202 3581 ▶sub core {
Subroutine core: called CC=2 SCAR=6.9
Pod documented
203 3581 my ( $self, @args ) = @_;
204 3581 ▶ $self->_should('core') and $self->log( 'core', _serialize(@args) );
Branch: 1 branch
BranchTrueFalseTotal
$self->_should("core") and $self->log("core", _serialize(@args)) 17 3564 3581
205 }
206  
207 37 ▶sub debug {
Subroutine debug: called CC=2 SCAR=6.9
Pod documented
208 37 my ( $self, @args ) = @_;
209 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
210 }
211  
212 20 ▶sub info {
Subroutine info: called CC=2 SCAR=6.9
Pod documented
213 20 my ( $self, @args ) = @_;
214 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
215 }
216  
217 20 ▶sub warning {
Subroutine warning: called CC=2 SCAR=6.9
Pod documented
218 20 my ( $self, @args ) = @_;
219 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
220 }
221  
222 55 ▶sub error {
Subroutine error: called CC=2 SCAR=7.2
Pod documented
223 55 my ( $self, @args ) = @_;
224 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
225 }
226  
227 150 1;
228  
229 __END__
« lib/Dancer2/Core/Role/ConfigReader.pm ↑ Summary lib/Dancer2/Core/Role/DSL.pm »