PHP: 将警告存储到日志文件中而不创建个人 set_error_handler
PHP: store warnings into log file without creating a personal set_error_handler
您是否知道 - 如果可能的话 - 如何将警告存储在日志文件中,就像发生致命错误时一样?
例如。代码:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL | E_STRICT);
[...]
error_log($try = buildLog(), 3, "errors_log/php_errors.log");
[...]
function buildLog() {
// Get time of request
if( ($time = $_SERVER['REQUEST_TIME']) == '') {
$time = time();
}
// Get IP address
if( ($remote_addr = $_SERVER['REMOTE_ADDR']) == '') {
$remote_addr = "REMOTE_ADDR_UNKNOWN";
}
// Get requested script
if( ($request_uri = $_SERVER['REQUEST_URI']) == '') {
$request_uri = "REQUEST_URI_UNKNOWN";
}
echo "************** CHECK ***************";
$errMy = error_get_last();
$debug = print_r($errMy, TRUE);
echo "<br>".$debug;
echo "************** CHECK ***************";
// Format the date and time
$date = date("Y-m-d H:i:s", $time);
$message = "\n"."Here is problem: $debug"."\n"."$date - $remote_addr - $request_uri "."\n";
return $message;
};
[...]
在这种情况下,例如,我收到了这些日志:
Here is problem: Array
(
[type] => 2 [message] => Missing argument 1 for buildLog(), called in /.../file.php on line 45 and defined
[file] => /.../file.php
[line] => 729
) - 2016-02-01 01:09:39 - 93.40.189.183 /.../file.php
因为我有一个致命错误,但我无法得到这个:
Warning:
fopen(http://.../file.php) [function.fopen]: failed to open stream: HTTP request failed!
HTTP/1.0 404 Not Found in /.../file.php on line 78
这是我收到 运行 页面的警告,因为:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL | E_STRICT);
更新 v. 0.1
正在测试你的建议,我发现了一种紧张行为。
这是代码:
[...]
error_reporting(0);
$url = "http://.../test.php";
echo "<br>";
print_r($url);
$child1j10 = fopen($url, 'r');
if ($child1j10 == FALSE) {
echo ("<br><b>ERROR:</b> couldn't get file, so I'll retry"."\n"."".$url."\n"."".fopen($url, 'r'));
$errMy2 = error_get_last();
$debug2 = print_r($errMy2, TRUE);
echo "<br>".$debug2;
};
echo_flush();
即使使用 error_reporting(0),这也向我显示了正确的警告信号,更准确地说:
ERROR: couldn't get file, so I'll retry http:/.../test.php
Array ( [type] => 2 [message] => fopen(http:/.../test.php) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found [file] => /.../test.php [line] => 80)
但是如果我稍后在我的代码中使用 error_get_last() 来获取日志文件中的写入,它就不起作用。
我的下一部分代码是:
error_log($prova = buildLog(), 3, "errors_log/php_errors.log");
//BUILD LOG MESSAGE
function buildLog(){
[... get all data...]
echo "************** CHECK ***************";
$errMy = error_get_last();
$debug = print_r($errMy, TRUE);
//echo "<br>".$debug;
echo "************** CHECK ***************";
// Format the date and time
$date = date("Y-m-d H:i:s", $time);
$message = "\n"."Here is problem: $debug"."\n"."$date - $remote_addr - $request_uri "."\n";
return $message;
};
并且日志仅报告:
Here is problem:
2016-02-01 02:45:55 - 93.40.189.183 - /.../test.php
当介于 "problem" 和 "data" 之间时,应该有从 error_get_last()
生成的数组
再次感谢。
更新 v.1 - 解决方案
我们开始吧。
抱歉耽误了你的时间,我觉得有点傻。
问题是我必须在代码中稍后放置函数调用,而不是像我想的那样放在开头(我认为它有点像 "server" 代码)。
这部分:
error_log($test = buildLog(), 3, "errors_log/php_errors.log");
通过这种方式,我能够获取日志文件中的所有报告。
你必须使用内置函数trigger_error
:
Used to trigger a user error condition, it can be used in conjunction with the built-in error handler, or with a user defined function that has been set as the new error handler (set_error_handler()).
This function is useful when you need to generate a particular response to an exception at runtime.
编辑:
要将文本也保存到标准错误日志中,您可以使用 error_log
。请注意,消息可以发送到不同的目标,具体取决于系统 and/or 服务器配置。
您是否知道 - 如果可能的话 - 如何将警告存储在日志文件中,就像发生致命错误时一样?
例如。代码:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL | E_STRICT);
[...]
error_log($try = buildLog(), 3, "errors_log/php_errors.log");
[...]
function buildLog() {
// Get time of request
if( ($time = $_SERVER['REQUEST_TIME']) == '') {
$time = time();
}
// Get IP address
if( ($remote_addr = $_SERVER['REMOTE_ADDR']) == '') {
$remote_addr = "REMOTE_ADDR_UNKNOWN";
}
// Get requested script
if( ($request_uri = $_SERVER['REQUEST_URI']) == '') {
$request_uri = "REQUEST_URI_UNKNOWN";
}
echo "************** CHECK ***************";
$errMy = error_get_last();
$debug = print_r($errMy, TRUE);
echo "<br>".$debug;
echo "************** CHECK ***************";
// Format the date and time
$date = date("Y-m-d H:i:s", $time);
$message = "\n"."Here is problem: $debug"."\n"."$date - $remote_addr - $request_uri "."\n";
return $message;
};
[...]
在这种情况下,例如,我收到了这些日志:
Here is problem: Array
(
[type] => 2 [message] => Missing argument 1 for buildLog(), called in /.../file.php on line 45 and defined
[file] => /.../file.php
[line] => 729
) - 2016-02-01 01:09:39 - 93.40.189.183 /.../file.php
因为我有一个致命错误,但我无法得到这个:
Warning:
fopen(http://.../file.php) [function.fopen]: failed to open stream: HTTP request failed!
HTTP/1.0 404 Not Found in /.../file.php on line 78
这是我收到 运行 页面的警告,因为:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL | E_STRICT);
更新 v. 0.1
正在测试你的建议,我发现了一种紧张行为。 这是代码:
[...]
error_reporting(0);
$url = "http://.../test.php";
echo "<br>";
print_r($url);
$child1j10 = fopen($url, 'r');
if ($child1j10 == FALSE) {
echo ("<br><b>ERROR:</b> couldn't get file, so I'll retry"."\n"."".$url."\n"."".fopen($url, 'r'));
$errMy2 = error_get_last();
$debug2 = print_r($errMy2, TRUE);
echo "<br>".$debug2;
};
echo_flush();
即使使用 error_reporting(0),这也向我显示了正确的警告信号,更准确地说:
ERROR: couldn't get file, so I'll retry http:/.../test.php
Array ( [type] => 2 [message] => fopen(http:/.../test.php) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found [file] => /.../test.php [line] => 80)
但是如果我稍后在我的代码中使用 error_get_last() 来获取日志文件中的写入,它就不起作用。 我的下一部分代码是:
error_log($prova = buildLog(), 3, "errors_log/php_errors.log");
//BUILD LOG MESSAGE
function buildLog(){
[... get all data...]
echo "************** CHECK ***************";
$errMy = error_get_last();
$debug = print_r($errMy, TRUE);
//echo "<br>".$debug;
echo "************** CHECK ***************";
// Format the date and time
$date = date("Y-m-d H:i:s", $time);
$message = "\n"."Here is problem: $debug"."\n"."$date - $remote_addr - $request_uri "."\n";
return $message;
};
并且日志仅报告:
Here is problem:
2016-02-01 02:45:55 - 93.40.189.183 - /.../test.php
当介于 "problem" 和 "data" 之间时,应该有从 error_get_last()
生成的数组再次感谢。
更新 v.1 - 解决方案
我们开始吧。 抱歉耽误了你的时间,我觉得有点傻。
问题是我必须在代码中稍后放置函数调用,而不是像我想的那样放在开头(我认为它有点像 "server" 代码)。
这部分:
error_log($test = buildLog(), 3, "errors_log/php_errors.log");
通过这种方式,我能够获取日志文件中的所有报告。
你必须使用内置函数trigger_error
:
Used to trigger a user error condition, it can be used in conjunction with the built-in error handler, or with a user defined function that has been set as the new error handler (set_error_handler()).
This function is useful when you need to generate a particular response to an exception at runtime.
编辑:
要将文本也保存到标准错误日志中,您可以使用 error_log
。请注意,消息可以发送到不同的目标,具体取决于系统 and/or 服务器配置。