line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Elasticsearch::Role::Logger; |
2
|
|
|
|
|
|
|
$Elasticsearch::Role::Logger::VERSION = '1.05'; |
3
|
42
|
|
|
42
|
|
51814
|
use Moo::Role; |
|
42
|
|
|
|
|
104
|
|
|
42
|
|
|
|
|
374
|
|
4
|
|
|
|
|
|
|
|
5
|
42
|
|
|
42
|
|
59167
|
use URI(); |
|
42
|
|
|
|
|
89469
|
|
|
42
|
|
|
|
|
1225
|
|
6
|
42
|
|
|
42
|
|
318
|
use Try::Tiny; |
|
42
|
|
|
|
|
112
|
|
|
42
|
|
|
|
|
3745
|
|
7
|
42
|
|
|
42
|
|
331
|
use Elasticsearch::Util qw(new_error); |
|
42
|
|
|
|
|
90
|
|
|
42
|
|
|
|
|
436
|
|
8
|
42
|
|
|
42
|
|
14141
|
use namespace::clean; |
|
42
|
|
|
|
|
101
|
|
|
42
|
|
|
|
|
379
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has 'serializer' => ( is => 'ro', required => 1 ); |
11
|
|
|
|
|
|
|
has 'log_as' => ( is => 'ro', default => 'elasticsearch.event' ); |
12
|
|
|
|
|
|
|
has 'trace_as' => ( is => 'ro', default => 'elasticsearch.trace' ); |
13
|
|
|
|
|
|
|
has 'log_to' => ( is => 'ro' ); |
14
|
|
|
|
|
|
|
has 'trace_to' => ( is => 'ro' ); |
15
|
|
|
|
|
|
|
has 'trace_handle' => ( |
16
|
|
|
|
|
|
|
is => 'lazy', |
17
|
|
|
|
|
|
|
handles => [qw( trace tracef is_trace)] |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'log_handle' => ( |
21
|
|
|
|
|
|
|
is => 'lazy', |
22
|
|
|
|
|
|
|
handles => [ qw( |
23
|
|
|
|
|
|
|
debug debugf is_debug |
24
|
|
|
|
|
|
|
info infof is_info |
25
|
|
|
|
|
|
|
warning warningf is_warning |
26
|
|
|
|
|
|
|
error errorf is_error |
27
|
|
|
|
|
|
|
critical criticalf is_critical |
28
|
|
|
|
|
|
|
) |
29
|
|
|
|
|
|
|
] |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
#=================================== |
33
|
|
|
|
|
|
|
sub throw_error { |
34
|
|
|
|
|
|
|
#=================================== |
35
|
16
|
|
|
16
|
0
|
6885
|
my ( $self, $type, $msg, $vars ) = @_; |
36
|
16
|
|
|
|
|
87
|
my $error = new_error( $type, $msg, $vars ); |
37
|
16
|
|
|
|
|
112
|
$self->error($error); |
38
|
16
|
|
|
|
|
12083
|
die $error; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
#=================================== |
42
|
|
|
|
|
|
|
sub throw_critical { |
43
|
|
|
|
|
|
|
#=================================== |
44
|
14
|
|
|
14
|
0
|
1051
|
my ( $self, $type, $msg, $vars ) = @_; |
45
|
14
|
|
|
|
|
70
|
my $error = new_error( $type, $msg, $vars ); |
46
|
14
|
|
|
|
|
73
|
$self->critical($error); |
47
|
14
|
|
|
|
|
3623
|
die $error; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#=================================== |
51
|
|
|
|
|
|
|
sub trace_request { |
52
|
|
|
|
|
|
|
#=================================== |
53
|
152
|
|
|
152
|
1
|
3900
|
my ( $self, $cxn, $params ) = @_; |
54
|
152
|
100
|
|
|
|
537
|
return unless $self->is_trace; |
55
|
|
|
|
|
|
|
|
56
|
4
|
|
|
|
|
305
|
my $uri = URI->new( 'http://localhost:9200' . $params->{path} ); |
57
|
4
|
|
|
|
|
307
|
my %qs = ( %{ $params->{qs} }, pretty => 1 ); |
|
4
|
|
|
|
|
21
|
|
58
|
4
|
|
|
|
|
16
|
$uri->query_form( [ map { $_, $qs{$_} } sort keys %qs ] ); |
|
8
|
|
|
|
|
40
|
|
59
|
|
|
|
|
|
|
|
60
|
4
|
100
|
|
|
|
477
|
my $body |
61
|
|
|
|
|
|
|
= $params->{serialize} eq 'std' |
62
|
|
|
|
|
|
|
? $self->serializer->encode_pretty( $params->{body} ) |
63
|
|
|
|
|
|
|
: $params->{data}; |
64
|
|
|
|
|
|
|
|
65
|
4
|
100
|
|
|
|
12
|
if ( defined $body ) { |
66
|
3
|
|
|
|
|
9
|
$body =~ s/'/\\u0027/g; |
67
|
3
|
|
|
|
|
9
|
$body = " -d '\n$body'\n"; |
68
|
|
|
|
|
|
|
} |
69
|
1
|
|
|
|
|
2
|
else { $body = "\n" } |
70
|
|
|
|
|
|
|
|
71
|
4
|
|
|
|
|
18
|
my $msg = sprintf( |
72
|
|
|
|
|
|
|
"# Request to: %s\n" # |
73
|
|
|
|
|
|
|
. "curl -X%s '%s'%s", # |
74
|
|
|
|
|
|
|
$cxn->stringify, |
75
|
|
|
|
|
|
|
$params->{method}, |
76
|
|
|
|
|
|
|
$uri, |
77
|
|
|
|
|
|
|
$body |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
|
80
|
4
|
|
|
|
|
53
|
$self->trace($msg); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
#=================================== |
84
|
|
|
|
|
|
|
sub trace_response { |
85
|
|
|
|
|
|
|
#=================================== |
86
|
116
|
|
|
116
|
1
|
2082
|
my ( $self, $cxn, $code, $response, $took ) = @_; |
87
|
116
|
100
|
|
|
|
438
|
return unless $self->is_trace; |
88
|
|
|
|
|
|
|
|
89
|
2
|
|
100
|
|
|
198
|
my $body = $self->serializer->encode_pretty($response) || "\n"; |
90
|
2
|
|
|
|
|
16
|
$body =~ s/^/# /mg; |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
16
|
my $msg = sprintf( |
93
|
|
|
|
|
|
|
"# Response: %s, Took: %d ms\n%s", # |
94
|
|
|
|
|
|
|
$code, $took * 1000, $body |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
|
97
|
2
|
|
|
|
|
11
|
$self->trace($msg); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
#=================================== |
101
|
|
|
|
|
|
|
sub trace_error { |
102
|
|
|
|
|
|
|
#=================================== |
103
|
30
|
|
|
30
|
1
|
86
|
my ( $self, $cxn, $error ) = @_; |
104
|
30
|
100
|
|
|
|
138
|
return unless $self->is_trace; |
105
|
|
|
|
|
|
|
|
106
|
2
|
|
100
|
|
|
391
|
my $body |
107
|
|
|
|
|
|
|
= $self->serializer->encode_pretty( $error->{vars}{body} || "\n" ); |
108
|
2
|
|
|
|
|
12
|
$body =~ s/^/# /mg; |
109
|
|
|
|
|
|
|
|
110
|
2
|
|
|
|
|
13
|
my $msg |
111
|
|
|
|
|
|
|
= sprintf( "# ERROR: %s %s\n%s", ref($error), $error->{text}, $body ); |
112
|
|
|
|
|
|
|
|
113
|
2
|
|
|
|
|
10
|
$self->trace($msg); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
#=================================== |
117
|
|
|
|
|
|
|
sub trace_comment { |
118
|
|
|
|
|
|
|
#=================================== |
119
|
1
|
|
|
1
|
1
|
445
|
my ( $self, $comment ) = @_; |
120
|
1
|
50
|
|
|
|
8
|
return unless $self->is_trace; |
121
|
1
|
|
|
|
|
141
|
$comment =~ s/^/# *** /mg; |
122
|
1
|
|
|
|
|
3
|
chomp $comment; |
123
|
1
|
|
|
|
|
7
|
$self->trace("$comment\n"); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# ABSTRACT: Provides common functionality to Logger implementations |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
__END__ |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=pod |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=encoding UTF-8 |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 NAME |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Elasticsearch::Role::Logger - Provides common functionality to Logger implementations |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 VERSION |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
version 1.05 |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 DESCRIPTION |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
This role provides common functionality to Logger implementations, to enable |
147
|
|
|
|
|
|
|
the logging of events and the tracing of request-response conversations |
148
|
|
|
|
|
|
|
with Elasticsearch nodes. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
See L<Elasticsearch::Logger::LogAny> for the default implementation. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 CONFIGURATION |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 C<log_to> |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Parameters passed to C<log_to> are used by L<Elasticsearch::Role::Logger> |
157
|
|
|
|
|
|
|
implementations to setup the L</log_handle()>. See |
158
|
|
|
|
|
|
|
L<Elasticsearch::Logger::LogAny/log_to> for details. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 C<log_as> |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
By default, events emitted by L</debug()>, L</info()>, L</warning()>, |
163
|
|
|
|
|
|
|
L</error()> and L</critical()> are logged to the L</log_handle()> under the |
164
|
|
|
|
|
|
|
category C<"elasticsearch.event">, which can be configured with C<log_as>. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 C<trace_to> |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Parameters passed to C<trace_to> are used by L<Elasticsearch::Role::Logger> |
169
|
|
|
|
|
|
|
implementations to setup the L</trace_handle()>. See |
170
|
|
|
|
|
|
|
L<Elasticsearch::Logger::LogAny/trace_to> for details. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 C<trace_as> |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
By default, trace output emitted by L</trace_request()>, L</trace_response()>, |
175
|
|
|
|
|
|
|
L</trace_error()> and L</trace_comment()> are logged under the category |
176
|
|
|
|
|
|
|
C<elasticsearch.trace>, which can be configured with C<trace_as>. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 METHODS |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head2 C<log_handle()> |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Returns an object which can handle the methods: |
183
|
|
|
|
|
|
|
C<debug()>, C<debugf()>, C<is_debug()>, C<info()>, C<infof()>, C<is_info()>, |
184
|
|
|
|
|
|
|
C<warning()>, C<warningf()>, C<is_warning()>, C<error()>, C<errorf()>, |
185
|
|
|
|
|
|
|
C<is_error()>, C<critical()>, C<criticalf()> and C<is_critical()>. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head2 C<trace_handle()> |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Returns an object which can handle the methods: |
190
|
|
|
|
|
|
|
C<trace()>, C<tracef()> and C<is_trace()>. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 C<trace_request()> |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
$logger->trace_request($cxn,\%request); |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Accepts a Cxn object and request parameters and logs them if tracing is |
197
|
|
|
|
|
|
|
enabled. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 C<trace_response()> |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
$logger->trace_response($cxn,$code,$response,$took); |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Logs a successful HTTP response, where C<$code> is the HTTP status code, |
204
|
|
|
|
|
|
|
C<$response> is the HTTP body and C<$took> is the time the request |
205
|
|
|
|
|
|
|
took in seconds |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 C<trace_error()> |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
$logger->trace_error($cxn,$error); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Logs a failed HTTP response, where C<$error> is an L<Elasticsearch::Error> |
212
|
|
|
|
|
|
|
object. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head2 C<trace_comment()> |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
$logger->trace_comment($comment); |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Used to insert debugging comments into trace output. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head1 AUTHOR |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Clinton Gormley <drtech@cpan.org> |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Elasticsearch BV. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
This is free software, licensed under: |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
The Apache License, Version 2.0, January 2004 |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=cut |