gpt4 book ai didi

php - 在我的应用程序日志文件中写入PHP日志

转载 作者:行者123 更新时间:2023-12-03 08:03:44 25 4
gpt4 key购买 nike

我正在开发一个php应用程序,并且已经为其创建了日志记录功能。我的意思是每次出现问题时,我都会在日志文件中写入错误,因此,如果用户报告错误,则可以使用日志文件来跟踪该错误。

问题是,如果我在生产服务器上上传项目,则必须禁用PHP的默认错误显示;我只想在自己的日志文件中记录php错误以及Web服务器的“error.log”。有什么办法可以做到吗?

最佳答案

对于特色的可用异常(exception)情况(我正在调用功能齐全的可用异常(exception)情况,(没有堆栈跟踪)):

如果您是开发人员,请设置:

   define('EXCEPTION_HANDLER_ACTIVE', true);

在生产中,这也应该是正确的,但是要修改代码以不向最终用户显示错误。 (使用:页面顶部的 error_reporting(0); ini_set('display_errors', false)));

首先设置用于处理异常的主要功能:
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 =&gt;$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);
}
}

Exception.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>

关于php - 在我的应用程序日志文件中写入PHP日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12736125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com