Вопрос

I've researched different methods and directives including:

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

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 example, say index.php has the line:

include('subdir/file.php');

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.

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.

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?

Это было полезно?

Решение

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

(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');
?>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top