PHP Apache Access Log 分析工具 拆分字段成CSV文件并插入Mysql数据库分析

现在需要分析访问日志,怎么办?

比如分析D:ServersApache2.2logsaccess2014-05-22.log

http://my.oschina.net/cart/针对这个问题特意开发了一款小工具分析Apache 日志,拆分字段成CSV文件并插入Mysql数据库分析

<?php
$date = '2014-05-22';

file_put_contents($date.'.csv', '');
file_put_contents($date.'.csv', '"IP","Date","Zone","Protocol","URL","Version","Status","Size","Referer","User-Agent"'."
", FILE_APPEND);
preg_match_all('/(.*?) .*? .*? [(.*?) (.*?)] "(.*?) (.*?) (.*?)" (.*?) (.*?) "(.*?)" "(.*?)"/isu', file_get_contents('../Servers/Apache2.2/logs/access'.$date.'.log'), $matches);

$num = count($matches[0]);
for($i = 0; $i < $num; $i ++){
    file_put_contents($date.'.csv', '"'.trim($matches[1][$i]).'","'.$matches[2][$i].'","'.$matches[3][$i].'","'.$matches[4][$i].'","'.$matches[5][$i].'","'.$matches[6][$i].'","'.$matches[7][$i].'","'.$matches[8][$i].'","'.$matches[9][$i].'","'.$matches[10][$i].'"'."
", FILE_APPEND);
}

最后在Mysql中按照CSV字段信息建立字段,通过Mysql的 CSV using LOAD DATA 导入你的CSV文件即可。

导入2G的CSV日志,速度都很快!!!

版权所有:http://my.oschina.net/cart/ 

 哈哈,http://my.oschina.net/cart/日志已经成功拆成列入到数据库了,剩下怎么筛选,怎么折腾http://my.oschina.net/cart/,随你意!哈哈!

原文地址:https://www.cnblogs.com/hubing/p/3745893.html