Elasticsearch/Role/Logger.pm

filter: Statementstmt 100% 70 / 70 Branchbran 91.6% 11 / 12 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 71.4% 5 / 7 totaltotal 97.2% 104 / 107 CC 9 SCAR 8.3
CC
9
Cov
99%
trace_request
4.0
trace_response
3.0
trace_error
3.0
CRAP 2.3
« Elasticsearch/Role/Serializer/JSON.pm ↑ Summary Elasticsearch/Client/8_0.pm »
1 # Licensed to Elasticsearch B.V. under one or more contributor
2 # license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright
4 # ownership. Elasticsearch B.V. licenses this file to you under
5 # the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17  
18 package Search::Elasticsearch::Role::Logger;
19 55 $Search::Elasticsearch::Role::Logger::VERSION = '8.12';
20 55 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 55 ▶use URI();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 55 ▶use Search::Elasticsearch::Util qw(new_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 55 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 55 has 'serializer' => ( is => 'ro', required => 1 );
28 55 has 'log_as' => ( is => 'ro', default => 'elasticsearch.event' );
29 55 has 'trace_as' => ( is => 'ro', default => 'elasticsearch.trace' );
30 55 has 'deprecate_as' => ( is => 'ro', default => 'elasticsearch.deprecation' );
31 55 has 'log_to' => ( is => 'ro' );
32 55 has 'trace_to' => ( is => 'ro' );
33 55 has 'deprecate_to' => ( is => 'ro' );
34  
35 55 has 'trace_handle' => (
36 is => 'lazy',
37 handles => [qw( trace tracef is_trace)]
38 );
39  
40 55 has 'log_handle' => (
41 is => 'lazy',
42 handles => [ qw(
43 debug debugf is_debug
44 info infof is_info
45 warning warningf is_warning
46 error errorf is_error
47 critical criticalf is_critical
48 )
49 ]
50 );
51  
52 55 has 'deprecate_handle' => ( is => 'lazy' );
53  
54 #===================================
55 16 ▶sub throw_error {
Subroutine throw_error: called CC=1 SCAR=0.0
Pod undocumented
56 #===================================
57 16 my ( $self, $type, $msg, $vars ) = @_;
58 16 my $error = new_error( $type, $msg, $vars );
59 16 $self->error($error);
60 16 die $error;
61 }
62  
63 #===================================
64 13 ▶sub throw_critical {
Subroutine throw_critical: called CC=1 SCAR=0.0
Pod undocumented
65 #===================================
66 13 my ( $self, $type, $msg, $vars ) = @_;
67 13 my $error = new_error( $type, $msg, $vars );
68 13 $self->critical($error);
69 13 die $error;
70 }
71  
72 #===================================
73 152 ▶sub trace_request {
Subroutine trace_request: called CC=4 SCAR=13.9
Pod documented
74 #===================================
75 152 my ( $self, $cxn, $params ) = @_;
76 152 ▶ return unless $self->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_trace 148 4 152
77  
78 4 my $uri = URI->new( 'http://localhost:9200' . $params->{path} );
79 4 my %qs = ( %{ $params->{qs} }, pretty => "true" );
80 4 $uri->query_form( [ map { $_, $qs{$_} } sort keys %qs ] );
81  
82 my $body
83 = $params->{serialize} eq 'std'
84 ? $self->serializer->encode_pretty( $params->{body} )
85 4 ▶ : $params->{data};
Branch: 1 branch
BranchTrueFalseTotal
$params->{'serialize'} eq 'std' ? : 3 1 4
86  
87 4 my $content_type = '';
88 4 ▶ if ( defined $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) { } 3 1 4
89 3 $body =~ s/'/\\u0027/g;
90 3 $body = " -d '\n$body'\n";
91 3 $content_type = '-H "Content-type: ' . $params->{mime_type} . '" ';
92 }
93 1 else { $body = "\n" }
94  
95 my $msg = sprintf(
96 "# Request to: %s\n" #
97 . "curl %s-X%s '%s'%s", #
98 $cxn->stringify,
99 $content_type,
100 $params->{method},
101 4 $uri,
102 $body
103 );
104  
105 4 $self->trace($msg);
106 }
107  
108 #===================================
109 116 ▶sub trace_response {
Subroutine trace_response: called CC=3 SCAR=11.0
Pod documented
110 #===================================
111 116 my ( $self, $cxn, $code, $response, $took ) = @_;
112 116 ▶ return unless $self->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_trace 114 2 116
113  
114 2 ▶ my $body = $self->serializer->encode_pretty($response) || "\n";
Condition100%2/2
$self->serializer->encode_pretty($response) || "\n"
l!l
11
MC/DC100%1/1
$self->serializer->encode_pretty($response) || "\n"
$self->serializer->encode_pretty($response)
Truth table100%2/2
A
A= $self->serializer->encode_pretty($response)
Aresult
11
00
115 2 $body =~ s/^/# /mg;
116  
117 2 my $msg = sprintf(
118 "# Response: %s, Took: %d ms\n%s", #
119 $code, $took * 1000, $body
120 );
121  
122 2 $self->trace($msg);
123 }
124  
125 #===================================
126 29 ▶sub trace_error {
Subroutine trace_error: called CC=3 SCAR=11.0
Pod documented
127 #===================================
128 29 my ( $self, $cxn, $error ) = @_;
129 29 ▶ return unless $self->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_trace 27 2 29
130  
131 my $body
132 2 ▶ = $self->serializer->encode_pretty( $error->{vars}{body} || "\n" );
Condition100%2/2
$error->{'vars'}{'body'} || "\n"
l!l
11
MC/DC100%1/1
$error->{'vars'}{'body'} || "\n"
$error->{'vars'}{'body'}
Truth table100%2/2
A
A= $error->{'vars'}{'body'}
Aresult
11
00
133 2 $body =~ s/^/# /mg;
134  
135 my $msg
136 2 = sprintf( "# ERROR: %s %s\n%s", ref($error), $error->{text}, $body );
137  
138 2 $self->trace($msg);
139 }
140  
141 #===================================
142 1 ▶sub trace_comment {
Subroutine trace_comment: called CC=2 SCAR=7.0
Pod documented
143 #===================================
144 1 my ( $self, $comment ) = @_;
145 1 ▶ return unless $self->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_trace 0 1 1
146 1 $comment =~ s/^/# *** /mg;
147 1 chomp $comment;
148 1 $self->trace("$comment\n");
149 }
150  
151 #===================================
152 1 ▶sub deprecation {
Subroutine deprecation: called CC=1 SCAR=0.0
Pod documented
153 #===================================
154 1 my $self = shift;
155  
156 1 $self->deprecate_handle->warnf( "[DEPRECATION] %s - In request: %s", @_ );
157 }
158 55 1;
159  
160 # ABSTRACT: Provides common functionality to Logger implementations
161  
162 __END__
« Elasticsearch/Role/Serializer/JSON.pm ↑ Summary Elasticsearch/Client/8_0.pm »