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/6_0.pm »
1 package Search::Elasticsearch::Role::Logger;
2 55 $Search::Elasticsearch::Role::Logger::VERSION = '6.80';
3 55 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 55 ▶use URI();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use Search::Elasticsearch::Util qw(new_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 55 has 'serializer' => ( is => 'ro', required => 1 );
11 55 has 'log_as' => ( is => 'ro', default => 'elasticsearch.event' );
12 55 has 'trace_as' => ( is => 'ro', default => 'elasticsearch.trace' );
13 55 has 'deprecate_as' => ( is => 'ro', default => 'elasticsearch.deprecation' );
14 55 has 'log_to' => ( is => 'ro' );
15 55 has 'trace_to' => ( is => 'ro' );
16 55 has 'deprecate_to' => ( is => 'ro' );
17  
18 55 has 'trace_handle' => (
19 is => 'lazy',
20 handles => [qw( trace tracef is_trace)]
21 );
22  
23 55 has 'log_handle' => (
24 is => 'lazy',
25 handles => [ qw(
26 debug debugf is_debug
27 info infof is_info
28 warning warningf is_warning
29 error errorf is_error
30 critical criticalf is_critical
31 )
32 ]
33 );
34  
35 55 has 'deprecate_handle' => ( is => 'lazy' );
36  
37 #===================================
38 sub throw_error {
39 #===================================
40 16 ▶ my ( $self, $type, $msg, $vars ) = @_;
Subroutine throw_error: called CC=1 SCAR=0.0
Pod undocumented
41 16 my $error = new_error( $type, $msg, $vars );
42 16 $self->error($error);
43 16 die $error;
44 }
45  
46 #===================================
47 sub throw_critical {
48 #===================================
49 13 ▶ my ( $self, $type, $msg, $vars ) = @_;
Subroutine throw_critical: called CC=1 SCAR=0.0
Pod undocumented
50 13 my $error = new_error( $type, $msg, $vars );
51 13 $self->critical($error);
52 13 die $error;
53 }
54  
55 #===================================
56 sub trace_request {
57 #===================================
58 152 ▶ my ( $self, $cxn, $params ) = @_;
Subroutine trace_request: called CC=4 SCAR=13.9
Pod documented
59 152 ▶ return unless $self->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_trace 148 4 152
60  
61 4 my $uri = URI->new( 'http://localhost:9200' . $params->{path} );
62 4 my %qs = ( %{ $params->{qs} }, pretty => "true" );
63 4 $uri->query_form( [ map { $_, $qs{$_} } sort keys %qs ] );
64  
65 my $body
66 = $params->{serialize} eq 'std'
67 ? $self->serializer->encode_pretty( $params->{body} )
68 4 ▶ : $params->{data};
Branch: 1 branch
BranchTrueFalseTotal
$params->{'serialize'} eq 'std' ? : 3 1 4
69  
70 4 my $content_type = '';
71 4 ▶ if ( defined $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) { } 3 1 4
72 3 $body =~ s/'/\\u0027/g;
73 3 $body = " -d '\n$body'\n";
74 3 $content_type = '-H "Content-type: ' . $params->{mime_type} . '" ';
75 }
76 1 else { $body = "\n" }
77  
78 my $msg = sprintf(
79 "# Request to: %s\n" #
80 . "curl %s-X%s '%s'%s", #
81 $cxn->stringify,
82 $content_type,
83 $params->{method},
84 4 $uri,
85 $body
86 );
87  
88 4 $self->trace($msg);
89 }
90  
91 #===================================
92 sub trace_response {
93 #===================================
94 116 ▶ my ( $self, $cxn, $code, $response, $took ) = @_;
Subroutine trace_response: called CC=3 SCAR=11.0
Pod documented
95 116 ▶ return unless $self->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_trace 114 2 116
96  
97 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
98 2 $body =~ s/^/# /mg;
99  
100 2 my $msg = sprintf(
101 "# Response: %s, Took: %d ms\n%s", #
102 $code, $took * 1000, $body
103 );
104  
105 2 $self->trace($msg);
106 }
107  
108 #===================================
109 sub trace_error {
110 #===================================
111 29 ▶ my ( $self, $cxn, $error ) = @_;
Subroutine trace_error: called CC=3 SCAR=11.0
Pod documented
112 29 ▶ return unless $self->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_trace 27 2 29
113  
114 my $body
115 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
116 2 $body =~ s/^/# /mg;
117  
118 my $msg
119 2 = sprintf( "# ERROR: %s %s\n%s", ref($error), $error->{text}, $body );
120  
121 2 $self->trace($msg);
122 }
123  
124 #===================================
125 sub trace_comment {
126 #===================================
127 1 ▶ my ( $self, $comment ) = @_;
Subroutine trace_comment: called CC=2 SCAR=7.0
Pod documented
128 1 ▶ return unless $self->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_trace 0 1 1
129 1 $comment =~ s/^/# *** /mg;
130 1 chomp $comment;
131 1 $self->trace("$comment\n");
132 }
133  
134 #===================================
135 sub deprecation {
136 #===================================
137 1 ▶ my $self = shift;
Subroutine deprecation: called CC=1 SCAR=0.0
Pod documented
138  
139 1 $self->deprecate_handle->warnf( "[DEPRECATION] %s - In request: %s", @_ );
140 }
141 55 1;
142  
143 # ABSTRACT: Provides common functionality to Logger implementations
144  
145 __END__
« Elasticsearch/Role/Serializer/JSON.pm ↑ Summary Elasticsearch/Client/6_0.pm »