我正在开发一个 php 应用程序,并为它创建了日志记录功能。我的意思是每次出现问题时,我都会将其写入日志文件中,因此如果用户报告错误,我可以使用我的日志文件来跟踪该错误。
问题是如果我在生产服务器上上传我的项目,我必须禁用 PHP 的默认错误显示;我只想在我自己的日志文件中记录 php 错误以及网络服务器的“error.log”。有什么办法让我可以做到吗?
我正在开发一个 php 应用程序,并为它创建了日志记录功能。我的意思是每次出现问题时,我都会将其写入日志文件中,因此如果用户报告错误,我可以使用我的日志文件来跟踪该错误。
问题是如果我在生产服务器上上传我的项目,我必须禁用 PHP 的默认错误显示;我只想在我自己的日志文件中记录 php 错误以及网络服务器的“error.log”。有什么办法让我可以做到吗?
您可以使用自定义错误处理程序在 php.ini 中进行自己的日志记录。这也将允许您登录到数据库(假设错误与数据库无关并且您有有效的连接)或基于错误的平面文件,并设置自定义错误页面。看:
对于 FEATURED USABLE EXCEPTIONS(我正在调用全功能的可用异常,(没有堆栈跟踪)):
如果您是开发人员,请设置为:
define('EXCEPTION_HANDLER_ACTIVE', true);
在生产中,这也应该是正确的,但修改了代码以不向最终用户显示错误。(使用:error_reporting(0); ini_set('display_errors', false)
在页面顶部));
首先设置处理异常的main函数:
set_error_handler("_handler_exception");
调用异常处理函数:
function _handler_exception($err_severity, $err_msg, $err_filepath, $err_line)
{
if (EXCEPTION_HANDLER_ACTIVE == 1)
{
$eh = new exceptionhandler;
$eh->exception_active = true;
$eh->set_exception_handler_log($err_severity, $err_msg, $err_filepath, $err_line);
}
else
{
$eh = new exceptionhandler;
$eh->exception_active = false;
$eh->set_exception_handler_log($err_severity, $err_msg, $err_filepath, $err_line);
}
}
类异常处理程序:
class exceptionhandler
{
public $exception_active = true;
public function set_exception_handler_log($err_severity, $err_msg, $err_filepath, $err_line)
{
$ret_err_severity = NULL;
$ret_err_no = NULL;
$ret_info = NULL;
$filenameerr = NULL;
$dbout = NULL;
switch ($err_severity)
{
case E_ERROR:
$ret_err_severity = "E_ERROR"." [ ".$err_severity." ]";
$ret_err_no = "1";
$ret_info = "Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.";
$ret_err_msg = $err_msg;
break;
case E_STRICT:
$ret_err_severity = "E_STRICT"." [ ".$err_severity." ]";
$ret_err_no = "2048";
$ret_info = "Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. ";
$ret_err_msg = $err_msg;
break;
default:
$ret_err_severity = "CODE"." [ ".$err_severity." ]";
$ret_err_no = $err_severity;
$ret_info = "Other error";
$ret_err_msg = $err_msg;
break;
}
$ef = explode("/", $err_filepath);
$ec = (count($ef) - 1);
$filenameerr = $ef[$ec];
$backtrace = debug_backtrace();
if (!isset($doc_root)) {
$doc_root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
}
$debug_backtrace_parse = print_r(debug_backtrace(), true);
$debug_backtrace2 = str_replace($err_msg, '<b>'.$err_msg.'</b>', $debug_backtrace_parse);
$debug_backtrace = str_replace($err_filepath, '<b>'.$err_filepath.'</b>', $debug_backtrace2);
$line = (isset($backtrace[1]['line'])) ? htmlspecialchars($backtrace[1]['line']) : '';
$file = (isset($backtrace[1]['file'])) ? htmlspecialchars(str_replace(array('\\', $doc_root), array('/', ''), $backtrace[1]['file'])) : '';
$class = !empty($backtrace[2]['class']) ? htmlspecialchars($backtrace[2]['class']) . '::' : '';
$function = !empty($backtrace[2]['function']) ? htmlspecialchars($backtrace[2]['function']) . '() ' : '';
$dbout = "<pre>$class$function =>$file #$line</b><pre>";
$memory = memory_get_usage()/1024/1024;
if ($this->exception_active == true)
{
$source_highlight = $this->source_highlight($file, $backtrace);
ob_start();
include_once(exception.tpl);
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
exit(-1);
}
}
异常.tpl
<html>
<head>
<title><?php echo $ret_err_severity; ?> | <?php echo $filenameerr; ?></title>
<style type="text/css">
div#errorhandlerexception
{
border: 1px solid #ff0000;
background-color: #ff9999;
width: 96%;
padding: 10px;
color: #000;
font-family: sans-serif;
font-size: 10px;
margin: 0 auto;
}
div#errorhandlerexception h2
{
border-bottom: 1px solid #fff;
color: #ffffff;
}
div#errorhandlerexception td
{
background-color: #ffcccc;
width: 100%;
padding: 3px;
}
div#errorhandlerexception .main
{
width: 100px;
height: 30px;
}
div#errorhandlerexception
{
margin-bottom: 40px;
}
.linenum
{
text-align:right;
background:#FDECE1;
border:1px solid #cc6666;
padding:0px 1px 0px 9px;
float:left;
width:25px;
margin:3px 0px 30px 0px;
font-family: Courier New, Courier;
font-size: 11px;
}
.code
{
font-family:Courier New, Courier;
font-size: 11px;
float: left;
width: 100%;
}
.linetext
{
width:95%;
text-align:left;
background: #fff;
border: 1px solid #cc6666;
border-left:0px;
padding:0px 1px 0px 8px;
font-family: Courier New, Courier;
float:left;
margin:3px 0px 30px 0px;
font-size: 11px;
}
br.clear
{
font-family:Courier New, Courier;
clear:both;
font-size: 11px;
}
#exception_selected_block
{
background-color: #ffff00;
}
div.exception_filename
{
border-left: 19px solid #AA7777;
border-top: 3px solid #AA7777;
color: #000000;
float: left;
font-weight: bold;
padding: 6px;
width: 98%;
}
</style>
</head>
<body>
<div id="errorhandlerexception"><?php #echo $msg; ?>
<h2>EXCEPTION ERROR HANDLER</h2>
<table>
<tr><td class="main">Status:</td><td><?php echo $ret_err_severity; echo " #".$err_severity; ?></td></tr>
<tr><td class="main">Debug stack:</td><td><?php echo $dbout; ?></td></tr>
<tr><td class="main">Code:</td><td><?php echo $ret_err_no; ?></td></tr>
<tr><td class="main">Message:</td><td><?php echo $ret_err_msg; ?></td></tr>
<tr><td class="main">Info:</td><td><?php echo $ret_info; ?></td></tr>
<tr><td class="main">File:</td><td><?php echo $filenameerr; ?><br><?php echo $err_filepath; ?></td></tr>
<tr><td class="main">Line:</td><td><?php echo $err_line; ?></td></tr>
<tr><td class="main" colspan="2"><?php echo $source_highlight; ?></td></tr>
<tr><td class="main">Debug backtrace:</td><td><pre><?php echo $debug_backtrace; ?></pre></td></tr>
</table>
</div>
</body>
</html>
您可以使用 set_error_handler 定义您自己的函数,在出错时调用该函数。
<?php
//error handler function
function customError($errno, $errstr, $errfile, $errline)
{
echo "<b>Custom error:</b> [$errno] $errstr<br />";
echo " Error on line $errline in $errfile<br />";
echo "Ending Script";
die();
//set error handler
set_error_handler("customError");
$test=2;
//trigger error
if ($test>1)
{
trigger_error("A custom error has been triggered");
}
?>
参考:http ://www.w3schools.com/php/func_error_set_error_handler.asp