PHP Apache Access Log analysis tool split Word interleaved CSV file and insert MySQL database analysis

Source: Internet
Author: User
Tags apache access log

What if I need to analyze the access log now?

Like analyzing D:\Servers\Apache2.2\logs\access2014-05-22.log .

http://my.oschina.net/cart/ developed a gadget to analyze Apache logs, split the word interleaved CSV file and insert MySQL database analysis for this issue

<?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". " \ r \ n ",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].‘"‘." \ r \ n ",file_append);}

Finally, in MySQL, according to the CSV field information to establish the field, through the MySQL CSV using LOAD DATA import your CSV file.

Import 2G of CSV log, the speed is very fast!!!

All rights reserved:http://my.oschina.net/cart/

  Haha, http://my.oschina.net/cart/log has been successfully split into the database, the remaining how to screen, how to toss http://my.oschina.net/cart/, with your intention! Ha ha!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.