error_log与包含文件在同一目录中? [英] error_log in the same directory as included files?

查看:137
本文介绍了error_log与包含文件在同一目录中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've researched different methods and directives including:


  • auto_prepend_file

  • .user。 ini文件

  • getcwd()

  • debug_backtrace()

  • auto_prepend_file
  • .user.ini files
  • getcwd()
  • debug_backtrace()

我似乎无法找到一种方法来更改error_log的路径,以便在与包含/需要的文件相同的路径中登录。

And I can't seem to find a way to change the path of the error_log to log in the same path as the file being included/required.

For例如,说index.php有一行:

For example, say index.php has the line:

include('subdir/file.php');

如果 subdir / file.php 有一个语法错误,强制php创建 subdir / error_log ,而不是在与 index.php ,auto_prepend_file受到同样的限制,因为它在调用第一个脚本之前预先指定了文件,而不是包含每个文件。

If subdir/file.php has a syntax error, forcing php to create subdir/error_log, rather than the default behavior of creating error_log in the same path as index.php, auto_prepend_file suffers from this same limitation as it prepends the specified file before the first script called, rather than each file included.

我环顾四周,似乎无法找到合法的方法来做到这一点。通过这样做,我了解性能开销的影响,并计划仅将其用于开发目的。我相信这可以帮助隔离错误而不是使用堆栈跟踪,例如 debug_backtrace(),因为我可以使用终端脚本显示上次修改的最后错误日志,并查找有问题的文件比阅读大量错误日志要快得多。

I've looked around and can't seem to find a legitimate way to do this. I understand the implications of performance overhead by doing this, and plan to only use it for development purposes. I believe this can help isolate errors rather than using stack traces such as debug_backtrace(), as I can use a terminal script to show the last error logs by last modified, and find the offending files much quicker than reading through a massive error log.

我的目标是最终不要显示这些文件,显然是调试现有网站并且必须经历10GB错误日志或 tail / multitail 这可能有些麻烦。我正在尝试设计这种方法,以便可以通过路径隔离错误。有什么建议么?

My goal is ultimately not to have these files appear at all, obviously, as debugging already existing sites and having to go through a 10GB error logs or tail/multitailing it can be somewhat cumbersome. I'm trying to devise this method so errors can be isolated by path. Any suggestions?

推荐答案

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

Based on hakre's suggestions above, I've created this script, to be included at the top of any php script:

(如果你想分叉/下载它,这里也是我对这个文件的要点:在github上查看

(also here is a gist I made of this file if you wish to fork/download it: 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');
?>

这篇关于error_log与包含文件在同一目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆