You can create your own custom error handler and add stack trace to your error log.
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;
}
You can check if this is working by adding trigger_error
somewhere in your code.
trigger_error('Annoying notice');
Your error log should output something like that:
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"
With this kind of message it will be much easier to find out where the problem is.