php-log: Process apache log files with PHP

I’ve recently had to quickly parse an Apache log file, to get basic usage statistics for a site over a month and wanted the ability to quickly process any general logs and store them in a MySQL table, so I’ve made a small package class.log.

Included in the package are:

  • class.log.php (the main class)
  • class.log.processor.php (an interface for a log processor, very basic!!)
  • class.log.output.php (a simple extension of the processor (outputs <p> tags with <span>s)
  • class.log.mysql.php (a simple mysql importer of the processed data)
  • parse.php (a simple implementation, bringing all the classes together)

The class is designed to use one line at a time from the log retrieved, and the regular expression specified in class.log.php can be modified to parse different types of logs as long as the matches array is also updated.

I’ve only used this for apache logs currently, which it managed quite well, I’m not sure if I’d use this script in an automated script, but I’ll leave that for you to decide.

As other scripts, this class is available under the Creative Commons Share Alike Attribution license.

Edit: Minor update, made class more generic and added more comments.

Download (3.7kB)

5 Replies to “php-log: Process apache log files with PHP”

  1. Great work, although may I suggest an improvement to your regular expression.

    The pattern expects a number after the response code, however this sometimes can be a hyphen, essentially when the served response had a length of 0. Which can be the case for request type HEAD but also seems to occur on redirects.

    Thus in your pattern, I’d suggest to replace

    Hope that helps,


    1. Hi there,

      Thanks for your comment! That’s a great point, I might even change it slightly to \S (which is the same as [^\s] I think…) but well spotted. The main apache log processing was mostly an example of the interface, but I have ended up using it to extrapolate 404 data.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.