Leser: 23
1 2 3 4 5
use File::Tail; my $logfile=File::Tail->new(name=>$filename, tail=>10); while (defined(my $line=$logfile->read)) { print "$line"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#! /usr/bin/perl use warnings; use strict; use Tie::File; use constant { MAX_LINES => 10, }; my $logfile = '/tmp/logfile'; # binde datei an array; legt datei an, wenn nicht existent tie my @file, 'Tie::File', $logfile or die "$logfile: $!\n"; # generiere log eintraege for my $i ( 1 .. 20 ) { # entferne ersten eintrag, wenn maximum erreicht shift @file if MAX_LINES == @file; # fuege eintrag hinten an push @file, sprintf "meine nachricht: %02d\n", $i; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/perl use strict; use warnings; use FindBin '$Bin'; use lib $Bin; use Log::Log4perl::Appender::File::fixed_size; my $app = Log::Log4perl::Appender::File::fixed_size->new( filename => 'file.log', max_lines => 10, header_text=>'Nur ein Test!', ); for(0..100) { $app->log(message => "Log me (I'm message $_ from ".localtime()." )\n"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
package Log::Log4perl::Appender::File::fixed_size; ################################################## use base qw(Log::Log4perl::Appender::File); use constant _INTERNAL_DEBUG => 0; use warnings; use strict; use Fcntl; use Tie::File; ################################################## sub new { ################################################## my($class, @options) = @_; pop(@options) if(@options%2!=0); my $self={ max_lines => 0, name => "unknown name", umask => undef, owner => undef, group => undef, binmode => undef, utf8 => undef, recreate => 0, recreate_check_interval => 30, recreate_check_signal => undef, recreate_pid_write => undef, create_at_logtime => 0, header_text => undef, @options }; if($self->{create_at_logtime}) { $self->{recreate} = 1; } if(defined $self->{umask} and $self->{umask} =~ /^0/) { # umask value is a string, meant to be an oct value $self->{umask} = oct($self->{umask}); } if(defined $self->{header_text} && $self->{header_text} !~ /\n\Z/){ $self->{header_text} .= "\n"; } die "Mandatory parameter 'filename' missing" unless exists $self->{filename}; bless $self, $class; # This will die() if it fails $self->file_open() unless $self->{create_at_logtime}; return $self; } ################################################## sub file_open { ################################################## my($self) = @_; my $arrows = ">"; my $mode = (O_CREAT|O_RDWR); my $old_umask = umask(); umask($self->{umask}) if defined $self->{umask}; $self->{tied}=tie(@{$self->{log}}, 'Tie::File', $self->{filename}, mode => $mode); if( -f $self->{filename} and ( defined $self->{owner} or defined $self->{group} ) ) { eval { $self->perms_fix() }; if($@) { # Cleanup and re-throw $self->file_close(); die $@; } } if($self->{recreate}) { $self->{watcher} = Log::Log4perl::Config::Watch->new( file => $self->{filename}, (defined $self->{recreate_check_interval} ? (check_interval => $self->{recreate_check_interval}) : ()), (defined $self->{recreate_check_signal} ? (signal => $self->{recreate_check_signal}) : ()), ); } umask($old_umask) if defined $self->{umask}; if (defined $self->{binmode}) { binmode $self->{tied}->{fh}, $self->{binmode}; } if (defined $self->{utf8}) { binmode $self->{tied}->{fh}, ":utf8"; } } ################################################## sub file_close { ################################################## my($self) = @_; if(defined $self->{tied}) { delete($self->{tied}); untie(@{$self->{log}}); delete($self->{log}); } } ################################################## sub file_switch { ################################################## my($self, $new_filename) = @_; print "Switching file from $self->{filename} to $new_filename\n" if _INTERNAL_DEBUG; $self->file_close(); $self->{filename} = $new_filename; $self->file_open(); } ################################################## sub log { ################################################## my($self, %params) = @_; if($self->{recreate}) { if($self->{recreate_check_signal}) { if($self->{watcher}->{signal_caught}) { $self->{watcher}->{signal_caught} = 0; $self->file_switch($self->{filename}); } } else { if(!$self->{watcher} or $self->{watcher}->file_has_moved()) { $self->file_switch($self->{filename}); } } } shift(@{$self->{log}}) if($self->{max_lines} && defined $self->{header_text}); push(@{$self->{log}},$params{message}); shift(@{$self->{log}}) while($self->{max_lines} && @{$self->{log}}>$self->{max_lines}); unshift(@{$self->{log}}, $self->{header_text}) if($self->{max_lines} && defined $self->{header_text}); } ################################################## sub DESTROY { ################################################## $_[0]->file_close(); } 1;