File Coverage

blib/lib/Log/Log4perl/Appender/Elasticsearch/Bulk.pm
Criterion Covered Total %
statement 32 42 76.1
branch 1 4 25.0
condition 1 2 50.0
subroutine 9 10 90.0
pod 1 2 50.0
total 44 60 73.3


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