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.00';
20 55     55   26856 use Moo::Role;
  55         124  
  55         310  
21              
22 55     55   29952 use URI();
  55         111733  
  55         1244  
23 55     55   308 use Try::Tiny;
  55         131  
  55         3151  
24 55     55   344 use Search::Elasticsearch::Util qw(new_error);
  55         116  
  55         356  
25 55     55   13434 use namespace::clean;
  55         116  
  55         342  
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 4329 my ( $self, $type, $msg, $vars ) = @_;
58 16         71 my $error = new_error( $type, $msg, $vars );
59 16         306 $self->error($error);
60 16         530 die $error;
61             }
62              
63             #===================================
64             sub throw_critical {
65             #===================================
66 13     13 0 2007 my ( $self, $type, $msg, $vars ) = @_;
67 13         57 my $error = new_error( $type, $msg, $vars );
68 13         242 $self->critical($error);
69 13         504 die $error;
70             }
71              
72             #===================================
73             sub trace_request {
74             #===================================
75 152     152 1 3555 my ( $self, $cxn, $params ) = @_;
76 152 100       2786 return unless $self->is_trace;
77              
78 4         302 my $uri = URI->new( 'http://localhost:9200' . $params->{path} );
79 4         295 my %qs = ( %{ $params->{qs} }, pretty => "true" );
  4         17  
80 4         15 $uri->query_form( [ map { $_, $qs{$_} } sort keys %qs ] );
  8         32  
81              
82             my $body
83             = $params->{serialize} eq 'std'
84             ? $self->serializer->encode_pretty( $params->{body} )
85 4 100       367 : $params->{data};
86              
87 4         8 my $content_type = '';
88 4 100       8 if ( defined $body ) {
89 3         9 $body =~ s/'/\\u0027/g;
90 3         8 $body = " -d '\n$body'\n";
91 3         6 $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         15 $uri,
102             $body
103             );
104              
105 4         115 $self->trace($msg);
106             }
107              
108             #===================================
109             sub trace_response {
110             #===================================
111 116     116 1 1967 my ( $self, $cxn, $code, $response, $took ) = @_;
112 116 100       1952 return unless $self->is_trace;
113              
114 2   100     249 my $body = $self->serializer->encode_pretty($response) || "\n";
115 2         14 $body =~ s/^/# /mg;
116              
117 2         15 my $msg = sprintf(
118             "# Response: %s, Took: %d ms\n%s", #
119             $code, $took * 1000, $body
120             );
121              
122 2         50 $self->trace($msg);
123             }
124              
125             #===================================
126             sub trace_error {
127             #===================================
128 29     29 1 79 my ( $self, $cxn, $error ) = @_;
129 29 100       564 return unless $self->is_trace;
130              
131             my $body
132 2   100     328 = $self->serializer->encode_pretty( $error->{vars}{body} || "\n" );
133 2         13 $body =~ s/^/# /mg;
134              
135             my $msg
136 2         11 = sprintf( "# ERROR: %s %s\n%s", ref($error), $error->{text}, $body );
137              
138 2         40 $self->trace($msg);
139             }
140              
141             #===================================
142             sub trace_comment {
143             #===================================
144 1     1 1 326 my ( $self, $comment ) = @_;
145 1 50       29 return unless $self->is_trace;
146 1         213 $comment =~ s/^/# *** /mg;
147 1         4 chomp $comment;
148 1         22 $self->trace("$comment\n");
149             }
150              
151             #===================================
152             sub deprecation {
153             #===================================
154 1     1 1 3 my $self = shift;
155              
156 1         28 $self->deprecate_handle->warnf( "[DEPRECATION] %s - In request: %s", @_ );
157             }
158             1;
159              
160             # ABSTRACT: Provides common functionality to Logger implementations
161              
162             __END__