line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::Logger::Abstract; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:SUKRIA'; |
3
|
|
|
|
|
|
|
#ABSTRACT: Abstract logging engine for Dancer |
4
|
|
|
|
|
|
|
$Dancer::Logger::Abstract::VERSION = '1.3521'; |
5
|
99
|
|
|
99
|
|
1338
|
use strict; |
|
99
|
|
|
|
|
268
|
|
|
99
|
|
|
|
|
2862
|
|
6
|
99
|
|
|
99
|
|
656
|
use warnings; |
|
99
|
|
|
|
|
291
|
|
|
99
|
|
|
|
|
2299
|
|
7
|
99
|
|
|
99
|
|
581
|
use Carp; |
|
99
|
|
|
|
|
244
|
|
|
99
|
|
|
|
|
5411
|
|
8
|
99
|
|
|
99
|
|
694
|
use base 'Dancer::Engine'; |
|
99
|
|
|
|
|
286
|
|
|
99
|
|
|
|
|
12168
|
|
9
|
|
|
|
|
|
|
|
10
|
99
|
|
|
99
|
|
3028
|
use Dancer::SharedData; |
|
99
|
|
|
|
|
299
|
|
|
99
|
|
|
|
|
2746
|
|
11
|
99
|
|
|
99
|
|
656
|
use Dancer::Timer; |
|
99
|
|
|
|
|
294
|
|
|
99
|
|
|
|
|
3065
|
|
12
|
99
|
|
|
99
|
|
747
|
use Dancer::Config 'setting'; |
|
99
|
|
|
|
|
264
|
|
|
99
|
|
|
|
|
5377
|
|
13
|
99
|
|
|
99
|
|
47888
|
use POSIX qw/strftime/; |
|
99
|
|
|
|
|
598804
|
|
|
99
|
|
|
|
|
811
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# This is the only method to implement by logger engines. |
16
|
|
|
|
|
|
|
# It receives the following arguments: |
17
|
|
|
|
|
|
|
# $msg_level, $msg_content, it gets called only if the configuration allows |
18
|
|
|
|
|
|
|
# a message of the given level to be logged. |
19
|
16
|
|
|
16
|
|
2749
|
sub _log { confess "_log not implemented" } |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $levels = { |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# levels < 0 are for core only |
24
|
|
|
|
|
|
|
core => -10, |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# levels > 0 are for end-users only |
27
|
|
|
|
|
|
|
debug => 1, |
28
|
|
|
|
|
|
|
info => 2, |
29
|
|
|
|
|
|
|
warn => 3, |
30
|
|
|
|
|
|
|
warning => 3, |
31
|
|
|
|
|
|
|
error => 4, |
32
|
|
|
|
|
|
|
}; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $log_formats = { |
35
|
|
|
|
|
|
|
simple => '[%P] %L @%D> %i%m in %f l. %l', |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _log_format { |
39
|
34
|
|
|
34
|
|
97
|
my $config = setting('logger_format'); |
40
|
|
|
|
|
|
|
|
41
|
34
|
100
|
|
|
|
96
|
if ( !$config ) { |
42
|
27
|
|
|
|
|
75
|
return $log_formats->{simple}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
exists $log_formats->{$config} |
46
|
7
|
100
|
|
|
|
26
|
? return $log_formats->{$config} |
47
|
|
|
|
|
|
|
: return $config; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _should { |
51
|
3067
|
|
|
3067
|
|
5182
|
my ($self, $msg_level) = @_; |
52
|
3067
|
|
100
|
|
|
6339
|
my $conf_level = setting('log') || 'debug'; |
53
|
|
|
|
|
|
|
|
54
|
3067
|
100
|
|
|
|
7288
|
if (!exists $levels->{$conf_level}) { |
55
|
1
|
|
|
|
|
4
|
setting('log' => 'debug'); |
56
|
1
|
|
|
|
|
3
|
$conf_level = 'debug'; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
3067
|
|
|
|
|
10196
|
return $levels->{$conf_level} <= $levels->{$msg_level}; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub format_message { |
63
|
31
|
|
|
31
|
1
|
4766
|
my ($self, $level, $message) = @_; |
64
|
31
|
|
|
|
|
83
|
chomp $message; |
65
|
|
|
|
|
|
|
|
66
|
31
|
100
|
|
|
|
94
|
$message = Encode::encode(setting('charset'), $message) |
67
|
|
|
|
|
|
|
if setting('charset'); |
68
|
|
|
|
|
|
|
|
69
|
31
|
100
|
|
|
|
205
|
$level = 'warn' if $level eq 'warning'; |
70
|
31
|
|
|
|
|
120
|
$level = sprintf('%5s', $level); |
71
|
|
|
|
|
|
|
|
72
|
31
|
|
|
|
|
140
|
my $r = Dancer::SharedData->request; |
73
|
31
|
|
|
|
|
151
|
my @stack = caller(3); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $block_handler = sub { |
76
|
2
|
|
|
2
|
|
7
|
my ( $block, $type ) = @_; |
77
|
2
|
100
|
|
|
|
11
|
if ( $type eq 't' ) { |
|
|
50
|
|
|
|
|
|
78
|
1
|
|
|
|
|
182
|
return "[" . strftime( $block, localtime ) . "]"; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
elsif ( $type eq 'h' ) { |
81
|
1
|
50
|
|
|
|
3
|
return '-' unless defined $r; |
82
|
1
|
|
50
|
|
|
16
|
return scalar $r->header($block) || '-'; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
0
|
|
|
|
|
0
|
Carp::carp("{$block}$type not supported"); |
86
|
0
|
|
|
|
|
0
|
return "-"; |
87
|
|
|
|
|
|
|
} |
88
|
31
|
|
|
|
|
179
|
}; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my $chars_mapping = { |
91
|
|
|
|
|
|
|
h => sub { |
92
|
|
|
|
|
|
|
defined $r |
93
|
1
|
50
|
0
|
1
|
|
7
|
? $r->env->{'HTTP_X_REAL_IP'} || $r->env->{'REMOTE_ADDR'} || '-' |
94
|
|
|
|
|
|
|
: '-'; |
95
|
|
|
|
|
|
|
}, |
96
|
1
|
|
50
|
1
|
|
3
|
t => sub { Encode::decode(setting('charset') || 'utf8', |
97
|
|
|
|
|
|
|
POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime )) }, |
98
|
1
|
|
|
1
|
|
43
|
T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime ) }, |
99
|
0
|
|
0
|
0
|
|
0
|
u => sub { Encode::decode(setting('charset') || 'utf8', |
100
|
|
|
|
|
|
|
POSIX::strftime( "%d/%b/%Y %H:%M:%S", gmtime )) }, |
101
|
0
|
|
|
0
|
|
0
|
U => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", gmtime ) }, |
102
|
26
|
|
|
26
|
|
141
|
P => sub { $$ }, |
103
|
27
|
|
|
27
|
|
112
|
L => sub { $level }, |
104
|
|
|
|
|
|
|
D => sub { |
105
|
26
|
|
|
26
|
|
109
|
my $time = Dancer::SharedData->timer->tick; |
106
|
26
|
|
|
|
|
143
|
return $time; |
107
|
|
|
|
|
|
|
}, |
108
|
29
|
|
|
29
|
|
125
|
m => sub { $message }, |
109
|
26
|
100
|
|
26
|
|
190
|
f => sub { $stack[1] || '-' }, |
110
|
26
|
100
|
|
26
|
|
137
|
l => sub { $stack[2] || '-' }, |
111
|
|
|
|
|
|
|
i => sub { |
112
|
26
|
100
|
|
26
|
|
149
|
defined $r ? "[hit #" . $r->id . "]" : ""; |
113
|
|
|
|
|
|
|
}, |
114
|
31
|
|
|
|
|
615
|
}; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
my $char_mapping = sub { |
117
|
190
|
|
|
190
|
|
355
|
my $char = shift; |
118
|
|
|
|
|
|
|
|
119
|
190
|
|
|
|
|
314
|
my $cb = $chars_mapping->{$char}; |
120
|
190
|
100
|
|
|
|
388
|
unless ($cb) { |
121
|
1
|
|
|
|
|
265
|
Carp::carp "\%$char not supported."; |
122
|
1
|
|
|
|
|
71
|
return "-"; |
123
|
|
|
|
|
|
|
} |
124
|
189
|
|
|
|
|
351
|
$cb->($char); |
125
|
31
|
|
|
|
|
119
|
}; |
126
|
|
|
|
|
|
|
|
127
|
31
|
|
|
|
|
115
|
my $fmt = $self->_log_format(); |
128
|
|
|
|
|
|
|
|
129
|
31
|
|
|
|
|
260
|
$fmt =~ s^ |
130
|
|
|
|
|
|
|
(?: |
131
|
|
|
|
|
|
|
\%\{(.+?)\}([a-z])| |
132
|
|
|
|
|
|
|
\%([a-zA-Z]) |
133
|
|
|
|
|
|
|
) |
134
|
192
|
100
|
|
|
|
634
|
^ $1 ? $block_handler->($1, $2) : $char_mapping->($3) ^egx; |
135
|
|
|
|
|
|
|
|
136
|
31
|
|
|
|
|
730
|
return $fmt."\n"; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
2987
|
100
|
|
2987
|
1
|
9683
|
sub core { $_[0]->_should('core') and $_[0]->_log('core', $_[1]) } |
140
|
14
|
100
|
|
14
|
1
|
2319
|
sub debug { $_[0]->_should('debug') and $_[0]->_log('debug', $_[1]) } |
141
|
7
|
100
|
|
7
|
1
|
2380
|
sub info { $_[0]->_should('info') and $_[0]->_log('info', $_[1]) } |
142
|
12
|
100
|
|
12
|
1
|
2472
|
sub warning { $_[0]->_should('warning') and $_[0]->_log('warning', $_[1]) } |
143
|
47
|
50
|
|
47
|
1
|
2707
|
sub error { $_[0]->_should('error') and $_[0]->_log('error', $_[1]) } |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
__END__ |