【问题标题】:error_log in the same directory as included files?error_log 与包含的文件在同一目录中吗?
【发布时间】:2013-09-13 17:17:40
【问题描述】:

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

  • auto_prepend_file
  • .user.ini 文件
  • getcwd()
  • debug_backtrace()

而且我似乎无法找到一种方法来更改 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/multitailing 这可能有点麻烦。我正在尝试设计这种方法,以便可以通过路径隔离错误。有什么建议?

【问题讨论】:

  • 我不确定你的想法在实践中是否成功,但包含一个文件并不意味着工作目录会改变。我看到的唯一机会是您创建了自己的错误处理函数 (php.net/set_error_handler),该函数查看错误文件的回溯(注意某些错误不在文件中,而是“无处”,因此您需要查看last 文件),然后登录到该目录 (php.net/error_log)。您在 auto_prepend_file. 中注册该处理程序
  • "10GB 错误日志" - 您需要先解决不同的问题,例如首先对这个大球进行logrotate。如果消息数量过多,也暂时不要记录重复错误:stackoverflow.com/q/1964732/367456(可能对您没有帮助,只是说)
  • 我喜欢不记录重复错误的想法。我遇到了一些已经存在的 Magento 和 Wordpress 网站,所以通常如果我发现一个大的错误日志,我会重命名它(就像大多数人一样)并抓取出现的新日志。但是在php的上下文中,除非已经缓存,否则所有内容都包含在运行时,这可能无济于事,如果我是正确的,它只会阻止在同一请求上发生重复错误,并且不会每次都将日志修剪到查看是否存在错误。我的假设正确吗?
  • 您对自定义错误日志的第一个建议似乎更适合这种情况。我可以使用 debug_backtrace 并从数组中获取“文件”值,在其上使用正则表达式来消除实际文件名,并仅使用与 ini_set('error_log',$error_path) 一起使用的路径
  • 好吧,一个正则表达式似乎是错误的,例如dirname()。回溯在这里:php.net/debug_backtrace - 第二条评论并不是建议如何做你所要求的更多,如果错误日志那么大,你已经遇到了很大的问题。你需要先修复一些错误。

标签: php include prepend error-log


【解决方案1】:

根据上面 hakre 的建议,我创建了这个脚本,以包含在任何 php 脚本的顶部:

(如果你想分叉/下载它,这里也是我对这个文件的一个要点: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');
?>

【讨论】:

    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 2013-05-03
    相关资源
    最近更新 更多