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]

Comments are closed.

Related URL:
  1. amazon aws ami LAMP / NGINX / PHP / memcache steps
  2. 很強的 Message Broker : ActiveMQ
  3. Protected: 觀察 22:40 – 23:15 apache log 的變化
  4. 解決用 NFS 當 documentroot 時會當掉的方法
  5. debian / ubuntu 裝 oracle instant client / sqlplus / oci8 / apache2 config / steps by steps
  6. [ubuntu] apache server module 的 enable / disable
  7. apache Custom Log Formats – 好用的參數
  8. Simple Monster Tracking System – step by step
  9. Protected: apache mod_rewrite , rewrite , rewriterule 真難搞
  10. 好用的 iptables rules – 限制 client 連 http port 的次數