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.3514_04'; # TRIAL |
5
|
|
|
|
|
|
|
$Dancer::Logger::Abstract::VERSION = '1.351404'; |
6
|
100
|
|
|
100
|
|
1653
|
use strict; |
|
100
|
|
|
|
|
237
|
|
|
100
|
|
|
|
|
2401
|
|
7
|
100
|
|
|
100
|
|
446
|
use warnings; |
|
100
|
|
|
|
|
183
|
|
|
100
|
|
|
|
|
1925
|
|
8
|
100
|
|
|
100
|
|
480
|
use Carp; |
|
100
|
|
|
|
|
190
|
|
|
100
|
|
|
|
|
5273
|
|
9
|
100
|
|
|
100
|
|
604
|
use base 'Dancer::Engine'; |
|
100
|
|
|
|
|
224
|
|
|
100
|
|
|
|
|
11035
|
|
10
|
|
|
|
|
|
|
|
11
|
100
|
|
|
100
|
|
2938
|
use Dancer::SharedData; |
|
100
|
|
|
|
|
223
|
|
|
100
|
|
|
|
|
2412
|
|
12
|
100
|
|
|
100
|
|
499
|
use Dancer::Timer; |
|
100
|
|
|
|
|
232
|
|
|
100
|
|
|
|
|
2464
|
|
13
|
100
|
|
|
100
|
|
522
|
use Dancer::Config 'setting'; |
|
100
|
|
|
|
|
236
|
|
|
100
|
|
|
|
|
4562
|
|
14
|
100
|
|
|
100
|
|
37963
|
use POSIX qw/strftime/; |
|
100
|
|
|
|
|
499224
|
|
|
100
|
|
|
|
|
636
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# This is the only method to implement by logger engines. |
17
|
|
|
|
|
|
|
# It receives the following arguments: |
18
|
|
|
|
|
|
|
# $msg_level, $msg_content, it gets called only if the configuration allows |
19
|
|
|
|
|
|
|
# a message of the given level to be logged. |
20
|
16
|
|
|
16
|
|
2507
|
sub _log { confess "_log not implemented" } |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $levels = { |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# levels < 0 are for core only |
25
|
|
|
|
|
|
|
core => -10, |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# levels > 0 are for end-users only |
28
|
|
|
|
|
|
|
debug => 1, |
29
|
|
|
|
|
|
|
info => 2, |
30
|
|
|
|
|
|
|
warn => 3, |
31
|
|
|
|
|
|
|
warning => 3, |
32
|
|
|
|
|
|
|
error => 4, |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $log_formats = { |
36
|
|
|
|
|
|
|
simple => '[%P] %L @%D> %i%m in %f l. %l', |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _log_format { |
40
|
34
|
|
|
34
|
|
77
|
my $config = setting('logger_format'); |
41
|
|
|
|
|
|
|
|
42
|
34
|
100
|
|
|
|
84
|
if ( !$config ) { |
43
|
27
|
|
|
|
|
66
|
return $log_formats->{simple}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
exists $log_formats->{$config} |
47
|
7
|
100
|
|
|
|
18
|
? return $log_formats->{$config} |
48
|
|
|
|
|
|
|
: return $config; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _should { |
52
|
3054
|
|
|
3054
|
|
4379
|
my ($self, $msg_level) = @_; |
53
|
3054
|
|
100
|
|
|
5396
|
my $conf_level = setting('log') || 'debug'; |
54
|
|
|
|
|
|
|
|
55
|
3054
|
100
|
|
|
|
6022
|
if (!exists $levels->{$conf_level}) { |
56
|
1
|
|
|
|
|
4
|
setting('log' => 'debug'); |
57
|
1
|
|
|
|
|
2
|
$conf_level = 'debug'; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
3054
|
|
|
|
|
8346
|
return $levels->{$conf_level} <= $levels->{$msg_level}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub format_message { |
64
|
31
|
|
|
31
|
1
|
4549
|
my ($self, $level, $message) = @_; |
65
|
31
|
|
|
|
|
65
|
chomp $message; |
66
|
|
|
|
|
|
|
|
67
|
31
|
100
|
|
|
|
110
|
$message = Encode::encode(setting('charset'), $message) |
68
|
|
|
|
|
|
|
if setting('charset'); |
69
|
|
|
|
|
|
|
|
70
|
31
|
100
|
|
|
|
196
|
$level = 'warn' if $level eq 'warning'; |
71
|
31
|
|
|
|
|
125
|
$level = sprintf('%5s', $level); |
72
|
|
|
|
|
|
|
|
73
|
31
|
|
|
|
|
113
|
my $r = Dancer::SharedData->request; |
74
|
31
|
|
|
|
|
124
|
my @stack = caller(3); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $block_handler = sub { |
77
|
2
|
|
|
2
|
|
6
|
my ( $block, $type ) = @_; |
78
|
2
|
100
|
|
|
|
7
|
if ( $type eq 't' ) { |
|
|
50
|
|
|
|
|
|
79
|
1
|
|
|
|
|
78
|
return "[" . strftime( $block, localtime ) . "]"; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
elsif ( $type eq 'h' ) { |
82
|
1
|
50
|
|
|
|
2
|
return '-' unless defined $r; |
83
|
1
|
|
50
|
|
|
4
|
return scalar $r->header($block) || '-'; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
0
|
|
|
|
|
0
|
Carp::carp("{$block}$type not supported"); |
87
|
0
|
|
|
|
|
0
|
return "-"; |
88
|
|
|
|
|
|
|
} |
89
|
31
|
|
|
|
|
134
|
}; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $chars_mapping = { |
92
|
|
|
|
|
|
|
h => sub { |
93
|
|
|
|
|
|
|
defined $r |
94
|
1
|
50
|
0
|
1
|
|
5
|
? $r->env->{'HTTP_X_REAL_IP'} || $r->env->{'REMOTE_ADDR'} || '-' |
95
|
|
|
|
|
|
|
: '-'; |
96
|
|
|
|
|
|
|
}, |
97
|
1
|
|
50
|
1
|
|
2
|
t => sub { Encode::decode(setting('charset') || 'utf8', |
98
|
|
|
|
|
|
|
POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime )) }, |
99
|
1
|
|
|
1
|
|
37
|
T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime ) }, |
100
|
0
|
|
0
|
0
|
|
0
|
u => sub { Encode::decode(setting('charset') || 'utf8', |
101
|
|
|
|
|
|
|
POSIX::strftime( "%d/%b/%Y %H:%M:%S", gmtime )) }, |
102
|
0
|
|
|
0
|
|
0
|
U => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", gmtime ) }, |
103
|
26
|
|
|
26
|
|
123
|
P => sub { $$ }, |
104
|
27
|
|
|
27
|
|
89
|
L => sub { $level }, |
105
|
|
|
|
|
|
|
D => sub { |
106
|
26
|
|
|
26
|
|
76
|
my $time = Dancer::SharedData->timer->tick; |
107
|
26
|
|
|
|
|
122
|
return $time; |
108
|
|
|
|
|
|
|
}, |
109
|
29
|
|
|
29
|
|
107
|
m => sub { $message }, |
110
|
26
|
100
|
|
26
|
|
153
|
f => sub { $stack[1] || '-' }, |
111
|
26
|
100
|
|
26
|
|
98
|
l => sub { $stack[2] || '-' }, |
112
|
|
|
|
|
|
|
i => sub { |
113
|
26
|
100
|
|
26
|
|
142
|
defined $r ? "[hit #" . $r->id . "]" : ""; |
114
|
|
|
|
|
|
|
}, |
115
|
31
|
|
|
|
|
528
|
}; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my $char_mapping = sub { |
118
|
190
|
|
|
190
|
|
284
|
my $char = shift; |
119
|
|
|
|
|
|
|
|
120
|
190
|
|
|
|
|
244
|
my $cb = $chars_mapping->{$char}; |
121
|
190
|
100
|
|
|
|
286
|
unless ($cb) { |
122
|
1
|
|
|
|
|
228
|
Carp::carp "\%$char not supported."; |
123
|
1
|
|
|
|
|
68
|
return "-"; |
124
|
|
|
|
|
|
|
} |
125
|
189
|
|
|
|
|
290
|
$cb->($char); |
126
|
31
|
|
|
|
|
101
|
}; |
127
|
|
|
|
|
|
|
|
128
|
31
|
|
|
|
|
102
|
my $fmt = $self->_log_format(); |
129
|
|
|
|
|
|
|
|
130
|
31
|
|
|
|
|
236
|
$fmt =~ s^ |
131
|
|
|
|
|
|
|
(?: |
132
|
|
|
|
|
|
|
\%\{(.+?)\}([a-z])| |
133
|
|
|
|
|
|
|
\%([a-zA-Z]) |
134
|
|
|
|
|
|
|
) |
135
|
192
|
100
|
|
|
|
509
|
^ $1 ? $block_handler->($1, $2) : $char_mapping->($3) ^egx; |
136
|
|
|
|
|
|
|
|
137
|
31
|
|
|
|
|
614
|
return $fmt."\n"; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
2974
|
100
|
|
2974
|
1
|
8537
|
sub core { $_[0]->_should('core') and $_[0]->_log('core', $_[1]) } |
141
|
14
|
100
|
|
14
|
1
|
2252
|
sub debug { $_[0]->_should('debug') and $_[0]->_log('debug', $_[1]) } |
142
|
7
|
100
|
|
7
|
1
|
2602
|
sub info { $_[0]->_should('info') and $_[0]->_log('info', $_[1]) } |
143
|
12
|
100
|
|
12
|
1
|
2414
|
sub warning { $_[0]->_should('warning') and $_[0]->_log('warning', $_[1]) } |
144
|
47
|
50
|
|
47
|
1
|
2592
|
sub error { $_[0]->_should('error') and $_[0]->_log('error', $_[1]) } |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
1; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
__END__ |