Splitting a large Apache log file with PHP

[code language=’php’]
$months = array(
  ‘Jul’ => ’07’,
  ‘Aug’ => ’08’,
  ‘Sep’ => ’09’
);

$open_file = false;
$fw = false;

$fp = fopen(‘filename.log’, ‘r’);

while($line = fgets($fp)) {
  preg_match(‘:([0-9]{2})/(Jul|Aug|Sep):’, $line, $matches);
  // $matches[1] = day of month
  // $matches[2] = month name
  $filename = “filename.log.2007{$months[$matches[2]]}{$matches[1]}”;
  if($filename != $open_file) {
    if($fw) {
      fclose($fw);
    }
    $fw = fopen($filename, ‘a’);
    $open_file = $filename;
    echo “$filename\n”;
  }
  fputs($fw, $line);
}

fclose($fw);
fclose($fp);
[/code]

wordpress / gentoo / apache / mod_rewrite 的設定

<VirtualHost *:80>
  ServerName toy.monster.tw
  ServerAdmin [email protected]
  DocumentRoot “/var/www/toy.monster.tw”
  CustomLog /var/www/toy.monster.tw/log/access_log.txt combined

  <Directory “/var/www/toy.monster.tw”>
    Options FollowSymLinks
    AllowOverride All
    # AllowOverride controls what directives may be placed in .htaccess files.
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

Read more