整个示例由 perishablepress.com 提供。这不是我自己的工作。我只是提供来源。它主要用于 PHP 错误,但您可以轻松更改它以保存您的 JS 错误。
https://perishablepress.com/ajax-error-log/
工作示例(在新窗口中打开):
https://perishablepress.com/tools/ajaxlog/
提供的链接中解释的步骤来解决您的问题:
步骤1
创建您的数据库。
第2步
创建错误记录页面:
<?php
/*
Dynamic Error Logging by Jeff Starr @ Perishable Press
Project URL: https://perishablepress.com/ajax-error-log/
License: GNU General Public License
Version: 1.1
*/
// database credentials
$hostname = "hostname";
$database = "database";
$username = "username";
$password = "password";
// site URL (no trailing slash!)
$website = "http://example.com";
// send proper headers for 404
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
// set some server variables
$logtime = date("F jS Y, h:ia", time() - 10800);
$request = $site . $_SERVER['REQUEST_URI'];
$referer = $_SERVER['HTTP_REFERER'];
$qstring = $_SERVER['QUERY_STRING'];
$address = $_SERVER['REMOTE_ADDR'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$error = "404"; // for other errors
// connect to database
$dbcon = mysql_connect($hostname, $username, $password)
or die("Couldn't connect to SQL Server on $dbhost");
$selected = mysql_select_db($database, $dbcon)
or die("Couldn't open database $database");
// sanitize server variables
$logtime_esc = mysql_real_escape_string($logtime);
$request_esc = mysql_real_escape_string($request);
$referer_esc = mysql_real_escape_string($referer);
$qstring_esc = mysql_real_escape_string($qstring);
$address_esc = mysql_real_escape_string($address);
$agent_esc = mysql_real_escape_string($agent);
// insert record and close database
$query = "INSERT INTO `error_log` (logtime,request,referer,qstring,address,agent,error) ";
$query .= "VALUES ('$logtime_esc','$request_esc','$referer_esc','$qstring_esc','$address_esc','$agent_esc','$error')";
mysql_query($query);
mysql_close($dbcon);
// and finally output the html
?><html>
<head><title>404 - Not Found</title></head>
<body id="error-404">
<div class="left">
<div class="post">
<h1>Error 404 — Not Found</h1>
<p>The requested resource was not found on this server.</p>
<div class="content">
<p><?php echo "Requested URL: " . $website . $request_esc; ?></p>
</div>
</div>
</div>
</body>
</html>
第 3 步
“下一步是指示服务器使用自定义 404.php 文件而不是默认错误页面。使用 Apache,只需将一行添加到站点的根 .htaccess 文件即可:”
ErrorDocument 404 /ajax-error-log/404.php
第4步
验证功能
第 5 步
创建动态日志
<?php
/*
Dynamic Error Logging by Jeff Starr @ Perishable Press
Project URL: https://perishablepress.com/ajax-error-log/
License: GNU General Public License
Version: 1.1
*/
// database credentials
$hostname = "hostname";
$database = "database";
$username = "username";
$password = "password";
// site URL (no trailing slash!)
$website = "http://example.com";
// connect to server
$dbhandle = mysql_connect($hostname, $username, $password)
or die('Could not connect to SQL Server on $hostname');
// select the database
$selected = mysql_select_db($database, $dbhandle)
or die('Could not open database $database');
// create the sql query
$query = 'SELECT id,logtime,request,referer,qstring,address,agent,error
FROM error_log ORDER BY id DESC LIMIT 50'; // defaults to 50 entries
// execute query and return records
$result = mysql_query($query);
$number = mysql_num_rows($result);
// display the results
while($row = mysql_fetch_array($result)) {
echo '<div class="log-entry"><pre>';
echo 'Error: '.htmlspecialchars($row['error']);
echo ' - '.htmlspecialchars($row['logtime'])."\n";
echo 'Request: '.$website.htmlspecialchars($row['request'])."\n";
echo 'User Agent: '.htmlspecialchars($row['agent'])."\n";
echo 'IP Address: '.htmlspecialchars($row['address']);
echo '</pre></div>';
}
//close db connection
mysql_close($dbhandle);
?>
第 6 步
创建动态日志页面
最后,创建一个名为“AjaxErrorLog.html”的文件并插入以下代码:
<!DOCTYPE HTML>
<html>
<head>
<title>Ajax Error Log</title>
<!-- Ajax Error Log @ https://perishablepress.com/ajax-error-log/ -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
pre {
font: 10px/1.5 Courier, "Courier New", mono;
background-color: #efefef; border: 1px solid #ccc;
width: 700px; margin: 7px; padding: 10px;
white-space: pre-wrap;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#results").load("AjaxErrorLog.php");
var refreshId = setInterval(function() {
$("#results").load("AjaxErrorLog.php").fadeIn("slow");
}, 2000); // refresh time (default = 2000 ms = 2 seconds)
});
</script>
</head>
<body>
<noscript><div id="response"><h1>JavaScript is required for this demo.</h1></div></noscript>
<div id="results"></div>
</body>
</html>
“这是将在浏览器中动态显示结果的页面。这是一个基本的网页,它使用一点 jQuery/Ajax 魔法来加载 AjaxErrorLog.php 文件的输出并每两秒刷新一次结果(如在代码为“2000”)。随意使用您希望的任何刷新间隔,只要记住这些是服务器请求,所以不要太疯狂。”