php - error_log 与包含文件在同一目录中?

标签 php include prepend error-log

我研究了不同的方法和指令,包括:

  • 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/

相关文章:

c++ - #include 和可能的循环引用

Javascript 对象 - 将对象添加到现有对象

C程序前置,什么时候应该追加

php - 查询以查找过去 30 天内点击次数最多的 16 个帖子,并且没有重复的类别

php - 这是正确的面向对象的php编程吗?

php - 用户 ID 进入数据库,而不是通过电子邮件发送

c++ - 保护某些包含位置

c++ - CMake target_include_directories(系统...): SYSTEM option does not seem to work on recent versions of CMake targeting Xcode

objective-c - 如何在 Swift 或 Objective C 中将文本添加到文件中?

php - 一张一张更新图片