Reputation: 153
Do you have any idea - if it's possibile - how to store warnings within a log file exactly as it happens for fatal errors?
Ex. code:
<?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;
};
[...]
In this case, for ex., I've received these logs:
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
because I had a fatal error, but I'm not able to get this:
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
that is a warning I receive running page because of:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL | E_STRICT);
UPDATE v. 0.1
Testing your suggestion I'm finding a strain behavior. Here is code:
[...]
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();
This show me a correct warning signal even with error_reporting(0) and more precisely:
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)
But if I use error_get_last() later in my code in oder to obtain writing in log file, it doesn't work. My next part of code is:
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;
};
and the log reports only:
Here is problem:
2016-02-01 02:45:55 - 93.40.189.183 - /.../test.php
when between "problem" and "data" there should be the array produced from error_get_last()
thx again.
UPDATE v.1 - SOLUTION
Here we go. Sorry for your lost time, I feel a little stupid.
The problem was that I have to put function call later in code and not at the beginning as I thought (I thought it was somehow like a "server" code).
This part:
error_log($test = buildLog(), 3, "errors_log/php_errors.log");
In this way I was able to obtain all reports inside log file.
Upvotes: 0
Views: 708
Reputation: 11689
You have to use built-in function 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.
To also save the text to standard error log, you can use error_log
. Note that the message is can be sent to different targets, depending of system and/or server configuration.
Upvotes: 2