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.