line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::Logger::ColorConsole; |
2
|
1
|
|
|
1
|
|
25176
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
4
|
1
|
|
|
1
|
|
5
|
use base 'Dancer::Logger::Abstract'; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
831
|
|
5
|
1
|
|
|
1
|
|
311533
|
use Dancer::Config 'setting'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
61
|
|
6
|
1
|
|
|
1
|
|
1366
|
use Term::ANSIColor; |
|
1
|
|
|
|
|
9692
|
|
|
1
|
|
|
|
|
1079
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.0004'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub _log { |
11
|
0
|
|
|
0
|
|
|
my ($self, $level, $message) = @_; |
12
|
0
|
|
|
|
|
|
print STDERR $self->format_message($level => $message); |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub init { |
16
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
17
|
0
|
|
|
|
|
|
$self->SUPER::init(@_); |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
my $config = setting('engines')->{logger}; |
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
|
$self->{level_colors} = $config->{levels}; |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
0
|
|
|
|
$self->{level_colors}{core} ||= 'reset'; |
24
|
0
|
|
0
|
|
|
|
$self->{level_colors}{debug} ||= 'bright_blue'; |
25
|
0
|
|
0
|
|
|
|
$self->{level_colors}{warn} ||= $self->{level_colors}{warning} || 'bright_yellow'; |
|
|
|
0
|
|
|
|
|
26
|
0
|
|
0
|
|
|
|
$self->{level_colors}{error} ||= 'bright_red'; |
27
|
|
|
|
|
|
|
|
28
|
0
|
0
|
0
|
|
|
|
if (!exists($config->{default_regexps}) || $config->{default_regexps} != 0) { |
29
|
0
|
|
|
|
|
|
push @{$self->{regexps}} => |
|
0
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
( |
31
|
|
|
|
|
|
|
{ re => 'response: 2\d\d', color => 'bright_green' }, |
32
|
|
|
|
|
|
|
{ re => 'response: [45]\d\d', color => 'bright_red' }, |
33
|
|
|
|
|
|
|
{ re => '(?:GET|POST|PUT|DELETE) \S+', color => 'bright_blue' }, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
0
|
0
|
0
|
|
|
|
if (exists($config->{regexps}) && ref($config->{regexps}) eq 'ARRAY') { |
38
|
0
|
|
|
|
|
|
push @{$self->{regexps}} => @{$config->{regexps}}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub format_message { |
44
|
0
|
|
|
0
|
1
|
|
my ($self, $level, $message) = @_; |
45
|
0
|
|
|
|
|
|
chomp $message; |
46
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
if (setting('charset')) { |
48
|
0
|
|
|
|
|
|
$message = Encode::encode(setting('charset'), $message); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
$level = 'warn' if $level eq 'warning'; |
52
|
0
|
|
|
|
|
|
$level = color($self->{level_colors}{$level}) . sprintf('%5s', $level); |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
my $r = Dancer::SharedData->request; |
55
|
0
|
|
|
|
|
|
my @stack = caller(3); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $block_handler = sub { |
58
|
0
|
|
|
0
|
|
|
my ( $block, $type ) = @_; |
59
|
0
|
0
|
|
|
|
|
if ( $type eq 't' ) { |
|
|
0
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
return "[" . strftime( $block, localtime ) . "]"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
elsif ( $type eq 'h' ) { |
63
|
0
|
|
0
|
|
|
|
return scalar $r->header($block) || '-'; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else { |
66
|
0
|
|
|
|
|
|
Carp::carp("{$block}$type not supported"); |
67
|
0
|
|
|
|
|
|
return "-"; |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
|
|
|
|
}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $chars_mapping = { |
72
|
|
|
|
|
|
|
h => sub { |
73
|
0
|
0
|
0
|
0
|
|
|
defined $r |
74
|
|
|
|
|
|
|
? $r->env->{'HTTP_X_REAL_IP'} || $r->env->{'REMOTE_ADDR'} |
75
|
|
|
|
|
|
|
: '-'; |
76
|
|
|
|
|
|
|
}, |
77
|
0
|
|
|
0
|
|
|
t => sub { Encode::decode(setting('charset'), |
78
|
|
|
|
|
|
|
POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime )) }, |
79
|
0
|
|
|
0
|
|
|
T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime ) }, |
80
|
0
|
|
|
0
|
|
|
P => sub { $$ }, |
81
|
0
|
|
|
0
|
|
|
L => sub { $level }, |
82
|
|
|
|
|
|
|
D => sub { |
83
|
0
|
|
|
0
|
|
|
my $time = Dancer::SharedData->timer->tick; |
84
|
0
|
|
|
|
|
|
return $time; |
85
|
|
|
|
|
|
|
}, |
86
|
0
|
|
|
0
|
|
|
m => sub { $message }, |
87
|
0
|
0
|
|
0
|
|
|
f => sub { $stack[1] || '-' }, |
88
|
0
|
0
|
|
0
|
|
|
l => sub { $stack[2] || '-' }, |
89
|
|
|
|
|
|
|
i => sub { |
90
|
0
|
0
|
|
0
|
|
|
defined $r ? "[hit #" . $r->id . "]" : ""; |
91
|
|
|
|
|
|
|
}, |
92
|
0
|
|
|
|
|
|
}; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $char_mapping = sub { |
95
|
0
|
|
|
0
|
|
|
my $char = shift; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
my $cb = $chars_mapping->{$char}; |
98
|
0
|
0
|
|
|
|
|
unless ($cb) { |
99
|
0
|
|
|
|
|
|
Carp::carp "\%$char not supported."; |
100
|
0
|
|
|
|
|
|
return "-"; |
101
|
|
|
|
|
|
|
} |
102
|
0
|
|
|
|
|
|
$cb->($char); |
103
|
0
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $fmt = $self->_log_format(); |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
$fmt =~ s{ |
108
|
|
|
|
|
|
|
(?: |
109
|
|
|
|
|
|
|
\%\{(.+?)\}([a-z])| |
110
|
|
|
|
|
|
|
\%([a-zA-Z]) |
111
|
|
|
|
|
|
|
) |
112
|
0
|
0
|
|
|
|
|
}{ $1 ? $block_handler->($1, $2) : $char_mapping->($3) }egx; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
for my $re (@{$self->{regexps}}) { |
|
0
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
$fmt =~ s/($re->{re})/colored($1, $re->{color})/ge; |
|
0
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
return $fmt . color('reset') . "\n"; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
1; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
__END__ |