我发现解决此问题的最简单/快捷的方法是使用脚本读取apache的日志并每天发送带有摘要的邮件。
仅供参考:服务器的设置每天都有日志轮换,请在轮换时间之前通过crontab运行脚本。(加号:我是法语,所以发送的邮件中有法语单词)。
这是脚本:(我知道可以进行改进,但是仍然可以完成所需的工作)
<?php
$start = microtime(true);
$filepath = "PATH/TO/APACHE/LOGS";
$filename = "error.log";
$to = "MAILTO@ME.COM";
$file = file($filepath . $filename);
$warnings = [];
$errors = [];
$notices = [];
foreach($file as $line) {
$error = strpos($line, "PHP Fatal error");
if ($error !== false) {
$exp = explode("PHP Fatal error", $line);
$exp2 = explode(", referer", $exp[1]);
$final = "PHP Fatal error" . $exp2[0];
isset($errors[$final]) ? $errors[$final]+= 1 : $errors[$final]= 1;
} else {
$warning = strpos($line, "PHP Warning");
if ($warning !== false) {
$exp = explode("PHP Warning", $line);
$exp2 = explode(", referer", $exp[1]);
$final = "PHP Warning" . $exp2[0];
isset($warnings[$final]) ? $warnings[$final]+= 1 : $warnings[$final]= 1;
} else {
$notice = strpos($line, "PHP Notice");
if ($notice !== false) {
$exp = explode("PHP Notice", $line);
$exp2 = explode(", referer", $exp[1]);
$final = "PHP Notice" . $exp2[0];
isset($notices[$final]) ? $notices[$final]+= 1 : $notices[$final]= 1;
}
}
}
}
$errorNb = countArray($errors);
$warningNb = countArray($warnings);
$noticeNb = countArray($notices);
function countArray($ar) {
$nb = 0; foreach($ar as $a){ $nb+=$a; } return $nb;
}
$subject = "Rapport apache : Erreurs : " . $errorNb . " - Warnings : " . $warningNb . " - Notices :" .$noticeNb;
$message = "<br>Liste des erreurs (PHP Fatal error) rencontrées :"."<br>";
foreach($errors as $i => $e) {
$message .= "Rencontré " . $e . " fois : " . $i . "<br>";
}
$message .= "<br>"."<br>Liste des warning (PHP Warning) rencontrées :"."<br>";
foreach($warnings as $i => $e) {
$message .= "Rencontré " . $e . " fois : " . $i . "<br>";
}
$message .= "<br>"."<br>Liste des erreurs (PHP Notice) rencontrées :"."<br>";
foreach($notices as $i => $e) {
$message .= "Rencontré " . $e . " fois : " . $i . "<br>";
}
//echo $subject . "<br>" . "<br>";
$message.= "<br><br><br><br>Rapport généré en " . (microtime(true) - $start) . " secondes";
//echo $message;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";