dr6673999 2016-02-01 00:51
浏览 88
已采纳

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

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 = "
"."Here is problem: $debug"."
"."$date - $remote_addr - $request_uri "."
";

    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"."
"."".$url."
"."".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 = "
"."Here is problem: $debug"."
"."$date - $remote_addr - $request_uri "."
";

 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.

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-02-01 01:00
    关注

    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.

    Edit:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度