Thread Logfiles rollen (7 answers)
Opened by Relais at 2009-05-04 09:40

GwenDragon
 2009-05-04 10:09
#121108 #121108
User since
2005-01-17
14759 Artikel
Admin1
[Homepage]
user image
Log rollen? Du meinst rotieren?

Kommt immer darauf an wie ich Logfiles erstelle.

Schnippsel fürs Rotieren:
Code (perl): (dl )
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
my $MAXSIZE = 1024*1024;
my $DEBUG = 0;

sub RotateLog {
    my $filename = shift;
    my $MAXCYCLE = shift || 10;

    if ( -e "$filename.$MAXCYCLE" ) {
        foreach
          my $gz ( '/bin/gzip', '/usr/bin/gzip', '/usr/local/bin/gzip', undef )
        {
            last if ( not defined $gz );
            if ( -x $gz ) {
                system("$gz -c $filename.$MAXCYCLE >> $filename.gz");
                last;
            }
        }
    }

    for ( my $s = $MAXCYCLE - 1 ; $s >= 0 ; $s-- ) {
        my $oldname = $s ? "$filename.$s" : $filename;
        my $newname = join( ".", $filename, $s + 1 );

        if ( -e $oldname ) {
            warn("RotateLog: $oldname--\>$newname\n") if $DEBUG;
            rename( $oldname, $newname )
              or warn("RotateLog: $oldname not renamed to $newname\n");
        }
    }
        
        1;
}
...
# irgendwo im Skript
        my ($fsz) = (stat($logfile))[7];
        RotateLog($logfile) if ( $fsz >$MAXSIZE ); 


//EDIT: falsche Variable aus Code genommen; typisch wenn eine eben mal Strg C - V aus eigenen Codemodulen macht. ;)
Last edited: 2009-05-04 10:18:55 +0200 (CEST)

View full thread Logfiles rollen