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

filter: Statementstmt 87.3% 69 / 79 Branchbran 77.5% 31 / 40 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.4% 19 / 21 Podpod 0.0% 0 / 4 totaltotal 80.6% 121 / 150 CC 24 SCAR 16.0
CC
24
Cov
83%
colorize_message
10.1
colorize_level
8.1
__ANON__
6.0
CRAP 5.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.006'; # 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  
21 2 extends 'Dancer2::Logger::Console';
22  
23 2 has colored_origin => (
24 is => 'rw',
25 isa => Str,
26 );
27  
28 has colored_levels => (
29 is => 'rw',
30 isa => HashRef [Str],
31 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 2 );
33  
34 has colored_messages => (
35 is => 'rw',
36 isa => HashRef [Str],
37 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 2 );
39  
40 has colored_regex => (
41 is => 'rw',
42 isa => ArrayRef [ HashRef [Str] ],
43 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 2 );
45  
46 sub colorize_origin {
47 48 ▶ my ( $self, $string ) = @_;
Subroutine colorize_origin: called CC=2 SCAR=6.9
Pod undocumented
48  
49 # Configured color.
50 48 ▶ return colored( $string, $self->colored_origin ) if $self->colored_origin;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colored_origin 21 27 48
51  
52 # Default colors.
53 27 return colored( $string, 'cyan' );
54 }
55  
56 sub colorize_level {
57 16 ▶ my ( $self, $level ) = @_;
Subroutine colorize_level: called CC=8 SCAR=20.9
Pod undocumented
58 16 my $level_tmp = $level =~ s/\s+//gr;
59 16 ▶ $level_tmp = 'warning' if $level_tmp eq 'warn';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "warn" 0 16 16
60  
61 # Configured color.
62 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
63  
64 # Default colors.
65 9 ▶ return colored( $level, 'bold bright_white' ) if $level_tmp eq 'core';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "core" 1 8 9
66 8 ▶ return colored( $level, 'bold bright_blue' ) if $level_tmp eq 'debug';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "debug" 2 6 8
67 6 ▶ return colored( $level, 'bold green' ) if $level_tmp eq 'info';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "info" 2 4 6
68 4 ▶ return colored( $level, 'bold yellow' ) if $level_tmp eq 'warning';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "warning" 2 2 4
69 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
70 0 return colored( $level, 'bold magenta' );
71 }
72  
73 sub colorize_message {
74 16 ▶ my ( $self, $level, $message ) = @_;
Subroutine colorize_message: called CC=10 SCAR=23.1
Pod undocumented
75 16 my $level_tmp = $level =~ s/\s+//gr;
76 16 ▶ $level_tmp = 'warning' if $level_tmp eq 'warn';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "warn" 0 16 16
77  
78 # Check for regex match.
79 16 foreach my $pattern ( @{ $self->colored_regex } ) {
80 3 ▶ if ($message =~ m/$pattern->{re}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /$pattern->{"re"}/) 2 1 3
81 2 $message =~ s{($pattern->{re})}{colored($1, $pattern->{color} )}eg;
82 2 return $message;
83 }
84 }
85
86 # Configured color.
87 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
88  
89 # Default colors.
90 9 ▶ return colored( $message, 'bold bright_white' ) if $level_tmp eq 'core';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "core" 1 8 9
91 8 ▶ return colored( $message, 'bold bright_blue' ) if $level_tmp eq 'debug';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "debug" 2 6 8
92 6 ▶ return colored( $message, 'bold green' ) if $level_tmp eq 'info';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "info" 2 4 6
93 4 ▶ return colored( $message, 'bold yellow' ) if $level_tmp eq 'warning';
Branch: 1 branch
BranchTrueFalseTotal
if $level_tmp eq "warning" 2 2 4
94 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
95 0 return colored( $message, 'bold magenta' );
96 }
97  
98 # This comes original from Dancer2::Logger::Console. There are a few hooks
99 # required in order to colorize log messages propably.
100 sub format_message {
101 16 ▶ my ( $self, $level, $message ) = @_;
Subroutine format_message: called CC=3 SCAR=12.2
Pod undocumented
102 16 chomp $message;
103  
104 16 $level = sprintf( '%5s', $level );
105 16 ▶ $message = Encode::encode( $self->auto_encoding_charset, $message )
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_encoding_charset 0 16 16
106 if $self->auto_encoding_charset;
107  
108 16 my @stack = caller(8);
109  
110 my $block_handler = sub {
111 0 ▶ my ( $block, $type ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
112 0 ▶ if ( $type eq 't' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 't') { } 0 0 0
113 0 return "[" . strftime( $block, localtime(time) ) . "]";
114 }
115 else {
116 0 Carp::carp("{$block}$type not supported");
117 0 return "-";
118 }
119 16 };
120  
121 my $chars_mapping = {
122 16 ▶ a => sub { $self->colorize_origin( $self->app_name ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
123 t => sub {
124 0 ▶ Encode::decode(
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 setting('charset'),
126 POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime(time) ) );
127 },
128 16 ▶ T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime(time) ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
129 16 ▶ P => sub { $$ },
Subroutine __ANON__: called CC=1 SCAR=0.0
130 16 ▶ L => sub { $self->colorize_level($level) },
Subroutine __ANON__: called CC=1 SCAR=0.0
131 16 ▶ m => sub { $self->colorize_message( $level => $message ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
132 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
133 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
134 16 };
135  
136 my $char_mapping = sub {
137 112 ▶ my $char = shift;
Subroutine __ANON__: called CC=2 SCAR=7.9
138  
139 112 my $cb = $chars_mapping->{$char};
140 112 ▶ if ( !$cb ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 112 112
141 0 Carp::carp "\%$char not supported.";
142 0 return "-";
143 }
144 112 $cb->($char);
145 16 };
146  
147 16 my $fmt = $self->log_format;
148  
149 16 $fmt =~ s/
150 (?:
151 \%\{(.+?)\}([a-z])|
152 \%([a-zA-Z])
153 )
154 112 ▶ / $1 ? $block_handler->($1, $2) : $char_mapping->($3) /egx;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 112 112
155  
156 16 return $fmt . "\n";
157 }
158  
159 2 1;
160  
161 __END__
↑ Summary