File Coverage

blib/lib/Log/Saftpresse.pm
Criterion Covered Total %
statement 27 72 37.5
branch 0 10 0.0
condition 0 3 0.0
subroutine 9 15 60.0
pod 2 3 66.6
total 38 103 36.8


line stmt bran cond sub pod time code
1             package Log::Saftpresse;
2              
3 1     1   1397 use Moose;
  1         689894  
  1         12  
4              
5             # ABSTRACT: a modular logfile analyzer
6             our $VERSION = '1.6'; # VERSION
7              
8 1     1   11343 use Log::Saftpresse::Log4perl;
  1         6  
  1         270  
9 1     1   812 use Log::Saftpresse::Config;
  1         5  
  1         84  
10              
11 1     1   963 use Log::Saftpresse::Analyzer;
  1         3  
  1         48  
12 1     1   642 use Log::Saftpresse::Slurp;
  1         3  
  1         48  
13 1     1   603 use Log::Saftpresse::CounterOutputs;
  1         3  
  1         38  
14 1     1   431 use Log::Saftpresse::Outputs;
  1         2  
  1         52  
15              
16 1     1   722 use Time::Piece;
  1         8457  
  1         7  
17 1     1   72 use Sys::Hostname;
  1         3  
  1         673  
18              
19              
20             has 'config' => (
21             is => 'ro', isa => 'Log::Saftpresse::Config', lazy => 1,
22             default => sub { Log::Saftpresse::Config->new },
23             handles => [ 'load_config' ],
24             );
25              
26             has 'slurp' => (
27             is => 'ro', isa => 'Log::Saftpresse::Slurp', lazy => 1,
28             default => sub { Log::Saftpresse::Slurp->new },
29             );
30              
31             has 'analyzer' => (
32             is => 'ro', isa => 'Log::Saftpresse::Analyzer', lazy => 1,
33             default => sub { Log::Saftpresse::Analyzer->new },
34             );
35              
36             has 'counter_outputs' => (
37             is => 'ro', isa => 'Log::Saftpresse::CounterOutputs', lazy => 1,
38             default => sub { Log::Saftpresse::CounterOutputs->new },
39             );
40              
41             has 'outputs' => (
42             is => 'ro', isa => 'Log::Saftpresse::Outputs', lazy => 1,
43             default => sub { Log::Saftpresse::Outputs->new },
44             );
45              
46             has 'flush_interval' => ( is => 'rw', isa => 'Maybe[Int]' );
47              
48             has '_last_flush_counters' => (
49             is => 'rw', isa => 'Int',
50             default => sub { time },
51             );
52              
53              
54             sub init {
55 0     0 1   my $self = shift;
56 0           my $config = $self->config;
57            
58 0           Log::Saftpresse::Log4perl->init(
59             $config->get('logging', 'level'),
60             $config->get('logging', 'file'),
61             );
62 0           $self->flush_interval( $config->get('counters', 'flush_interval') );
63 0           $self->slurp->load_config( $config->get_node('Input') );
64 0           $self->analyzer->load_config( $config->get_node('Plugin') );
65 0           $self->counter_outputs->load_config( $config->get_node('CounterOutput') );
66 0           $self->outputs->load_config( $config->get_node('Output') );
67              
68 0           return;
69             }
70              
71             sub _need_flush_counters {
72 0     0     my $self = shift;
73              
74 0 0 0       if( ! defined $self->flush_interval
75             || $self->flush_interval < 1 ) {
76 0           return 0;
77             }
78              
79 0           my $next_flush = $self->_last_flush_counters + $self->flush_interval;
80 0 0         if( time < $next_flush ) {
81 0           return 0;
82             }
83              
84 0           return 1;
85             }
86              
87             sub _flushed_counters {
88 0     0     my $self = shift;
89 0           $self->_last_flush_counters( time );
90 0           return;
91             }
92              
93             sub saftpresse_version {
94 0     0 0   my $version = 'development';
95 0           eval '$version = $VERSION;'; ## no critic
96 0           return $version;
97             }
98              
99             sub _startup_event {
100 0     0     my $self = shift;
101 0           my $version = $self->saftpresse_version;
102             return {
103 0           time => Time::Piece->new,
104             host => hostname(),
105             message => "saftpresse ($version) started",
106             };
107             }
108              
109              
110             sub run {
111 0     0 1   my $self = shift;
112 0           my $slurp = $self->slurp;
113 0           my $last_flush = time;
114              
115 0           $self->outputs->output( $self->_startup_event );
116              
117 0           $log->info('entering main loop');
118 0           for(;;) { # main loop
119 0           my $events;
120 0 0         if( $slurp->can_read(1) ) {
121 0           $log->debug('checking for new input...');
122 0           $events = $slurp->read_events;
123 0           foreach my $event ( @$events ) {
124 0           $self->analyzer->process_event( $event );
125             }
126             }
127 0 0         if( scalar @$events ) {
128 0           $log->debug('sending '.scalar(@$events).' events to outputs...');
129 0           $self->outputs->output( @$events );
130             }
131              
132 0 0         if( $self->_need_flush_counters ){
133 0           $log->debug('flushing counters...');
134 0           $self->counter_outputs->output(
135             $self->analyzer->get_all_counters );
136 0           $self->_flushed_counters;
137             }
138             }
139              
140 0           return;
141             }
142              
143              
144             1;
145              
146             __END__
147              
148             =pod
149              
150             =encoding UTF-8
151              
152             =head1 NAME
153              
154             Log::Saftpresse - a modular logfile analyzer
155              
156             =head1 VERSION
157              
158             version 1.6
159              
160             =head1 Description
161              
162             This is the central class of the saftpresse log analyzer.
163              
164             =head1 Synopsis
165              
166             use Log::Saftpresse;
167              
168             my $saft = Log:::Saftpresse->new;
169              
170             $saft->load_config( $path );
171             $saft->init;
172              
173             # start main loop
174             $saft->run;
175              
176             =head1 Attributes
177              
178             =head2 config( L<Log::Saftpresse::Config>)
179              
180             Holds the configuration.
181              
182             =head2 slurp( L<Log::Saftpresse::Slurp> )
183              
184             Holds the slurp class implementing the input.
185              
186             =head2 analyzer( L<Log::Saftpresse::Analyzer> )
187              
188             Holds the analyzer object which controls the processing plugins.
189              
190             =head2 counter_outputs( L<Log::Saftpresse::CounterOutputs> )
191              
192             Holds the counter output object which controls output of metrics.
193              
194             =head2 outputs( L<Log::Saftpresse::Outputs> )
195              
196             Holds the Outputs plugin which controls the event output.
197              
198             =head2 flush_interval( $seconds )
199              
200             How often to flush metrics to CounterOutputs.
201              
202             =head1 Methods
203              
204             =head2 init
205              
206             Initialize saftpresse as configured in config file.
207              
208             Will load slurp, analyzer, counter_outputs, outputs and flush_interval
209             from configuration.
210              
211             =head2 run
212              
213             Run the main loop of saftpresse.
214              
215             =head1 See also
216              
217             =over
218              
219             =item L<Log::Saftpresse::App>
220              
221             Commandline glue for this class.
222              
223             =item bin/saftpresse
224              
225             Commandline interface of saftpresse with end-user docs.
226              
227             =back
228              
229             =head1 AUTHOR
230              
231             Markus Benning <ich@markusbenning.de>
232              
233             =head1 COPYRIGHT AND LICENSE
234              
235             This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
236              
237             This is free software, licensed under:
238              
239             The GNU General Public License, Version 2, June 1991
240              
241             =cut