I want to create a script that parses or makes sense of apache's error log to see what the most recent error was. I was wondering if anyone out there has something that does this or has any ideas where to start?
There are a few things to consider first:
However, if none of these apply, you can use the normal file reading commands to do it. The easiest way to get the last error is
$contents = @file('/path/to/error.log', FILE_SKIP_EMPTY_LINES);
if (is_array($contents)) {
echo end($contents);
}
unset($contents);
There's probably a better way of doing this that doesn't oink up memory, but I'll leave that as an exercise for the reader.
One last comment: PHP also has an ini setting to redirect PHP errors to a log file: error_log = /path/to/error.log
You can set this in httpd.conf or in an .htaccess file (if you have access to one) using the php_flag notation:
php_flag error_log /web/mysite/logs/error.log
for anyone else looking for a sample script, i threw something together, it's got the basics:
<?php
exec('tail /usr/local/apache/logs/error_log', $output);
?>
<Table border="1">
<tr>
<th>Date</th>
<th>Type</th>
<th>Client</th>
<th>Message</th>
</tr>
<?
foreach($output as $line) {
// sample line: [Wed Oct 01 15:07:23 2008] [error] [client 76.246.51.127] PHP 99. Debugger->handleError() /home/gsmcms/public_html/central/cake/libs/debugger.php:0
preg_match('~^\[(.*?)\]~', $line, $date);
if(empty($date[1])) {
continue;
}
preg_match('~\] \[([a-z]*?)\] \[~', $line, $type);
preg_match('~\] \[client ([0-9\.]*)\]~', $line, $client);
preg_match('~\] (.*)$~', $line, $message);
?>
<tr>
<td><?=$date[1]?></td>
<td><?=$type[1]?></td>
<td><?=$client[1]?></td>
<td><?=$message[1]?></td>
</tr>
<?
}
?>
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With