File Coverage

blib/lib/Log/Saftpresse/Input/FileTail.pm
Criterion Covered Total %
statement 15 46 32.6
branch 0 6 0.0
condition n/a
subroutine 5 11 45.4
pod 0 5 0.0
total 20 68 29.4


line stmt bran cond sub pod time code
1             package Log::Saftpresse::Input::FileTail;
2              
3 1     1   1622 use Moose;
  1         2  
  1         9  
4              
5             # ABSTRACT: log input for following a file
6             our $VERSION = '1.6'; # VERSION
7              
8              
9 1     1   5374 use IO::File;
  1         2  
  1         207  
10 1     1   866 use File::stat;
  1         6306  
  1         6  
11              
12 1     1   67 use Time::Piece;
  1         2  
  1         25  
13 1     1   71 use Sys::Hostname;
  1         2  
  1         415  
14              
15             extends 'Log::Saftpresse::Input';
16              
17             has 'path' => ( is => 'ro', isa => 'Str',
18             default => sub {
19             my $self = shift;
20             return $self->name;
21             },
22             );
23              
24             has 'file' => ( is => 'rw', isa => 'IO::File', lazy => 1,
25             default => sub {
26             my $self = shift;
27             return $self->_open_file;
28             },
29             );
30              
31             sub _open_file {
32 0     0     my $self = shift;
33 0           my $f = IO::File->new($self->path,"r");
34 0 0         if( ! defined $f ) {
35 0           die('could not open '.$self->path.' for input: '.$!);
36             }
37 0           $f->blocking(0);
38 0           $f->seek(0,2); # seek to end of file
39 0           return $f;
40             }
41              
42             sub reopen_file {
43 0     0 0   my $self = shift;
44 0           $self->file->close;
45 0           $self->file( $self->_open_file );
46 0           return;
47             }
48              
49             sub io_handles {
50 0     0 0   my $self = shift;
51 0           return;
52             }
53              
54             sub read_events {
55 0     0 0   my ( $self ) = @_;
56 0           my @events;
57 0           foreach my $line ( $self->file->getlines ) {
58 0           chomp( $line );
59 0           my $event = {
60             'host' => hostname,
61             'time' => Time::Piece->new,
62             'message' => $line,
63             };
64 0           push( @events, $event );
65             }
66 0           $self->file->seek(0,1); # clear eof flag
67 0           return @events;
68             }
69              
70             sub eof {
71 0     0 0   my $self = shift;
72 0 0         if( stat($self->file)->nlink == 0 ) {
73             # file has been deleted (logrotate?)
74 0           $self->reopen_file;
75             }
76 0           return 0; # we dont signal eof, we're almost always eof.
77             }
78              
79             sub can_read {
80 0     0 0   my $self = shift;
81 0           my $mypos = $self->file->tell;
82 0           my $size = stat($self->file)->size;
83 0 0         if( $size > $mypos ) {
84 0           return 1;
85             }
86 0           return 0;
87             }
88              
89              
90             1;
91              
92             __END__
93              
94             =pod
95              
96             =encoding UTF-8
97              
98             =head1 NAME
99              
100             Log::Saftpresse::Input::FileTail - log input for following a file
101              
102             =head1 VERSION
103              
104             version 1.6
105              
106             =head1 Description
107              
108             This input watches a file for newly appended lines.
109              
110             =head1 Synopsis
111              
112             <Input maillog>
113             module = "FileTail"
114             path = "/var/log/mail.log"
115             </Input>
116              
117             =head1 Format
118              
119             Foreach line appended to the file a event with the following fields is generated:
120              
121             =over
122              
123             =item message
124              
125             Content of the line.
126              
127             =item host
128              
129             The hostname of the system.
130              
131             =item time
132              
133             The current time.
134              
135             =back
136              
137             =head1 AUTHOR
138              
139             Markus Benning <ich@markusbenning.de>
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
144              
145             This is free software, licensed under:
146              
147             The GNU General Public License, Version 2, June 1991
148              
149             =cut