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.007004';
25 55     55   25828 use Moo::Role;
  55         107  
  55         319  
26              
27 55     55   34438 use URI();
  55         130233  
  55         1174  
28 55     55   268 use Try::Tiny;
  55         79  
  55         3374  
29 55     55   243 use OpenSearch::Client::Util qw(new_error);
  55         83  
  55         338  
30 55     55   12627 use namespace::clean;
  55         97  
  55         281  
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 4246 my ( $self, $type, $msg, $vars ) = @_;
63 16         58 my $error = new_error( $type, $msg, $vars );
64 16         267 $self->error($error);
65 16         477 die $error;
66             }
67              
68             #===================================
69             sub throw_critical {
70             #===================================
71 13     13 0 1942 my ( $self, $type, $msg, $vars ) = @_;
72 13         38 my $error = new_error( $type, $msg, $vars );
73 13         233 $self->critical($error);
74 13         344 die $error;
75             }
76              
77             #===================================
78             sub trace_request {
79             #===================================
80 152     152 1 3176 my ( $self, $cxn, $params ) = @_;
81 152 100       2610 return unless $self->is_trace;
82              
83 4         284 my $uri = URI->new( 'http://localhost:9200' . $params->{path} );
84 4         353 my %qs = ( %{ $params->{qs} }, pretty => "true" );
  4         14  
85 4         17 $uri->query_form( [ map { $_, $qs{$_} } sort keys %qs ] );
  8         30  
86              
87             my $body
88             = $params->{serialize} eq 'std'
89             ? $self->serializer->encode_pretty( $params->{body} )
90 4 100       382 : $params->{data};
91              
92 4         7 my $content_type = '';
93 4 100       8 if ( defined $body ) {
94 3         10 $body =~ s/'/\\u0027/g;
95 3         6 $body = " -d '\n$body'\n";
96 3         6 $content_type = '-H "Content-type: ' . $params->{mime_type} . '" ';
97             }
98 1         1 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         16 $uri,
107             $body
108             );
109              
110 4         146 $self->trace($msg);
111             }
112              
113             #===================================
114             sub trace_response {
115             #===================================
116 116     116 1 1443 my ( $self, $cxn, $code, $response, $took ) = @_;
117 116 100       1462 return unless $self->is_trace;
118              
119 2   100     195 my $body = $self->serializer->encode_pretty($response) || "\n";
120 2         11 $body =~ s/^/# /mg;
121              
122 2         11 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 64 my ( $self, $cxn, $error ) = @_;
134 29 100       534 return unless $self->is_trace;
135              
136             my $body
137 2   100     251 = $self->serializer->encode_pretty( $error->{vars}{body} || "\n" );
138 2         10 $body =~ s/^/# /mg;
139              
140             my $msg
141 2         6 = sprintf( "# ERROR: %s %s\n%s", ref($error), $error->{text}, $body );
142              
143 2         37 $self->trace($msg);
144             }
145              
146             #===================================
147             sub trace_comment {
148             #===================================
149 1     1 1 432 my ( $self, $comment ) = @_;
150 1 50       69 return unless $self->is_trace;
151 1         249 $comment =~ s/^/# *** /mg;
152 1         4 chomp $comment;
153 1         30 $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__