| 1 | # Licensed to Elasticsearch B.V under one or more agreements. | ||||||||||||
| 2 | # Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||||||||||||
| 3 | # See the LICENSE file in the project root for more information | ||||||||||||
| 4 | |||||||||||||
| 5 | package Search::Elasticsearch::Role::Logger; | ||||||||||||
| 6 | 55 | $Search::Elasticsearch::Role::Logger::VERSION = '7.30_1'; # TRIAL | |||||||||||
| 7 | |||||||||||||
| 8 | 55 | $Search::Elasticsearch::Role::Logger::VERSION = '7.301'; | |||||||||||
| 9 | 55 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 55 | ▶ | use URI(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 55 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 55 | ▶ | use Search::Elasticsearch::Util qw(new_error); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 55 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 55 | has 'serializer' => ( is => 'ro', required => 1 ); | |||||||||||
| 17 | 55 | has 'log_as' => ( is => 'ro', default => 'elasticsearch.event' ); | |||||||||||
| 18 | 55 | has 'trace_as' => ( is => 'ro', default => 'elasticsearch.trace' ); | |||||||||||
| 19 | 55 | has 'deprecate_as' => ( is => 'ro', default => 'elasticsearch.deprecation' ); | |||||||||||
| 20 | 55 | has 'log_to' => ( is => 'ro' ); | |||||||||||
| 21 | 55 | has 'trace_to' => ( is => 'ro' ); | |||||||||||
| 22 | 55 | has 'deprecate_to' => ( is => 'ro' ); | |||||||||||
| 23 | |||||||||||||
| 24 | 55 | has 'trace_handle' => ( | |||||||||||
| 25 | is => 'lazy', | ||||||||||||
| 26 | handles => [qw( trace tracef is_trace)] | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | |||||||||||||
| 29 | 55 | has 'log_handle' => ( | |||||||||||
| 30 | is => 'lazy', | ||||||||||||
| 31 | handles => [ qw( | ||||||||||||
| 32 | debug debugf is_debug | ||||||||||||
| 33 | info infof is_info | ||||||||||||
| 34 | warning warningf is_warning | ||||||||||||
| 35 | error errorf is_error | ||||||||||||
| 36 | critical criticalf is_critical | ||||||||||||
| 37 | ) | ||||||||||||
| 38 | ] | ||||||||||||
| 39 | ); | ||||||||||||
| 40 | |||||||||||||
| 41 | 55 | has 'deprecate_handle' => ( is => 'lazy' ); | |||||||||||
| 42 | |||||||||||||
| 43 | #=================================== | ||||||||||||
| 44 | sub throw_error { | ||||||||||||
| 45 | #=================================== | ||||||||||||
| 46 | 16 | ▶ | my ( $self, $type, $msg, $vars ) = @_; | ||||||||||
|
Subroutine
throw_error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 47 | 16 | my $error = new_error( $type, $msg, $vars ); | |||||||||||
| 48 | 16 | $self->error($error); | |||||||||||
| 49 | 16 | die $error; | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | #=================================== | ||||||||||||
| 53 | sub throw_critical { | ||||||||||||
| 54 | #=================================== | ||||||||||||
| 55 | 13 | ▶ | my ( $self, $type, $msg, $vars ) = @_; | ||||||||||
|
Subroutine
throw_critical: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 56 | 13 | my $error = new_error( $type, $msg, $vars ); | |||||||||||
| 57 | 13 | $self->critical($error); | |||||||||||
| 58 | 13 | die $error; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | #=================================== | ||||||||||||
| 62 | sub trace_request { | ||||||||||||
| 63 | #=================================== | ||||||||||||
| 64 | 152 | ▶ | my ( $self, $cxn, $params ) = @_; | ||||||||||
|
Subroutine
trace_request: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 65 | 152 | ▶ | return unless $self->is_trace; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | |||||||||||||
| 67 | 4 | my $uri = URI->new( 'http://localhost:9200' . $params->{path} ); | |||||||||||
| 68 | 4 | my %qs = ( %{ $params->{qs} }, pretty => "true" ); | |||||||||||
| 69 | 4 | $uri->query_form( [ map { $_, $qs{$_} } sort keys %qs ] ); | |||||||||||
| 70 | |||||||||||||
| 71 | my $body | ||||||||||||
| 72 | = $params->{serialize} eq 'std' | ||||||||||||
| 73 | ? $self->serializer->encode_pretty( $params->{body} ) | ||||||||||||
| 74 | 4 | ▶ | : $params->{data}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | |||||||||||||
| 76 | 4 | my $content_type = ''; | |||||||||||
| 77 | 4 | ▶ | if ( defined $body ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 3 | $body =~ s/'/\\u0027/g; | |||||||||||
| 79 | 3 | $body = " -d '\n$body'\n"; | |||||||||||
| 80 | 3 | $content_type = '-H "Content-type: ' . $params->{mime_type} . '" '; | |||||||||||
| 81 | } | ||||||||||||
| 82 | 1 | else { $body = "\n" } | |||||||||||
| 83 | |||||||||||||
| 84 | my $msg = sprintf( | ||||||||||||
| 85 | "# Request to: %s\n" # | ||||||||||||
| 86 | . "curl %s-X%s '%s'%s", # | ||||||||||||
| 87 | $cxn->stringify, | ||||||||||||
| 88 | $content_type, | ||||||||||||
| 89 | $params->{method}, | ||||||||||||
| 90 | 4 | $uri, | |||||||||||
| 91 | $body | ||||||||||||
| 92 | ); | ||||||||||||
| 93 | |||||||||||||
| 94 | 4 | $self->trace($msg); | |||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | #=================================== | ||||||||||||
| 98 | sub trace_response { | ||||||||||||
| 99 | #=================================== | ||||||||||||
| 100 | 116 | ▶ | my ( $self, $cxn, $code, $response, $took ) = @_; | ||||||||||
|
Subroutine
trace_response: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 101 | 116 | ▶ | return unless $self->is_trace; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | |||||||||||||
| 103 | 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)
| |||||||||||||
| 104 | 2 | $body =~ s/^/# /mg; | |||||||||||
| 105 | |||||||||||||
| 106 | 2 | my $msg = sprintf( | |||||||||||
| 107 | "# Response: %s, Took: %d ms\n%s", # | ||||||||||||
| 108 | $code, $took * 1000, $body | ||||||||||||
| 109 | ); | ||||||||||||
| 110 | |||||||||||||
| 111 | 2 | $self->trace($msg); | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | #=================================== | ||||||||||||
| 115 | sub trace_error { | ||||||||||||
| 116 | #=================================== | ||||||||||||
| 117 | 29 | ▶ | my ( $self, $cxn, $error ) = @_; | ||||||||||
|
Subroutine
trace_error: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 118 | 29 | ▶ | return unless $self->is_trace; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | |||||||||||||
| 120 | my $body | ||||||||||||
| 121 | 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'}
| |||||||||||||
| 122 | 2 | $body =~ s/^/# /mg; | |||||||||||
| 123 | |||||||||||||
| 124 | my $msg | ||||||||||||
| 125 | 2 | = sprintf( "# ERROR: %s %s\n%s", ref($error), $error->{text}, $body ); | |||||||||||
| 126 | |||||||||||||
| 127 | 2 | $self->trace($msg); | |||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | #=================================== | ||||||||||||
| 131 | sub trace_comment { | ||||||||||||
| 132 | #=================================== | ||||||||||||
| 133 | 1 | ▶ | my ( $self, $comment ) = @_; | ||||||||||
|
Subroutine
trace_comment: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 134 | 1 | ▶ | return unless $self->is_trace; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | 1 | $comment =~ s/^/# *** /mg; | |||||||||||
| 136 | 1 | chomp $comment; | |||||||||||
| 137 | 1 | $self->trace("$comment\n"); | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | #=================================== | ||||||||||||
| 141 | sub deprecation { | ||||||||||||
| 142 | #=================================== | ||||||||||||
| 143 | 1 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
deprecation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 144 | |||||||||||||
| 145 | 1 | $self->deprecate_handle->warnf( "[DEPRECATION] %s - In request: %s", @_ ); | |||||||||||
| 146 | } | ||||||||||||
| 147 | 55 | 1; | |||||||||||
| 148 | |||||||||||||
| 149 | # ABSTRACT: Provides common functionality to Logger implementations | ||||||||||||
| 150 | |||||||||||||
| 151 | __END__ | ||||||||||||