Created
June 13, 2013 15:41
-
-
Save meeuw/5774746 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// create table error_log(lastseen varchar(50), message text, count integer, unique (message), unique (lastseen,message)); | |
$month_translation = array("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12); | |
$error_log_format = "/\[(?<weekday>Mon|Tue|Wed|Thu|Fri|Sat|Sun) ". | |
"(?<month>Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ". | |
"(?<monthday>\d\d) ". | |
"(?<hour>\d\d):". | |
"(?<minutes>\d\d):". | |
"(?<seconds>\d\d).". | |
"(?<milliseconds>\d+) ". | |
"(?<year>\d\d\d\d)\] ". | |
"\[(?<class>[^\]]*)\] ". | |
"\[(?<pid>[^\]]*)\] ". | |
"\[(?<client>[^\]]*)\] ". | |
"(?<message>.*)/"; | |
//echo "$error_log_format\n"; exit(); | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
$db = new SQLite3('db/error_log.sqlite3'); | |
if (PHP_SAPI == 'cli') { | |
$f = fopen('/var/log/httpd/error_log', 'r'); | |
$entry = NULL; | |
$db->exec('BEGIN TRANSACTION;'); | |
while (($buffer = fgets($f, 4096)) !== false) { | |
if (preg_match($error_log_format, $buffer, $matches)) { | |
if (strncmp($matches["message"], 'PHP ', 4) == 0) { | |
if ( | |
strncmp($matches["message"], 'PHP Notice:', 11) == 0 || | |
strncmp($matches["message"], 'PHP Warning:', 12) == 0 || | |
strncmp($matches["message"], 'PHP Parse error:', 16) == 0 || | |
strncmp($matches["message"], 'PHP Fatal error:', 16) == 0 | |
) { | |
if ($entry) { | |
$lastseen = sprintf("%d-%02d-%02dT%02d:%02d:%02d.%d", $entry[0], $entry[1], $entry[2], $entry[3], $entry[4], $entry[5], $entry[6]); | |
$stmt = $db->prepare("SELECT COUNT(*) FROM error_log WHERE (strftime('%s', :lastseen) <= strftime('%s', lastseen) AND message = :message)"); | |
$stmt->bindValue(':lastseen', $lastseen); | |
$stmt->bindValue(':message', str_replace('\n', "\n", $entry[8])); | |
if ($stmt->execute()->fetchArray()[0] == 1) continue; | |
$stmt = $db->prepare("INSERT INTO error_log VALUES (:lastseen, :message, 1)"); | |
$stmt->bindValue(':lastseen', $lastseen); | |
$stmt->bindValue(':message', str_replace('\n', "\n", $entry[8])); | |
if (@$stmt->execute() === FALSE) { | |
$stmt = $db->prepare("UPDATE error_log SET count=count+1, lastseen=:lastseen WHERE message=:message"); | |
$stmt->bindValue(':message', str_replace('\n', "\n", $entry[8])); | |
$stmt->bindValue(':lastseen', $lastseen); | |
$stmt->execute(); | |
} | |
} | |
$entry = array($matches["year"], $month_translation[$matches["month"]], $matches["monthday"], $matches["hour"], $matches["minutes"], $matches["seconds"], $matches["milliseconds"], "notice", ""); | |
} | |
$entry[8] .= "\n".$matches["message"]; | |
} else error_log("This isn't a PHP error: $buffer"); | |
} else error_log("I don't understand: $buffer"); | |
} | |
$db->exec('COMMIT;'); | |
} else { | |
?><html><head></head><body><?php | |
$offset = @$_REQUEST['offset']; | |
$limit = @$_REQUEST['limit']; | |
if (!is_numeric($offset)) $offset = 0; | |
if (!is_numeric($limit)) $limit = 10; | |
if($_POST) { | |
foreach ($_POST as $key => $value) { | |
if (strncmp($key, 'fixed_', 6) == 0) { | |
$stmt = $db->prepare("UPDATE error_log SET count=0 WHERE rowid = :rowid"); | |
$stmt->bindValue(':rowid', substr($key, 6)); | |
$stmt->execute(); | |
header("Location: error_log.php?offset=$offset&limit=$limit"); | |
} | |
} | |
} else { | |
?><a href="?offset=<?php echo $offset + $limit; ?>">next</a> | |
<form action="error_log.php?offset=<?php echo $offset; ?>&limit=<?php echo $limit; ?>" method="post"> | |
<table border="1"><?php | |
$stmt = $db->prepare("SELECT rowid, count, lastseen, message FROM error_log WHERE count != 0 LIMIT :offset, :limit"); | |
$stmt->bindValue(':offset', $offset); | |
$stmt->bindValue(':limit', $limit); | |
$result = @$stmt->execute(); | |
echo "<tr><th>fixed</th><th><a href=\"?sort=count\">count</a></th><th>date</th><th>message</th></tr>"; | |
while($res = $result->fetchArray(SQLITE3_NUM)){ | |
echo "<tr><td><input type='submit' name='fixed_{$res[0]}' value='fixed'/></td><td>{$res[1]}</td><td><pre>{$res[2]}</pre></td><td><pre>{$res[3]}</pre></td></tr>"; | |
} | |
?></form></table><?php | |
} | |
?></body></html><?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment