File Coverage

blib/lib/Search/Elasticsearch/Role/Logger.pm
Criterion Covered Total %
statement 58 58 100.0
branch 11 12 91.6
condition 4 4 100.0
subroutine 12 12 100.0
pod 5 7 71.4
total 90 93 96.7


line stmt bran cond sub pod time code
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             $Search::Elasticsearch::Role::Logger::VERSION = '8.12';
20 55     55   36743 use Moo::Role;
  55         157  
  55         463  
21              
22 55     55   50308 use URI();
  55         175643  
  55         1610  
23 55     55   363 use Try::Tiny;
  55         114  
  55         4792  
24 55     55   370 use Search::Elasticsearch::Util qw(new_error);
  55         121  
  55         583  
25 55     55   20580 use namespace::clean;
  55         132  
  55         462  
26              
27             has 'serializer' => ( is => 'ro', required => 1 );
28             has 'log_as' => ( is => 'ro', default => 'elasticsearch.event' );
29             has 'trace_as' => ( is => 'ro', default => 'elasticsearch.trace' );
30             has 'deprecate_as' => ( is => 'ro', default => 'elasticsearch.deprecation' );
31             has 'log_to' => ( is => 'ro' );
32             has 'trace_to' => ( is => 'ro' );
33             has 'deprecate_to' => ( is => 'ro' );
34              
35             has 'trace_handle' => (
36             is => 'lazy',
37             handles => [qw( trace tracef is_trace)]
38             );
39              
40             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             has 'deprecate_handle' => ( is => 'lazy' );
53              
54             #===================================
55             sub throw_error {
56             #===================================
57 16     16 0 4272 my ( $self, $type, $msg, $vars ) = @_;
58 16         71 my $error = new_error( $type, $msg, $vars );
59 16         397 $self->error($error);
60 16         744 die $error;
61             }
62              
63             #===================================
64             sub throw_critical {
65             #===================================
66 13     13 0 2012 my ( $self, $type, $msg, $vars ) = @_;
67 13         44 my $error = new_error( $type, $msg, $vars );
68 13         260 $self->critical($error);
69 13         394 die $error;
70             }
71              
72             #===================================
73             sub trace_request {
74             #===================================
75 152     152 1 3318 my ( $self, $cxn, $params ) = @_;
76 152 100       4368 return unless $self->is_trace;
77              
78 4         268 my $uri = URI->new( 'http://localhost:9200' . $params->{path} );
79 4         304 my %qs = ( %{ $params->{qs} }, pretty => "true" );
  4         16  
80 4         12 $uri->query_form( [ map { $_, $qs{$_} } sort keys %qs ] );
  8         31  
81              
82             my $body
83             = $params->{serialize} eq 'std'
84             ? $self->serializer->encode_pretty( $params->{body} )
85 4 100       475 : $params->{data};
86              
87 4         7 my $content_type = '';
88 4 100       6 if ( defined $body ) {
89 3         9 $body =~ s/'/\\u0027/g;
90 3         7 $body = " -d '\n$body'\n";
91 3         5 $content_type = '-H "Content-type: ' . $params->{mime_type} . '" ';
92             }
93 1         2 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         18 $uri,
102             $body
103             );
104              
105 4         119 $self->trace($msg);
106             }
107              
108             #===================================
109             sub trace_response {
110             #===================================
111 116     116 1 8158 my ( $self, $cxn, $code, $response, $took ) = @_;
112 116 100       2912 return unless $self->is_trace;
113              
114 2   100     356 my $body = $self->serializer->encode_pretty($response) || "\n";
115 2         19 $body =~ s/^/# /mg;
116              
117 2         18 my $msg = sprintf(
118             "# Response: %s, Took: %d ms\n%s", #
119             $code, $took * 1000, $body
120             );
121              
122 2         78 $self->trace($msg);
123             }
124              
125             #===================================
126             sub trace_error {
127             #===================================
128 29     29 1 73 my ( $self, $cxn, $error ) = @_;
129 29 100       735 return unless $self->is_trace;
130              
131             my $body
132 2   100     414 = $self->serializer->encode_pretty( $error->{vars}{body} || "\n" );
133 2         16 $body =~ s/^/# /mg;
134              
135             my $msg
136 2         10 = sprintf( "# ERROR: %s %s\n%s", ref($error), $error->{text}, $body );
137              
138 2         71 $self->trace($msg);
139             }
140              
141             #===================================
142             sub trace_comment {
143             #===================================
144 1     1 1 247 my ( $self, $comment ) = @_;
145 1 50       27 return unless $self->is_trace;
146 1         163 $comment =~ s/^/# *** /mg;
147 1         3 chomp $comment;
148 1         16 $self->trace("$comment\n");
149             }
150              
151             #===================================
152             sub deprecation {
153             #===================================
154 1     1 1 3 my $self = shift;
155              
156 1         42 $self->deprecate_handle->warnf( "[DEPRECATION] %s - In request: %s", @_ );
157             }
158             1;
159              
160             # ABSTRACT: Provides common functionality to Logger implementations
161              
162             __END__