File Coverage

blib/lib/Log/Log4perl/Appender/Elasticsearch/Bulk.pm
Criterion Covered Total %
statement 30 40 75.0
branch 1 4 25.0
condition 1 2 50.0
subroutine 8 9 88.8
pod 1 2 50.0
total 41 57 71.9


line stmt bran cond sub pod time code
1             package Log::Log4perl::Appender::Elasticsearch::Bulk;
2              
3 2     2   46604 use 5.006;
  2         7  
  2         82  
4 2     2   10 use strict;
  2         4  
  2         56  
5 2     2   20 use warnings;
  2         9  
  2         59  
6              
7 2     2   8 use constant _INTERNAL_DEBUG => 0;
  2         3  
  2         120  
8              
9 2     2   12 use base 'Log::Log4perl::Appender::Elasticsearch';
  2         2  
  2         795  
10              
11             =head1 NAME
12              
13             Log::Log4perl::Appender::Elasticsearch::Bulk
14              
15             =head1 DESCRIPTION
16              
17             This appender is based on L. It buffers the log entries and flush by certain buffer size or on destroy.
18              
19             =head1 OPTIONS
20              
21             =over 4
22              
23             =item
24              
25             buffer_size
26              
27             the number of log entries in a bulk load.
28              
29             default 50
30              
31             =back
32              
33             For further options see L
34              
35             =cut
36              
37             sub new {
38 1     1 1 990 my ($class, %p) = @_;
39 1         3 my $fc = delete($p{buffer_size});
40              
41 1         9 my $self = $class->SUPER::new(%p);
42              
43 1         3 $self->{_buffer} = [];
44 1   50     5 $self->{_buffer_size} = $fc || 50;
45              
46 1         4 return $self;
47             } ## end sub new
48              
49             sub _flush {
50 1     1   1 my ($self) = @_;
51 1         2 my $data = "";
52 1         2 my $buff = delete $self->{_buffer};
53 1         1 $self->{_buffer} = [];
54              
55 1 50       1 scalar(@{$buff}) || return;
  1         48  
56              
57 0         0 foreach (@{$buff}) {
  0         0  
58 0         0 $data .= join $/, '{"create":{}}', $self->_to_json($_), '';
59             }
60              
61 0         0 if (_INTERNAL_DEBUG) {
62             require Data::Dumper;
63             print Data::Dumper::Dumper($buff);
64             print $data;
65             }
66              
67 0         0 $self->_send_request($data, '_bulk');
68             } ## end sub _flush
69              
70             sub log {
71 0     0 0 0 my ($self, %p) = @_;
72 0         0 push @{ $self->{_buffer} }, $self->_prepare_body(%p);
  0         0  
73 0 0       0 (scalar(@{ $self->{_buffer} }) == $self->{_buffer_size}) && $self->_flush();
  0         0  
74             }
75              
76             sub DESTROY {
77 1     1   477 my ($self) = @_;
78 1         1 _INTERNAL_DEBUG && print "_flush on destroy\n";
79 1         3 $self->_flush();
80             }
81              
82             =head1 AUTHOR
83              
84             Alexei Pastuchov
85              
86             =head1 REPOSITORY
87              
88             L
89              
90             =head1 LICENSE AND COPYRIGHT
91              
92             Copyright 2015 by Alexei Pastuchov Epalik at cpan.orgE.
93              
94             This library is free software; you can redistribute it and/or modify
95             it under the same terms as Perl itself.
96              
97             =cut
98              
99             1; # End of Log::Log4perl::Appender::Elasticsearch::Bulk