您可以创建自己的自定义错误处理程序,并将堆栈跟踪添加到错误日志中。
set_error_handler(\'wpse_288408_handle_error\');
function wpse_288408_handle_error( $errno, $errstr, $errfile, $errline ) {
if( $errno === E_USER_NOTICE ) {
$message = \'You have an error notice: "%s" in file "%s" at line: "%s".\' ;
$message = sprintf($message, $errstr, $errfile, $errline);
error_log($message);
error_log(wpse_288408_generate_stack_trace());
}
}
// Function from php.net http://php.net/manual/en/function.debug-backtrace.php#112238
function wpse_288408_generate_stack_trace() {
$e = new \\Exception();
$trace = explode( "\\n" , $e->getTraceAsString() );
// reverse array to make steps line up chronologically
$trace = array_reverse($trace);
array_shift($trace); // remove {main}
array_pop($trace); // remove call to this method
$length = count($trace);
$result = array();
for ($i = 0; $i < $length; $i++) {
$result[] = ($i + 1) . \')\' . substr($trace[$i], strpos($trace[$i], \' \')); // replace \'#someNum\' with \'$i)\', set the right ordering
}
$result = implode("\\n", $result);
$result = "\\n" . $result . "\\n";
return $result;
}
您可以通过添加
trigger_error
代码中的某个地方。
trigger_error(\'Annoying notice\');
错误日志应输出如下内容:
2017/01/02 12:00:00 [error] 999#999: *999 FastCGI sent in stderr: "PHP message: You have an error notice: "Annoying notice" in file "/var/www/test/wp-content/plugins/test/test.php" at line: "99".
PHP message:
1) /var/www/test/index.php(17): require(\'/var/www/test/w...\')
2) /var/www/test/wp-blog-header.php(13): require_once(\'/var/www/test/w...\')
3) /var/www/test/wp-load.php(37): require_once(\'/var/www/test/w...\')
4) /var/www/test/wp-config.php(93): require_once(\'/var/www/test/w...\')
5) /var/www/test/wp-settings.php(305): include_once(\'/var/www/test/w...\')
6) /var/www/test/wp-content/plugins/test/test.php(99): trigger_error(\'Annoying notice\')
7) [internal function]: wpse_288408_handle_error(1024, \'Annoying notice\', \'/var/www/test/w...\', 99, Array)" while reading response header from upstream, client: 192.168.33.1, server: test.dev, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9070", host: "test.dev"
有了这种信息,就更容易找出问题所在。