line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Plugin::Log::Colorful; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
29981
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
71
|
|
4
|
2
|
|
|
2
|
|
11
|
use vars qw($TEXT $BACKGROUND); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
97
|
|
5
|
2
|
|
|
2
|
|
5138
|
use Term::ANSIColor; |
|
2
|
|
|
|
|
18339
|
|
|
2
|
|
|
|
|
182
|
|
6
|
2
|
|
|
2
|
|
2716
|
use Data::Dumper; |
|
2
|
|
|
|
|
15076
|
|
|
2
|
|
|
|
|
563
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.15'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub setup { |
11
|
0
|
|
|
0
|
|
|
my $c = shift; |
12
|
|
|
|
|
|
|
|
13
|
0
|
|
0
|
|
|
|
my $config = $c->config->{'Plugin::Log::Colorful'} || $c->config->{log_colorful}; |
14
|
0
|
|
0
|
|
|
|
$TEXT = $config->{text} || 'red' ; |
15
|
0
|
|
|
|
|
|
$BACKGROUND = $config->{background}; |
16
|
|
|
|
|
|
|
|
17
|
0
|
0
|
|
|
|
|
if ( !$config->{on_backward_compatibility} ) { |
18
|
0
|
|
|
|
|
|
$c->log( Catalyst::Plugin::Log::Colorful::_::Log->new( { color_table => $config->{color_table} } ) ) ; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
|
$c = $c->next::method(@_); |
22
|
0
|
|
|
|
|
|
return $c; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub Catalyst::Log::color { |
26
|
0
|
|
|
0
|
|
|
my ( $s ,$var, $color , $bg_color ) = @_; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# is not debug mdoe. |
29
|
0
|
0
|
|
|
|
|
return unless $s->is_debug; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
0
|
|
|
|
$color = $color || $Catalyst::Plugin::Log::Colorful::TEXT ; |
32
|
0
|
|
0
|
|
|
|
$bg_color = $bg_color || $Catalyst::Plugin::Log::Colorful::BACKGROUND ; |
33
|
|
|
|
|
|
|
|
34
|
0
|
0
|
0
|
|
|
|
if ( ref $var eq 'ARRAY' or ref $var eq 'HASH') { |
35
|
0
|
|
|
|
|
|
$var = Dumper( $var ); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
0
|
0
|
|
|
|
|
if ( $bg_color ) { |
39
|
0
|
|
|
|
|
|
$color .= " on_$bg_color"; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
$s->debug( color( $color ) . $var .color('reset')); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
package Catalyst::Plugin::Log::Colorful::_::Log; |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
2
|
|
18
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
63
|
|
48
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
58
|
|
49
|
2
|
|
|
2
|
|
10
|
use base qw/Catalyst::Log/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1022
|
|
50
|
2
|
|
|
2
|
|
11
|
use Term::ANSIColor; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
94
|
|
51
|
2
|
|
|
2
|
|
9
|
use Data::Dumper; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
990
|
|
52
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/color_table/); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub new { |
55
|
0
|
|
|
0
|
|
|
my $class = shift; |
56
|
0
|
|
|
|
|
|
my $args = shift; |
57
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(); |
58
|
0
|
|
|
|
|
|
$self->{color_table} = $args->{color_table}; |
59
|
0
|
|
|
|
|
|
return $self; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
sub warn { |
62
|
0
|
|
|
0
|
|
|
my ( $s ,$var, $color , $bg_color ) = @_; |
63
|
0
|
0
|
|
|
|
|
return unless $s->is_warn; |
64
|
0
|
|
0
|
|
|
|
$color = $color || $s->{color_table}{warn}{color} || 'yellow' ; |
65
|
0
|
|
0
|
|
|
|
$bg_color = $bg_color || $s->{color_table}{warn}{bg_color} ; |
66
|
0
|
|
|
|
|
|
$s->_colorful_log('warn', $var, $color , $bg_color ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
sub error{ |
69
|
0
|
|
|
0
|
|
|
my ( $s ,$var, $color , $bg_color ) = @_; |
70
|
0
|
0
|
|
|
|
|
return unless $s->is_error; |
71
|
0
|
|
0
|
|
|
|
$color = $color || $s->{color_table}{error}{color} || 'red' ; |
72
|
0
|
|
0
|
|
|
|
$bg_color = $bg_color || $s->{color_table}{error}{bg_color} ; |
73
|
0
|
|
|
|
|
|
$s->_colorful_log('error', $var, $color , $bg_color ); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
sub fatal{ |
76
|
0
|
|
|
0
|
|
|
my ( $s ,$var, $color , $bg_color ) = @_; |
77
|
0
|
0
|
|
|
|
|
return unless $s->is_fatal; |
78
|
0
|
|
0
|
|
|
|
$color = $color || $s->{color_table}{fatal}{color} || 'white'; |
79
|
0
|
|
0
|
|
|
|
$bg_color = $bg_color || $s->{color_table}{fatal}{bg_color} || 'red'; |
80
|
0
|
|
|
|
|
|
$s->_colorful_log('fatal', $var, $color , $bg_color ); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
sub debug { |
83
|
0
|
|
|
0
|
|
|
my ( $s ,$var, $color , $bg_color ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
0
|
0
|
|
|
|
|
return unless $s->is_debug; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
0
|
|
|
|
$color = $color || $s->{color_table}{debug}{color} || 'black'; |
88
|
0
|
|
0
|
|
|
|
$bg_color = $bg_color || $s->{color_table}{debug}{bg_color} || 'white'; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
$s->_colorful_log('debug', $var, $color , $bg_color ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
sub info { |
93
|
0
|
|
|
0
|
|
|
my ( $s ,$var, $color , $bg_color ) = @_; |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
return unless $s->is_info; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
0
|
|
|
|
$color = $color || $s->{color_table}{info}{color} ; |
98
|
0
|
|
0
|
|
|
|
$bg_color = $bg_color || $s->{color_table}{info}{bg_color} ; |
99
|
|
|
|
|
|
|
|
100
|
0
|
0
|
|
|
|
|
if ( $color ) { |
101
|
0
|
|
|
|
|
|
$s->_colorful_log('info', $var, $color , $bg_color ); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
else { |
104
|
0
|
|
|
|
|
|
$s->SUPER::info( $var ); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub _colorful_log { |
109
|
0
|
|
|
0
|
|
|
my ( $s , $type, $var , $color , $bg_color ) = @_; |
110
|
|
|
|
|
|
|
|
111
|
0
|
0
|
|
|
|
|
if ( ref $var ) { |
112
|
0
|
|
|
|
|
|
$var = Dumper( $var ); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
if ( $bg_color ) { |
116
|
0
|
|
|
|
|
|
$color .= " on_$bg_color"; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
my $method = 'SUPER::' . $type; |
120
|
0
|
|
|
|
|
|
$s->$method( color( $color ) . $var .color('reset')); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1 |
124
|
|
|
|
|
|
|
; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 NAME |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Catalyst::Plugin::Log::Colorful - Catalyst Plugin for Colorful Log |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 SYNOPSIS |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub foo : Private { |
133
|
|
|
|
|
|
|
my ($self , $c ) = @_; |
134
|
|
|
|
|
|
|
$c->log->debug('debug'); |
135
|
|
|
|
|
|
|
$c->log->info( 'info'); |
136
|
|
|
|
|
|
|
$c->log->warn( 'warn'); |
137
|
|
|
|
|
|
|
$c->log->error('error'); |
138
|
|
|
|
|
|
|
$c->log->fatal('fatal'); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
$c->log->debug('debug' , 'red', 'white'); |
141
|
|
|
|
|
|
|
$c->log->warn( 'warn' , 'blue' ); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
myapp.yml # default color is set but can change. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
'Plugin::Log::Colorful' : |
147
|
|
|
|
|
|
|
color_table : |
148
|
|
|
|
|
|
|
debug : |
149
|
|
|
|
|
|
|
color : white |
150
|
|
|
|
|
|
|
bg_color : blue |
151
|
|
|
|
|
|
|
warn : |
152
|
|
|
|
|
|
|
color : blue |
153
|
|
|
|
|
|
|
bg_color : green |
154
|
|
|
|
|
|
|
error : |
155
|
|
|
|
|
|
|
color : red |
156
|
|
|
|
|
|
|
bg_color : yellow |
157
|
|
|
|
|
|
|
fatal : |
158
|
|
|
|
|
|
|
color : red |
159
|
|
|
|
|
|
|
bg_color : green |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 DESCRIPTION |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Sometimes when I am monitoring 'tail -f error_log' or './script/my_server.pl' |
164
|
|
|
|
|
|
|
during develop phase, I could not find log message because of a lot of |
165
|
|
|
|
|
|
|
logs. This plugin may help to find it out. This plugin is using L<Term::ANSIColor>. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Of course when you open log file with vi or some editor, the color wont |
168
|
|
|
|
|
|
|
change and also you will see additional log such as '[31;47moraora[0m'. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 BACKWARD COMPATIBILITY |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
for new version I remove $c->log->color() but still you can use if you turn on on_backward_compatibility setting. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This plugin injects a color() method into the L<Catalyst::Log> namespace. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
use Catalyst qw/-Debug ConfigLoader Log::Colorful/; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
__PACKAGE__->config( |
179
|
|
|
|
|
|
|
name => 'MyApp' , |
180
|
|
|
|
|
|
|
'Plugin::Log::Colorful' => { |
181
|
|
|
|
|
|
|
on_backward_compatibility => 1, |
182
|
|
|
|
|
|
|
text => 'blue', |
183
|
|
|
|
|
|
|
background => 'green', |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
In your controller. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
$c->log->color('hello'); |
190
|
|
|
|
|
|
|
$c->log->color('hello blue' , 'blue'); |
191
|
|
|
|
|
|
|
$c->log->color('hello red on white' , 'red' , 'white'); |
192
|
|
|
|
|
|
|
$c->log->color( $hash_ref ); |
193
|
|
|
|
|
|
|
$c->log->color( $array_ref ); |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 METHOD |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head2 setup |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 SEE ALSO |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
L<Catalyst::Log> |
203
|
|
|
|
|
|
|
L<Term::ANSIColor> |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 AUTHOR |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Tomohiro Teranishi <tomohiro.teranishi@gmail.com> |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=cut |