File Coverage

blib/lib/Dancer2/Logger/Console/Colored.pm
Criterion Covered Total %
statement 60 70 85.7
branch 31 40 77.5
condition 2 4 50.0
subroutine 16 18 88.8
pod 0 4 0.0
total 109 136 80.1


line stmt bran cond sub pod time code
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 .
7             #
8             # This is free software, licensed under:
9             #
10             # The MIT (X11) License
11             #
12              
13             BEGIN {
14 2     2   74176 our $VERSION = '0.006'; # VERSION: generated by DZP::OurPkgVersion
15             }
16              
17 2     2   343 use Term::ANSIColor;
  2         6435  
  2         106  
18 2     2   298 use Moo;
  2         7794  
  2         9  
19 2     2   1640 use Dancer2::Core::Types qw( ArrayRef HashRef Str );
  2         110228  
  2         22  
20              
21             extends 'Dancer2::Logger::Console';
22              
23             has colored_origin => (
24             is => 'rw',
25             isa => Str,
26             );
27              
28             has colored_levels => (
29             is => 'rw',
30             isa => HashRef [Str],
31             default => sub { {} },
32             );
33              
34             has colored_messages => (
35             is => 'rw',
36             isa => HashRef [Str],
37             default => sub { {} },
38             );
39              
40             has colored_regex => (
41             is => 'rw',
42             isa => ArrayRef [ HashRef [Str] ],
43             default => sub { [] },
44             );
45              
46             sub colorize_origin {
47 48     48 0 85 my ( $self, $string ) = @_;
48              
49             # Configured color.
50 48 100       728 return colored( $string, $self->colored_origin ) if $self->colored_origin;
51              
52             # Default colors.
53 27         182 return colored( $string, 'cyan' );
54             }
55              
56             sub colorize_level {
57 16     16 0 29 my ( $self, $level ) = @_;
58 16         50 my $level_tmp = $level =~ s/\s+//gr;
59 16 50       40 $level_tmp = 'warning' if $level_tmp eq 'warn';
60              
61             # Configured color.
62 16 100       254 return colored( $level, $self->colored_levels->{$level_tmp} ) if $self->colored_levels->{$level_tmp};
63              
64             # Default colors.
65 9 100       68 return colored( $level, 'bold bright_white' ) if $level_tmp eq 'core';
66 8 100       28 return colored( $level, 'bold bright_blue' ) if $level_tmp eq 'debug';
67 6 100       18 return colored( $level, 'bold green' ) if $level_tmp eq 'info';
68 4 100       15 return colored( $level, 'bold yellow' ) if $level_tmp eq 'warning';
69 2 50       9 return colored( $level, 'bold yellow on_red' ) if $level_tmp eq 'error';
70 0         0 return colored( $level, 'bold magenta' );
71             }
72              
73             sub colorize_message {
74 16     16 0 32 my ( $self, $level, $message ) = @_;
75 16         51 my $level_tmp = $level =~ s/\s+//gr;
76 16 50       40 $level_tmp = 'warning' if $level_tmp eq 'warn';
77              
78             # Check for regex match.
79 16         21 foreach my $pattern ( @{ $self->colored_regex } ) {
  16         309  
80 3 100       35 if ($message =~ m/$pattern->{re}/) {
81 2         20 $message =~ s{($pattern->{re})}{colored($1, $pattern->{color} )}eg;
  3         38  
82 2         60 return $message;
83             }
84             }
85            
86             # Configured color.
87 14 100       256 return colored( $message, $self->colored_messages->{$level_tmp} ) if $self->colored_messages->{$level_tmp};
88              
89             # Default colors.
90 9 100       70 return colored( $message, 'bold bright_white' ) if $level_tmp eq 'core';
91 8 100       21 return colored( $message, 'bold bright_blue' ) if $level_tmp eq 'debug';
92 6 100       21 return colored( $message, 'bold green' ) if $level_tmp eq 'info';
93 4 100       13 return colored( $message, 'bold yellow' ) if $level_tmp eq 'warning';
94 2 50       9 return colored( $message, 'bold yellow on_red' ) if $level_tmp eq 'error';
95 0         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     16 0 156050 my ( $self, $level, $message ) = @_;
102 16         36 chomp $message;
103              
104 16         57 $level = sprintf( '%5s', $level );
105 16 50       62 $message = Encode::encode( $self->auto_encoding_charset, $message )
106             if $self->auto_encoding_charset;
107              
108 16         117 my @stack = caller(8);
109              
110             my $block_handler = sub {
111 0     0   0 my ( $block, $type ) = @_;
112 0 0       0 if ( $type eq 't' ) {
113 0         0 return "[" . strftime( $block, localtime(time) ) . "]";
114             }
115             else {
116 0         0 Carp::carp("{$block}$type not supported");
117 0         0 return "-";
118             }
119 16         94 };
120              
121             my $chars_mapping = {
122 16     16   53 a => sub { $self->colorize_origin( $self->app_name ) },
123             t => sub {
124 0     0   0 Encode::decode(
125             setting('charset'),
126             POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime(time) ) );
127             },
128 16     16   575 T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime(time) ) },
129 16     16   70 P => sub { $$ },
130 16     16   37 L => sub { $self->colorize_level($level) },
131 16     16   45 m => sub { $self->colorize_message( $level => $message ) },
132 16   50 16   48 f => sub { $self->colorize_origin( $stack[1] || '-' ) },
133 16   50 16   39 l => sub { $self->colorize_origin( $stack[2] || '-' ) },
134 16         184 };
135              
136             my $char_mapping = sub {
137 112     112   200 my $char = shift;
138              
139 112         162 my $cb = $chars_mapping->{$char};
140 112 50       203 if ( !$cb ) {
141 0         0 Carp::carp "\%$char not supported.";
142 0         0 return "-";
143             }
144 112         199 $cb->($char);
145 16         48 };
146              
147 16         262 my $fmt = $self->log_format;
148              
149 16         162 $fmt =~ s/
150             (?:
151             \%\{(.+?)\}([a-z])|
152             \%([a-zA-Z])
153             )
154 112 50       2635 / $1 ? $block_handler->($1, $2) : $char_mapping->($3) /egx;
155              
156 16         1187 return $fmt . "\n";
157             }
158              
159             1;
160              
161             __END__