gpt4 book ai didi

PHP:将警告存储到日志文件中而不创建个人 set_error_handler

转载 作者:行者123 更新时间:2023-12-03 08:12:17 24 4
gpt4 key购买 nike

你有什么想法——如果可能的话——如何在日志文件中存储警告,就像发生 fatal error 一样?

前任。代码:

<?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

因为我有一个 fatal error ,但我无法得到这个:
 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

当在“问题”和“数据”之间时,应该有从 error_get_last() 生成的数组

再次感谢。

更新 v.1 - 解决方案

开始了。
抱歉耽误了您的时间,我觉得有点愚蠢。

问题是我必须稍后在代码中而不是像我想的那样在开头放置函数调用(我认为它有点像“服务器”代码)。

这部分:
 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 .请注意,消息可以发送到不同的目标,具体取决于系统和/或服务器配置。

关于PHP:将警告存储到日志文件中而不创建个人 set_error_handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com