| 1 | package Log::Log4perl::Appender::Elasticsearch; | ||||||||||||||||||||
| 2 | 4 | ▶ | use version(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 4 | $Log::Log4perl::Appender::Elasticsearch::VERSION = version->parse("0.09"); | |||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 4 | our @ISA = qw(Log::Log4perl::Appender); | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 4 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 4 | ▶ | use HTTP::Headers(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 4 | ▶ | use HTTP::Request(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 4 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 4 | ▶ | use LWP::UserAgent(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 4 | ▶ | use Log::Log4perl; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 4 | ▶ | use MIME::Base64; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 4 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | Log::Log4perl::Appender::Elasticsearch - implements appending to Elasticsearch | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | This is a simple appender for writing log entries to Elasticsearch via L<Index API|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html>. | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | L<Log::Log4perl::Appender::Elasticsearch::Bulk> does the same task by using L<Bulk API|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html>. | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =head1 VERSION | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | Version 0.09 | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | use Log::Log4perl; | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Log::Log4perl->init(\<<'HERE'); | ||||||||||||||||||||
| 39 | log4perl.logger=DEBUG, ES | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | log4perl.appender.ES = Log::Log4perl::Appender::Elasticsearch | ||||||||||||||||||||
| 42 | log4perl.appender.ES.layout = Log::Log4perl::Layout::NoopLayout | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | log4perl.appender.ES.body.level = %p | ||||||||||||||||||||
| 45 | log4perl.appender.ES.body.module = %M | ||||||||||||||||||||
| 46 | log4perl.appender.ES.body.line = %L | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | log4perl.appender.ES.nodes = localhost:9200 | ||||||||||||||||||||
| 49 | log4perl.appender.ES.index = log4perl | ||||||||||||||||||||
| 50 | log4perl.appender.ES.type = entry | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | log4perl.appender.ES.use_https = 0 | ||||||||||||||||||||
| 53 | log4perl.appender.ES.user_agent.timeout = 5 | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | log4perl.appender.ES.headers.User-Agent = foo | ||||||||||||||||||||
| 56 | HERE | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | Log::Log4perl::get_logger()->info("OK"); | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | # look up: | ||||||||||||||||||||
| 61 | # curl -XPOST 'http://localhost:9200/log4perl/_search' -d \ | ||||||||||||||||||||
| 62 | # '{"query": {"query_string": {"query": "level:INFO AND message:OK"}}}' | ||||||||||||||||||||
| 63 | # ... | ||||||||||||||||||||
| 64 | # "_source": { | ||||||||||||||||||||
| 65 | # "module": "main::__ANON__", | ||||||||||||||||||||
| 66 | # "line": "60", | ||||||||||||||||||||
| 67 | # "level": "INFO", | ||||||||||||||||||||
| 68 | # "message": "OK" | ||||||||||||||||||||
| 69 | # } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head1 OPTIONS | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =over 4 | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =item | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | nodes | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | a comma separeted list of nodes. The message will be sent to the next node only if previous request failed | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =item | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | index | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | The name of the elasticsearch index the message will be stored in. | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =item | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | type | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | The name of the type in given index the message belongs to. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =item | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | use_https | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0|1 global https setting for all nodes | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | the individual https setting possible too: | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | C<log4perl.appender.ES.nodes = https://user:password@node1:9200,localhost:9200> | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =item | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | user_agent | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | LWP::UserAgent parameters | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | C<log4perl.appender.ES.user_agent.timeout = 5> | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =item | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | headers | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | HTTP::Headers parameters | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | C<log4perl.appender.ES.headers.User-Agent = foo> | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =back | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 125 | 2 | my ($proto, %p) = @_; | |||||||||||||||||||
| 126 | 2 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 127 | 2 | my $self = bless {}, $class; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 2 | $self->_init(%p); | |||||||||||||||||||
| 130 | 2 | return $self; | |||||||||||||||||||
| 131 | } ## end sub new | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 3 | ▶ | sub index { | ||||||||||||||||||
|
Subroutine
index: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 134 | 3 | my ($self, $index) = @_; | |||||||||||||||||||
| 135 | 3 | ▶ | if (defined $index) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 2 | $self->{_index} = $index; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 3 | return $self->{_index}; | |||||||||||||||||||
| 140 | } ## end sub index | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 3 | ▶ | sub type { | ||||||||||||||||||
|
Subroutine
type: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 143 | 3 | my ($self, $type) = @_; | |||||||||||||||||||
| 144 | 3 | ▶ | if (defined $type) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | 2 | $self->{_type} = $type; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 3 | return $self->{_type}; | |||||||||||||||||||
| 149 | } ## end sub type | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 0 | ▶ | sub log { | ||||||||||||||||||
|
Subroutine
log: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 152 | 0 | my ($self, %p) = @_; | |||||||||||||||||||
| 153 | 0 | $self->_send_request($self->_to_json($self->_prepare_body(%p))); | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 2 | ▶ | sub _init { | ||||||||||||||||||
|
Subroutine
_init: called CC=11 SCAR=24.3
| |||||||||||||||||||||
| 157 | 2 | my ($self, %p) = @_; | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | defined($p{nodes}) | ||||||||||||||||||||
| 160 | 2 | ▶ | || Carp::croak('Log4perl: nodes not set in ', __PACKAGE__); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 2 | my $use_https = delete($p{use_https}); | |||||||||||||||||||
| 163 | 2 | foreach (split ',', delete($p{nodes})) { | |||||||||||||||||||
| 164 | 2 | (my $node = $_) =~ s/^\s+|\s+$//g; | |||||||||||||||||||
| 165 | 2 | ▶ | unless ($node =~ m{^http(s)?://}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | 2 | ▶ | $node = ($use_https ? 'https://' : 'http://') . $node; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 2 | my $uri = URI->new($node); | |||||||||||||||||||
| 170 | 2 | push @{ $self->{_nodes} }, $uri; | |||||||||||||||||||
| 171 | } ## end foreach (split ',', delete(...)) | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 2 | foreach my $k (qw/index type/) { | |||||||||||||||||||
| 174 | 4 | my $v = delete($p{$k}); | |||||||||||||||||||
| 175 | 4 | ▶ | $v || Carp::croak("Log4perl: $k not set in ", __PACKAGE__); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 176 | 4 | $self->$k($v); | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 2 | my $b = delete($p{body}); | |||||||||||||||||||
| 180 | 2 | ▶ | scalar(keys %{$b}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | || Carp::croak('Log4perl: body not set in ', __PACKAGE__); | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 2 | foreach my $k (keys %{$b}) { | |||||||||||||||||||
| 184 | 6 | ▶ | $k eq 'message' && Carp::croak( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | "Log4perl: choose an other key name instead $k. The key $k is used to store the logging message ", | ||||||||||||||||||||
| 186 | __PACKAGE__ | ||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||
| 188 | $self->{_body}{$k} = Log::Log4perl::Layout::PatternLayout->new( | ||||||||||||||||||||
| 189 | 6 | { ConversionPattern => { value => $b->{$k} } }); | |||||||||||||||||||
| 190 | } ## end foreach my $k (keys %{$b}) | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 2 | my $h = delete($p{headers}); | |||||||||||||||||||
| 193 | 2 | $self->{_headers} = HTTP::Headers->new(%{$h}); | |||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 2 | my $up = delete($p{user_agent}); | |||||||||||||||||||
| 196 | 2 | $self->{_user_agent} = LWP::UserAgent->new(%{$up}); | |||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 2 | foreach my $k (keys %p) { | |||||||||||||||||||
| 199 | 1 | $self->{$k} = $p{$k}; | |||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | } ## end sub _init | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 0 | ▶ | sub _send_request { | ||||||||||||||||||
|
Subroutine
_send_request: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 204 | 0 | my ($self, $data, $suffix) = @_; | |||||||||||||||||||
| 205 | 0 | my @nodes = @{ $self->{_nodes} }; | |||||||||||||||||||
| 206 | 0 | my (@errors, $ok); | |||||||||||||||||||
| 207 | 0 | ▶ | do { | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
not $ok and scalar @nodes
MC/DC0%0/1
not $ok and scalar @nodes
not $ok
Truth table0%0/2
not(A)
A= not $ok
| |||||||||||||||||||||
| 208 | 0 | my $node = shift @nodes; | |||||||||||||||||||
| 209 | 0 | my $uri = $self->_uri($node, $suffix); | |||||||||||||||||||
| 210 | 0 | my $req = $self->_request($uri, $data); | |||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 0 | my $resp = $self->{_user_agent}->request($req); | |||||||||||||||||||
| 213 | 0 | $ok = $resp->is_success; | |||||||||||||||||||
| 214 | 0 | ▶ | if (!$ok) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 215 | 0 | push @errors, join ': ', $uri, $resp->status_line; | |||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | } while (!$ok && scalar(@nodes)); | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 0 | ▶ | $ok || Carp::croak('coud not send log the message to any node: ', | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 220 | join '; ', @errors); | ||||||||||||||||||||
| 221 | } ## end sub _send_request | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | 1 | ▶ | sub _uri { | ||||||||||||||||||
|
Subroutine
_uri: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 224 | 1 | my ($self, $node, $suffix) = @_; | |||||||||||||||||||
| 225 | 1 | my $uri = $node->clone; | |||||||||||||||||||
| 226 | $uri->path(join '', $uri->path, | ||||||||||||||||||||
| 227 | 1 | ▶ | join('/', $self->{_index}, $self->{_type}, $suffix ? $suffix : '')); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | 1 | return $uri; | |||||||||||||||||||
| 230 | } ## end sub _uri | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | 1 | ▶ | sub _headers { | ||||||||||||||||||
|
Subroutine
_headers: called CC=2 SCAR=7.8
| |||||||||||||||||||||
| 233 | 1 | my ($self, $uri) = @_; | |||||||||||||||||||
| 234 | 1 | my $h = $self->{_headers}->clone; | |||||||||||||||||||
| 235 | 1 | $h->header('Content-Type' => 'application/json'); | |||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | 1 | my $ui = $uri->userinfo; | |||||||||||||||||||
| 238 | 1 | ▶ | if ($ui) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 239 | 0 | my $auth = MIME::Base64::encode_base64($ui); | |||||||||||||||||||
| 240 | 0 | chomp $auth; | |||||||||||||||||||
| 241 | 0 | $h->header(Authorization => "Basic $auth"); | |||||||||||||||||||
| 242 | } | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | 1 | return $h; | |||||||||||||||||||
| 245 | } ## end sub _headers | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | 0 | ▶ | sub _request { | ||||||||||||||||||
|
Subroutine
_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 248 | 0 | my ($self, $uri, $data) = @_; | |||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | 0 | return HTTP::Request->new( | |||||||||||||||||||
| 251 | POST => $uri, | ||||||||||||||||||||
| 252 | $self->_headers($uri), | ||||||||||||||||||||
| 253 | $data | ||||||||||||||||||||
| 254 | ); | ||||||||||||||||||||
| 255 | } ## end sub _request | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | 0 | ▶ | sub _prepare_body { | ||||||||||||||||||
|
Subroutine
_prepare_body: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 258 | 0 | my ($self, %p) = @_; | |||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | 0 | my $b = {}; | |||||||||||||||||||
| 261 | 0 | foreach my $k (keys %{ $self->{_body} }) { | |||||||||||||||||||
| 262 | my $v | ||||||||||||||||||||
| 263 | = $self->{_body}{$k} | ||||||||||||||||||||
| 264 | ->render($p{message}, $p{log4p_category}, $p{log4p_level}, | ||||||||||||||||||||
| 265 | 0 | 5 + $Log::Log4perl::caller_depth, | |||||||||||||||||||
| 266 | ); | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | 0 | $b->{$k} = $v; | |||||||||||||||||||
| 269 | } ## end foreach my $k (keys %{ $self...}) | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | 0 | $b->{message} = $p{message}; | |||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | 0 | return $b; | |||||||||||||||||||
| 274 | } ## end sub _prepare_body | ||||||||||||||||||||
| 275 | |||||||||||||||||||||
| 276 | 0 | ▶ | sub _to_json { | ||||||||||||||||||
|
Subroutine
_to_json: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 277 | 0 | my ($self, $o) = @_; | |||||||||||||||||||
| 278 | 0 | return JSON::encode_json($o); | |||||||||||||||||||
| 279 | } | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | 4 | 1; # End of Log::Log4perl::Appender::Elasticsearch | |||||||||||||||||||
| 282 | __END__ | ||||||||||||||||||||