File Coverage

blib/lib/OpenSearch/Client/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             # OpenSearch::Client is an unofficial client for OpenSearch.
2             # It is derived from Search::Elasticsearch version 7.714
3             # License details from the original work are contained in the
4             # NOTICE file distributed with this work.
5             #
6             #-----------------------------------------------------------------------
7             # OpenSearch::Client
8             #-----------------------------------------------------------------------
9             # Copyright 2026 Mark Dootson
10             #
11             # Licensed under the Apache License, Version 2.0 (the "License");
12             # you may not use this file except in compliance with the License.
13             # You may obtain a copy of the License at
14             #
15             # http://www.apache.org/licenses/LICENSE-2.0
16             #
17             # Unless required by applicable law or agreed to in writing, software
18             # distributed under the License is distributed on an "AS IS" BASIS,
19             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20             # See the License for the specific language governing permissions and
21             # limitations under the License.
22              
23             package OpenSearch::Client::Role::Logger;
24             $OpenSearch::Client::Role::Logger::VERSION = '3.007002';
25 55     55   25628 use Moo::Role;
  55         90  
  55         317  
26              
27 55     55   33760 use URI();
  55         125101  
  55         1117  
28 55     55   255 use Try::Tiny;
  55         92  
  55         3228  
29 55     55   239 use OpenSearch::Client::Util qw(new_error);
  55         79  
  55         353  
30 55     55   11979 use namespace::clean;
  55         90  
  55         266  
31              
32             has 'serializer' => ( is => 'ro', required => 1 );
33             has 'log_as' => ( is => 'ro', default => 'opensearch.event' );
34             has 'trace_as' => ( is => 'ro', default => 'opensearch.trace' );
35             has 'deprecate_as' => ( is => 'ro', default => 'opensearch.deprecation' );
36             has 'log_to' => ( is => 'ro' );
37             has 'trace_to' => ( is => 'ro' );
38             has 'deprecate_to' => ( is => 'ro' );
39              
40             has 'trace_handle' => (
41             is => 'lazy',
42             handles => [qw( trace tracef is_trace)]
43             );
44              
45             has 'log_handle' => (
46             is => 'lazy',
47             handles => [ qw(
48             debug debugf is_debug
49             info infof is_info
50             warning warningf is_warning
51             error errorf is_error
52             critical criticalf is_critical
53             )
54             ]
55             );
56              
57             has 'deprecate_handle' => ( is => 'lazy' );
58              
59             #===================================
60             sub throw_error {
61             #===================================
62 16     16 0 4144 my ( $self, $type, $msg, $vars ) = @_;
63 16         57 my $error = new_error( $type, $msg, $vars );
64 16         349 $self->error($error);
65 16         520 die $error;
66             }
67              
68             #===================================
69             sub throw_critical {
70             #===================================
71 13     13 0 1944 my ( $self, $type, $msg, $vars ) = @_;
72 13         52 my $error = new_error( $type, $msg, $vars );
73 13         210 $self->critical($error);
74 13         350 die $error;
75             }
76              
77             #===================================
78             sub trace_request {
79             #===================================
80 152     152 1 4442 my ( $self, $cxn, $params ) = @_;
81 152 100       4192 return unless $self->is_trace;
82              
83 4         428 my $uri = URI->new( 'http://localhost:9200' . $params->{path} );
84 4         491 my %qs = ( %{ $params->{qs} }, pretty => "true" );
  4         24  
85 4         21 $uri->query_form( [ map { $_, $qs{$_} } sort keys %qs ] );
  8         41  
86              
87             my $body
88             = $params->{serialize} eq 'std'
89             ? $self->serializer->encode_pretty( $params->{body} )
90 4 100       559 : $params->{data};
91              
92 4         5 my $content_type = '';
93 4 100       9 if ( defined $body ) {
94 3         90 $body =~ s/'/\\u0027/g;
95 3         7 $body = " -d '\n$body'\n";
96 3         6 $content_type = '-H "Content-type: ' . $params->{mime_type} . '" ';
97             }
98 1         2 else { $body = "\n" }
99              
100             my $msg = sprintf(
101             "# Request to: %s\n" #
102             . "curl %s-X%s '%s'%s", #
103             $cxn->stringify,
104             $content_type,
105             $params->{method},
106 4         17 $uri,
107             $body
108             );
109              
110 4         130 $self->trace($msg);
111             }
112              
113             #===================================
114             sub trace_response {
115             #===================================
116 116     116 1 1515 my ( $self, $cxn, $code, $response, $took ) = @_;
117 116 100       1498 return unless $self->is_trace;
118              
119 2   100     191 my $body = $self->serializer->encode_pretty($response) || "\n";
120 2         11 $body =~ s/^/# /mg;
121              
122 2         10 my $msg = sprintf(
123             "# Response: %s, Took: %d ms\n%s", #
124             $code, $took * 1000, $body
125             );
126              
127 2         35 $self->trace($msg);
128             }
129              
130             #===================================
131             sub trace_error {
132             #===================================
133 29     29 1 57 my ( $self, $cxn, $error ) = @_;
134 29 100       505 return unless $self->is_trace;
135              
136             my $body
137 2   100     262 = $self->serializer->encode_pretty( $error->{vars}{body} || "\n" );
138 2         12 $body =~ s/^/# /mg;
139              
140             my $msg
141 2         6 = sprintf( "# ERROR: %s %s\n%s", ref($error), $error->{text}, $body );
142              
143 2         38 $self->trace($msg);
144             }
145              
146             #===================================
147             sub trace_comment {
148             #===================================
149 1     1 1 250 my ( $self, $comment ) = @_;
150 1 50       30 return unless $self->is_trace;
151 1         196 $comment =~ s/^/# *** /mg;
152 1         2 chomp $comment;
153 1         16 $self->trace("$comment\n");
154             }
155              
156             #===================================
157             sub deprecation {
158             #===================================
159 1     1 1 2 my $self = shift;
160              
161 1         27 $self->deprecate_handle->warnf( "[DEPRECATION] %s - In request: %s", @_ );
162             }
163             1;
164              
165             __END__