我研究了不同的方法和指令,包括:
- auto_prepend_file
- .user.ini 文件
- getcwd()
- 调试回溯()
而且我似乎无法找到一种方法来更改 error_log 的路径以登录与包含/需要的文件相同的路径。
例如,假设 index.php 有一行:
include('subdir/file.php');
如果subdir/file.php
有语法错误,强制 php 创建 subdir/error_log
,而不是在与 index.php
相同的路径中创建 error_log 的默认行为, auto_prepend_file 受到同样的限制,因为它在调用第一个脚本之前添加指定的文件,而不是包含的每个文件。
我环顾四周,似乎无法找到一个合法的方式来做到这一点。我通过这样做了解性能开销的含义,并计划仅将其用于开发目的。我相信这可以帮助隔离错误,而不是使用堆栈跟踪,例如 debug_backtrace()
,因为我可以使用终端脚本来显示上次修改的最后错误日志,并且比阅读大量错误日志更快地找到有问题的文件。
我的目标最终是完全不让这些文件出现,很明显,因为调试已经存在的站点并且必须通过 10GB 的错误日志或 tail
/multitail
荷兰国际集团可能会有些麻烦。我正在尝试设计这种方法,以便可以按路径隔离错误。有什么建议么?
最佳答案
根据 hakre 的上述建议,我创建了这个脚本,将包含在任何 php 脚本的顶部:
(如果你想 fork /下载它,这里也是我对这个文件所做的要点:view on github)
<?
function custom_error_debug($errno, $errstr, $errfile, $errline, $errvars) {
$message = "";
$message .= "[ " . date('Y-m-d h-i-s') . " ] Error: [$errno] $errstr on line $errline of $errfile ";
//Dump all info to browser!
//WARNING: Leave this commented except for extreme cases where you need to see all variables in use!
//Using this will cause excessive processing time, and RAM. Use only as needed!
/*if (!empty($errvars)) {
echo $message . PHP_EOL . "Variables in use: <pre>";print_r($errvars); echo "</pre>";
//WARNING: not ending execution here may cause the browser to overload on larger frameworks, comment out at your own risk!
die();
}*/
//get the directory of the offending file, put a log in that path, and separate them by end of line, append to file
file_put_contents ( dirname($errfile) . "/php_errors.log", $message . PHP_EOL, FILE_APPEND );
//Dump all variables to file as well, (MAY CAUSE LARGE FILES, read above)
//file_put_contents ( dirname($errfile) . "/php_errors.log", $errvars . PHP_EOL, FILE_APPEND );
//Optionally end script execution
//die();
}
set_error_handler('custom_error_debug');
?>
关于php - error_log 与包含文件在同一目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18711422/