| 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 | 56 | $OpenSearch::Client::Role::Logger::VERSION = '3.007011'; | |||||||||||
| 25 | 56 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | |||||||||||||
| 27 | 56 | ▶ | use URI(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 28 | 56 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 56 | ▶ | use OpenSearch::Client::Util qw(new_error); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | 56 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | |||||||||||||
| 32 | 56 | has 'serializer' => ( is => 'ro', required => 1 ); | |||||||||||
| 33 | 56 | has 'log_as' => ( is => 'ro', default => 'opensearch.event' ); | |||||||||||
| 34 | 56 | has 'trace_as' => ( is => 'ro', default => 'opensearch.trace' ); | |||||||||||
| 35 | 56 | has 'deprecate_as' => ( is => 'ro', default => 'opensearch.deprecation' ); | |||||||||||
| 36 | 56 | has 'log_to' => ( is => 'ro' ); | |||||||||||
| 37 | 56 | has 'trace_to' => ( is => 'ro' ); | |||||||||||
| 38 | 56 | has 'deprecate_to' => ( is => 'ro' ); | |||||||||||
| 39 | |||||||||||||
| 40 | 56 | has 'trace_handle' => ( | |||||||||||
| 41 | is => 'lazy', | ||||||||||||
| 42 | handles => [qw( trace tracef is_trace)] | ||||||||||||
| 43 | ); | ||||||||||||
| 44 | |||||||||||||
| 45 | 56 | 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 | 56 | has 'deprecate_handle' => ( is => 'lazy' ); | |||||||||||
| 58 | |||||||||||||
| 59 | #=================================== | ||||||||||||
| 60 | 16 | ▶ | sub throw_error { | ||||||||||
|
Subroutine
throw_error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 61 | #=================================== | ||||||||||||
| 62 | 16 | my ( $self, $type, $msg, $vars ) = @_; | |||||||||||
| 63 | 16 | my $error = new_error( $type, $msg, $vars ); | |||||||||||
| 64 | 16 | $self->error($error); | |||||||||||
| 65 | 16 | die $error; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | #=================================== | ||||||||||||
| 69 | 13 | ▶ | sub throw_critical { | ||||||||||
|
Subroutine
throw_critical: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 70 | #=================================== | ||||||||||||
| 71 | 13 | my ( $self, $type, $msg, $vars ) = @_; | |||||||||||
| 72 | 13 | my $error = new_error( $type, $msg, $vars ); | |||||||||||
| 73 | 13 | $self->critical($error); | |||||||||||
| 74 | 13 | die $error; | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | #=================================== | ||||||||||||
| 78 | 152 | ▶ | sub trace_request { | ||||||||||
|
Subroutine
trace_request: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 79 | #=================================== | ||||||||||||
| 80 | 152 | my ( $self, $cxn, $params ) = @_; | |||||||||||
| 81 | 152 | ▶ | return unless $self->is_trace; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | |||||||||||||
| 83 | 4 | my $uri = URI->new( 'http://localhost:9200' . $params->{path} ); | |||||||||||
| 84 | 4 | my %qs = ( %{ $params->{qs} }, pretty => "true" ); | |||||||||||
| 85 | 4 | $uri->query_form( [ map { $_, $qs{$_} } sort keys %qs ] ); | |||||||||||
| 86 | |||||||||||||
| 87 | my $body | ||||||||||||
| 88 | = $params->{serialize} eq 'std' | ||||||||||||
| 89 | ? $self->serializer->encode_pretty( $params->{body} ) | ||||||||||||
| 90 | 4 | ▶ | : $params->{data}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | |||||||||||||
| 92 | 4 | my $content_type = ''; | |||||||||||
| 93 | 4 | ▶ | if ( defined $body ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 3 | $body =~ s/'/\\u0027/g; | |||||||||||
| 95 | 3 | $body = " -d '\n$body'\n"; | |||||||||||
| 96 | 3 | $content_type = '-H "Content-type: ' . $params->{mime_type} . '" '; | |||||||||||
| 97 | } | ||||||||||||
| 98 | 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 | $uri, | |||||||||||
| 107 | $body | ||||||||||||
| 108 | ); | ||||||||||||
| 109 | |||||||||||||
| 110 | 4 | $self->trace($msg); | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | #=================================== | ||||||||||||
| 114 | 116 | ▶ | sub trace_response { | ||||||||||
|
Subroutine
trace_response: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 115 | #=================================== | ||||||||||||
| 116 | 116 | my ( $self, $cxn, $code, $response, $took ) = @_; | |||||||||||
| 117 | 116 | ▶ | return unless $self->is_trace; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | |||||||||||||
| 119 | 2 | ▶ | my $body = $self->serializer->encode_pretty($response) || "\n"; | ||||||||||
|
Condition100%2/2
$self->serializer->encode_pretty($response) || "\n"
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)
| |||||||||||||
| 120 | 2 | $body =~ s/^/# /mg; | |||||||||||
| 121 | |||||||||||||
| 122 | 2 | my $msg = sprintf( | |||||||||||
| 123 | "# Response: %s, Took: %d ms\n%s", # | ||||||||||||
| 124 | $code, $took * 1000, $body | ||||||||||||
| 125 | ); | ||||||||||||
| 126 | |||||||||||||
| 127 | 2 | $self->trace($msg); | |||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | #=================================== | ||||||||||||
| 131 | 29 | ▶ | sub trace_error { | ||||||||||
|
Subroutine
trace_error: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 132 | #=================================== | ||||||||||||
| 133 | 29 | my ( $self, $cxn, $error ) = @_; | |||||||||||
| 134 | 29 | ▶ | return unless $self->is_trace; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | |||||||||||||
| 136 | my $body | ||||||||||||
| 137 | 2 | ▶ | = $self->serializer->encode_pretty( $error->{vars}{body} || "\n" ); | ||||||||||
|
Condition100%2/2
$error->{'vars'}{'body'} || "\n"
MC/DC100%1/1
$error->{'vars'}{'body'} || "\n"
$error->{'vars'}{'body'}
Truth table100%2/2
A
A= $error->{'vars'}{'body'}
| |||||||||||||
| 138 | 2 | $body =~ s/^/# /mg; | |||||||||||
| 139 | |||||||||||||
| 140 | my $msg | ||||||||||||
| 141 | 2 | = sprintf( "# ERROR: %s %s\n%s", ref($error), $error->{text}, $body ); | |||||||||||
| 142 | |||||||||||||
| 143 | 2 | $self->trace($msg); | |||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | #=================================== | ||||||||||||
| 147 | 1 | ▶ | sub trace_comment { | ||||||||||
|
Subroutine
trace_comment: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 148 | #=================================== | ||||||||||||
| 149 | 1 | my ( $self, $comment ) = @_; | |||||||||||
| 150 | 1 | ▶ | return unless $self->is_trace; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 151 | 1 | $comment =~ s/^/# *** /mg; | |||||||||||
| 152 | 1 | chomp $comment; | |||||||||||
| 153 | 1 | $self->trace("$comment\n"); | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | #=================================== | ||||||||||||
| 157 | 1 | ▶ | sub deprecation { | ||||||||||
|
Subroutine
deprecation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 158 | #=================================== | ||||||||||||
| 159 | 1 | my $self = shift; | |||||||||||
| 160 | |||||||||||||
| 161 | 1 | $self->deprecate_handle->warnf( "[DEPRECATION] %s - In request: %s", @_ ); | |||||||||||
| 162 | } | ||||||||||||
| 163 | 56 | 1; | |||||||||||
| 164 | |||||||||||||
| 165 | __END__ | ||||||||||||