Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

rotatelogs

  • Loading branch information...
commit 6509e8357005d4e0cdb1df73a21aa6772acbddf0 1 parent 53c4aef
Masahiro Nagano authored
1  .gitignore
@@ -9,3 +9,4 @@ Makefile.old
9 9 nytprof.out
10 10 MANIFEST.bak
11 11 *.sw[po]
  12 +*~
22 lib/File/RotateLogs.pm
@@ -20,7 +20,7 @@ has 'linkname' => (
20 20 is => 'ro',
21 21 isa => 'Str',
22 22 required => 0,
23   -)
  23 +);
24 24
25 25 has 'rotationtime' => (
26 26 is => 'ro',
@@ -64,7 +64,7 @@ sub print {
64 64 }
65 65
66 66 unless ($fh) {
67   - my $is_new = ( ! -f $fname || ! -l $self->filename ) ? 1 : 0;
  67 + my $is_new = ( ! -f $fname || ( $self->linkname && ! -l $self->linkname ) ) ? 1 : 0;
68 68 open $fh, '>>:unix', $fname or die "Cannot open file($fname): $!";
69 69 if ( $is_new ) {
70 70 eval {
@@ -88,7 +88,7 @@ sub rotation {
88 88 my $lock = $fname .'_lock';
89 89 sysopen(my $lockfh, $lock, O_CREAT|O_EXCL) or return;
90 90 close($lockfh);
91   - if ( $sefl->linkname ) {
  91 + if ( $self->linkname ) {
92 92 my $symlink = $fname .'_symlink';
93 93 symlink($fname, $symlink) or die $!;
94 94 rename($symlink, $self->linkname) or die $!;
@@ -101,21 +101,29 @@ sub rotation {
101 101
102 102 my $time = time;
103 103 my @to_unlink = grep { $time - [stat($_)]->[9] > $self->maxage }
104   - glob($self->filename . '.*');
  104 + glob($self->logfile_pattern);
105 105 if ( ! @to_unlink ) {
106   - unlink $symlock;
  106 + unlink $lock;
107 107 return;
108 108 }
109 109
110 110 if ( $self->sleep_before_remove ) {
111   - $self->unlink_background(@to_unlink,$symlock);
  111 + $self->unlink_background(@to_unlink,$lock);
112 112 }
113 113 else {
114 114 unlink $_ for @to_unlink;
115   - unlink $symlock;
  115 + unlink $lock;
116 116 }
117 117 }
118 118
  119 +sub logfile_pattern {
  120 + my $self = shift;
  121 + my $logfile = $self->logfile;
  122 + $logfile =~ s!%[%+A-Za-z]!*!g;
  123 + $logfile =~ s!\*+!*!g;
  124 + $logfile;
  125 +}
  126 +
119 127 sub unlink_background {
120 128 my ($self, @files) = @_;
121 129 my $daemon = Proc::Daemon->new();
32 lib/File/RotateLogs.pm~
... ... @@ -1,32 +0,0 @@
1   -package File::RotateLogs;
2   -use strict;
3   -use warnings;
4   -our $VERSION = '0.01';
5   -
6   -1;
7   -__END__
8   -
9   -=head1 NAME
10   -
11   -File::RotateLogs -
12   -
13   -=head1 SYNOPSIS
14   -
15   - use File::RotateLogs;
16   -
17   -=head1 DESCRIPTION
18   -
19   -File::RotateLogs is
20   -
21   -=head1 AUTHOR
22   -
23   -Masahiro Nagano E<lt>kazeburo {at} gmail.comE<gt>
24   -
25   -=head1 SEE ALSO
26   -
27   -=head1 LICENSE
28   -
29   -This library is free software; you can redistribute it and/or modify
30   -it under the same terms as Perl itself.
31   -
32   -=cut
106 t/01_basic.t
... ... @@ -0,0 +1,106 @@
  1 +use strict;
  2 +use warnings;
  3 +use File::RotateLogs;
  4 +use File::Temp qw/tempdir/;
  5 +use Test::More;
  6 +
  7 +my $tempdir = tempdir(CLEANUP=>1);
  8 +
  9 +my $rotatelogs = File::RotateLogs->new(
  10 + logfile => $tempdir.'/test_log.%Y%m%d%H%M%S',
  11 + linkname => $tempdir.'/test_log',
  12 + rotationtime => 2,
  13 + maxage => 4,
  14 + sleep_before_remove => 2,
  15 +);
  16 +
  17 +ok($rotatelogs);
  18 +
  19 +$rotatelogs->print("foo\n");
  20 +{
  21 + my $c=0;
  22 + my $link;
  23 + my $linkf;
  24 + my $log;
  25 + my $logf;
  26 + for my $f ( glob($tempdir.'/test_log*') ) {
  27 + $c++;
  28 + if ( -l $f ) {
  29 + $link++;
  30 + $linkf = $f;
  31 + }
  32 + if ( -f $f && ! -l $f ) {
  33 + $log++;
  34 + $logf = $f;
  35 + }
  36 + }
  37 + is($c,2);
  38 + is($link,1);
  39 + is($log,1);
  40 + is($logf, readlink($linkf));
  41 +}
  42 +
  43 +sleep(3);
  44 +$rotatelogs->print("bar\n");
  45 +{
  46 + my $c=0;
  47 + my $link;
  48 + my $linkf;
  49 + my $log;
  50 + my $logf;
  51 + for my $f ( sort { $a cmp $b } glob($tempdir.'/test_log*') ) {
  52 + $c++;
  53 + if ( -l $f ) {
  54 + $link++;
  55 + $linkf = $f;
  56 + }
  57 + if ( -f $f && ! -l $f ) {
  58 + $log++;
  59 + $logf = $f
  60 + }
  61 + }
  62 + is($c,3);
  63 + is($link,1);
  64 + is($log,2);
  65 + is($logf, readlink($linkf));
  66 +}
  67 +
  68 +sleep(2);
  69 +$rotatelogs->print("baz\n");
  70 +sleep(1);
  71 +{
  72 + my $c=0;
  73 + my $link;
  74 + my $log;
  75 + my $lock;
  76 + for my $f ( glob($tempdir.'/test_log*') ) {
  77 + $c++;
  78 + $link++ if -l $f;
  79 + $log++ if -f $f && ! -l $f;
  80 + $lock++ if $f =~ m!lock$!;
  81 + }
  82 + is($c,5);
  83 + ok($link);
  84 + is($log,4);
  85 + ok($lock);
  86 +}
  87 +
  88 +sleep(2);
  89 +{
  90 + my $c=0;
  91 + my $link;
  92 + my $log;
  93 + my $lock;
  94 + for my $f ( glob($tempdir.'/test_log*') ) {
  95 + $c++;
  96 + $link++ if -l $f;
  97 + $log++ if -f $f && ! -l $f;
  98 + $lock++ if $f =~ m!lock$!;
  99 + }
  100 + is($c,3);
  101 + ok($link);
  102 + is($log,2);
  103 + ok(!$lock);
  104 +}
  105 +
  106 +done_testing();
6 xt/01_podspell.t
@@ -9,3 +9,9 @@ __DATA__
9 9 Masahiro Nagano
10 10 kazeburo {at} gmail.com
11 11 File::RotateLogs
  12 +Filename
  13 +filename
  14 +linkname
  15 +maxage
  16 +plack
  17 +rotationtime

0 comments on commit 6509e83

Please sign in to comment.
Something went wrong with that request. Please try again.