blib/lib/Dancer2/Logger/Console/Colored.pm

filter: Statementstmt 84.7% 78 / 92 Branchbran 70.4% 31 / 44 Conditioncond 11.5% 3 / 26 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 81.4% 22 / 27 Podpod 0.0% 0 / 4 totaltotal 65.3% 134 / 205 SCAR 42.6
CC
35
Cov
69%
__ANON__
42.0
__ANON__
20.0
__ANON__
12.0
CRAP 71.0
↑ Summary
1 package Dancer2::Logger::Console::Colored;
2 # ABSTRACT: Dancer2 colored console logger.
3 #
4 # This file is part of Dancer2-Logger-Console-Colored
5 #
6 # This software is Copyright (c) 2014 by BURNERSK <burnersk@cpan.org>.
7 #
8 # This is free software, licensed under:
9 #
10 # The MIT (X11) License
11 #
12  
13 BEGIN {
14 2 ▶ our $VERSION = '0.007'; # VERSION: generated by DZP::OurPkgVersion
Subroutine BEGIN: called CC=1 SCAR=0.0
15 }
16  
17 2 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Dancer2::Core::Types qw( ArrayRef HashRef Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 extends 'Dancer2::Logger::Console';
23  
24 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 has colored_origin => (
27 is => 'rw',
28 isa => Str,
29 );
30  
31 has colored_levels => (
32 is => 'rw',
33 isa => HashRef [Str],
34 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 2 );
36  
37 has colored_messages => (
38 is => 'rw',
39 isa => HashRef [Str],
40 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 2 );
42  
43 has colored_regex => (
44 is => 'rw',
45 isa => ArrayRef [ HashRef [Str] ],
46 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 2 );
48  
49 sub colorize_origin {
50 48 ▶ my ( $self, $string ) = @_;
Subroutine colorize_origin: called CC=2 SCAR=6.9
Pod undocumented
51  
52 # Configured color.
53 48 ▶ return colored( $string, $self->colored_origin ) if $self->colored_origin;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colored_origin 21 27 48
54  
55 # Default colors.
56 27 return colored( $string, 'cyan' );
57 }
58  
59 sub colorize_level {
60 16 ▶ my ( $self, $level ) = @_;
Subroutine colorize_level: called CC=8 SCAR=20.9
Pod undocumented
61 16 my $level_tmp = $level =~ s/\s+//gr;
62 16 ▶ $level_tmp = 'warning' if $level_tmp eq 'warn';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "warn" 0 16 16
63  
64 # Configured color.
65 16 ▶ return colored( $level, $self->colored_levels->{$level_tmp} ) if $self->colored_levels->{$level_tmp};
Branch: 1 branch
BranchTrueFalseTotal
if $self->colored_levels->{$level_tmp} 7 9 16
66  
67 # Default colors.
68 9 ▶ return colored( $level, 'bold bright_white' ) if $level_tmp eq 'core';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "core" 1 8 9
69 8 ▶ return colored( $level, 'bold bright_blue' ) if $level_tmp eq 'debug';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "debug" 2 6 8
70 6 ▶ return colored( $level, 'bold green' ) if $level_tmp eq 'info';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "info" 2 4 6
71 4 ▶ return colored( $level, 'bold yellow' ) if $level_tmp eq 'warning';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "warning" 2 2 4
72 2 ▶ return colored( $level, 'bold yellow on_red' ) if $level_tmp eq 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "error" 2 0 2
73 0 return colored( $level, 'bold magenta' );
74 }
75  
76 sub colorize_message {
77 16 ▶ my ( $self, $level, $message ) = @_;
Subroutine colorize_message: called CC=10 SCAR=23.1
Pod undocumented
78 16 my $level_tmp = $level =~ s/\s+//gr;
79 16 ▶ $level_tmp = 'warning' if $level_tmp eq 'warn';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "warn" 0 16 16
80  
81 # Check for regex match.
82 16 foreach my $pattern ( @{ $self->colored_regex } ) {
83 3 ▶ if ($message =~ m/$pattern->{re}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /$pattern->{"re"}/) 2 1 3
84 2 $message =~ s{($pattern->{re})}{colored($1, $pattern->{color} )}eg;
85 2 return $message;
86 }
87 }
88  
89 # Configured color.
90 14 ▶ return colored( $message, $self->colored_messages->{$level_tmp} ) if $self->colored_messages->{$level_tmp};
Branch: 1 branch
BranchTrueFalseTotal
if $self->colored_messages->{$level_tmp} 5 9 14
91  
92 # Default colors.
93 9 ▶ return colored( $message, 'bold bright_white' ) if $level_tmp eq 'core';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "core" 1 8 9
94 8 ▶ return colored( $message, 'bold bright_blue' ) if $level_tmp eq 'debug';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "debug" 2 6 8
95 6 ▶ return colored( $message, 'bold green' ) if $level_tmp eq 'info';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "info" 2 4 6
96 4 ▶ return colored( $message, 'bold yellow' ) if $level_tmp eq 'warning';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "warning" 2 2 4
97 2 ▶ return colored( $message, 'bold yellow on_red' ) if $level_tmp eq 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "error" 2 0 2
98 0 return colored( $message, 'bold magenta' );
99 }
100  
101 # This comes original from Dancer2::Logger::Console. There are a few hooks
102 # required in order to colorize log messages properly.
103 sub format_message {
104 17 ▶ my ( $self, $level, $message ) = @_;
Subroutine format_message: called CC=3 SCAR=15.6
Pod undocumented
105 17 chomp $message;
106  
107 17 $level = sprintf( '%5s', $level );
108 17 ▶ $message = Encode::encode( $self->auto_encoding_charset, $message )
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_encoding_charset 0 17 17
109 if $self->auto_encoding_charset;
110  
111 17 my @stack = caller(8);
112 17 my $request = $self->request;
113 17 my $config = $self->config;
114  
115 my $block_handler = sub {
116 0 ▶ my ( $block, $type ) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
117 0 ▶ if ( $type eq 't' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 't') { } 0 0 0
elsif ($type eq 'h') { } 0 0 0
118 return Encode::decode(
119 0 ▶ $config->{'charset'} || 'UTF-8',
Condition0%0/2
$config->{'charset'} || 'UTF-8'
l!l
00
MC/DC0%0/1
$config->{'charset'} || 'UTF-8'
$config->{'charset'}
Truth table0%0/2
A
A= $config->{'charset'}
Aresult
11
00
120 POSIX::strftime( $block, localtime(time) )
121 );
122 }
123 elsif ( $type eq 'h' ) {
124 0 ▶ return ( $request && $request->header($block) ) || '-';
Condition0%0/5
$request && $request->header($block) || '-'
l!l
00
$request and $request->header($block)
!ll&&!rl&&r
000
MC/DC0%0/2
$request && $request->header($block) || '-'
$request $request->header($block)
Truth table0%0/3
A and B
A= $requestB= $request->header($block)
ABresult
111
0X0
100
125 }
126 else {
127 0 Carp::carp("{$block}$type not supported");
128 0 return "-";
129 }
130 17 };
131  
132 my $chars_mapping = {
133 16 ▶ a => sub { $self->colorize_origin( $self->app_name ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
134 t => sub {
135 Encode::decode(
136 1 ▶ $config->{'charset'} || 'UTF-8',
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$config->{'charset'} || 'UTF-8'
l!l
01
MC/DC0%0/1
$config->{'charset'} || 'UTF-8'
$config->{'charset'}
Truth table50%1/2
A
A= $config->{'charset'}
Aresult
11
00
137 POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime(time) )
138 );
139 },
140 16 ▶ T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
141 u => sub {
142 Encode::decode(
143 0 ▶ $config->{'charset'} || 'UTF-8',
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$config->{'charset'} || 'UTF-8'
l!l
00
MC/DC0%0/1
$config->{'charset'} || 'UTF-8'
$config->{'charset'}
Truth table0%0/2
A
A= $config->{'charset'}
Aresult
11
00
144 POSIX::strftime( "%d/%b/%Y %H:%M:%S", gmtime(time) )
145 );
146 },
147 0 ▶ U => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", gmtime(time) ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 16 ▶ P => sub { $$ },
Subroutine __ANON__: called CC=1 SCAR=0.0
149 16 ▶ L => sub { $self->colorize_level($level) },
Subroutine __ANON__: called CC=1 SCAR=0.0
150 16 ▶ m => sub { $self->colorize_message( $level => $message ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
151 16 ▶ f => sub { $self->colorize_origin( $stack[1] || '-' ) },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$stack[1] || '-'
l!l
160
MC/DC0%0/1
$stack[1] || '-'
$stack[1]
Truth table50%1/2
A
A= $stack[1]
Aresult
11
00
152 16 ▶ l => sub { $self->colorize_origin( $stack[2] || '-' ) },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$stack[2] || '-'
l!l
160
MC/DC0%0/1
$stack[2] || '-'
$stack[2]
Truth table50%1/2
A
A= $stack[2]
Aresult
11
00
153 h => sub {
154 0 ▶ $self->colorize_origin(
Subroutine __ANON__: not called CC=4 SCAR=30.0
Condition0%0/8
$request && ($request->remote_host || $request->address) || '-'
l!l
00
$request and $request->remote_host || $request->address
!ll&&!rl&&r
000
$request->remote_host or $request->address
l!l&&r!l&&!r
000
MC/DC0%0/3
$request && ($request->remote_host || $request->address) || '-'
$request $request->remote_host $request->address
Truth table0%0/4
A and B or C
A= $requestB= $request->remote_hostC= $request->address
ABCresult
11X1
1011
0XX0
1000
155 ( $request && ( $request->remote_host || $request->address ) ) || '-'
156 )
157 },
158 0 ▶ i => sub { ( $request && $request->id ) || '-' },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$request and $request->id or "-" 0 0 0
Condition0%0/3
$request and $request->id
!ll&&!rl&&r
000
MC/DC0%0/2
$request and $request->id
$request $request->id
Truth table0%0/3
A and B
A= $requestB= $request->id
ABresult
0X0
100
111
159 17 };
160  
161 my $char_mapping = sub {
162 113 ▶ my $char = shift;
Subroutine __ANON__: called CC=2 SCAR=7.9
163  
164 113 my $cb = $chars_mapping->{$char};
165 113 ▶ if ( !$cb ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 113 113
166 0 Carp::carp "\%$char not supported.";
167 0 return "-";
168 }
169 113 $cb->($char);
170 17 };
171  
172 17 my $fmt = $self->log_format;
173  
174 17 $fmt =~ s/
175 (?:
176 \%\{(.+?)\}([a-z])|
177 \%([a-zA-Z])
178 )
179 113 ▶ / $1 ? $block_handler->($1, $2) : $char_mapping->($3) /egx;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 113 113
180  
181 17 return $fmt . "\n";
182 }
183  
184 2 1;
185  
186 __END__
↑ Summary