<?xml version="1.0" encoding="windows-1252"?>
<node id="17704" title="Apache log splitter/compressor" created="2000-06-12 14:06:48" updated="2005-08-11 02:36:40">
<type id="1748">
sourcecode</type>
<author id="5549">
lhoward</author>
<data>
<field name="doctext">
&lt;code&gt;
#!/usr/bin/perl -w

use strict;

use Time::ParseDate;
use Time::CTime;
use Compress::Zlib;

my $out_dir=$ENV{LOG_DIR};

my $cur_str='';
my $outlog;

my $line;

$SIG{TERM}= sub {
  #handle apache closing by closing the .gz file properly;
  if(defined $outlog){
    $outlog-&gt;gzclose();
    undef $outlog;
  }
};

  while (defined($line=&lt;STDIN&gt;)){
    chomp $line;
    if($line=~/\[(\d+)\/(\w+)\/(\d+)\:/){
      #lines must contain an extractable date
      my ($dd,$mm,$yy)=($1,$2,$3);
      $dd=~s/^0//g;
      my $str="$dd $mm $yy";
      if($str ne $cur_str){
	# new day, rotate to next file
        $cur_str=$str;
        my $dt=parsedate($str);
        my $ts=strftime("%Y_%m_%d",localtime($dt));
        $ts=~s/\s/0/g;
        # close current log (If necessary)
        $outlog-&gt;gzclose() if defined $outlog;
	#find a unique name for the new log	
        my $ct=1;
        my $fn="$out_dir$ts.0.gz";
        while(-f $fn){
          $fn="$out_dir$ts.$ct.gz";
          $ct++;
        }
        $outlog=gzopen($fn,"a");
      }
      $outlog-&gt;gzwrite($line."\n");
    }else{
      # unrecognized line.... there should probably be some error checking here
    }
  }

  if(defined $outlog){
    $outlog-&gt;gzclose();
  }
&lt;/code&gt;
</field>
<field name="codedescription">
This program is designed to read from an apache logfile pipe.
It automatically compresses the data and splits it into
files each containg one day's worth of data.  The
directory to write the log files to should be set
in the environmental variable LOG_DIR.  Log files
are named access_log_YYYY_MM_DD.N.gz.
Here is how I have apache configured to call this program.
&lt;p&gt;&lt;tt&gt;
CustomLog |/path/to/websplit.pl combined
&lt;/tt&gt;</field>
<field name="codecategory">
Web Stuff</field>
<field name="codeauthor">
Les Howard
les@lesandchris.com
</field>
</data>
</node>
