dpftppc9674 2017-07-15 04:18
浏览 41
已采纳

如何在PHP中使用自定义错误日志记录的静态功能

I'm having a custom Static Error Logging method, as specified in the following link http://www.bbminfo.com/Tutor/php_error_error_log.php I executed the code as mentioned in the tutorial, I'm getting the output as expected. But now I moved the error handing method to a class and I made it as Static. I facing an issue its not working

class ErrorHandling {

    /* Error Handling Function */
    public static function bbmNotice($errNo, $errStr, $errFile, $errLine) {
        $error_msg = "Custom PHP Notice : " . $errNo . "
";
        $error_msg .= "Message : " . $errStr . "
";
        $error_msg .= "Location : " . $errFile . "
";
        $error_msg .= "Line Number : " . $errLine . "
";

        /* Error Logging in General error_log File*/
        error_log($error_msg, 0);
    }

    /* Error Handler Fixing */
    set_error_handler("bbmNotice", E_USER_NOTICE);

}


/* Undefined Variable: $str */
if(isset($str)) {
    echo $str ;
} else {
    trigger_error("Variable 'str' is not defined, Kindly define the variable 'str' before usage.", E_USER_NOTICE);
} 

I'm getting the following error

Parse error: syntax error, unexpected 'set_error_handler' (T_STRING), expecting function (T_FUNCTION) in /home2/bbminfon/public_html/error.php on line 17

Kindly assist me how to log the error in this setup.

  • 写回答

1条回答 默认 最新

  • dpw30157 2017-07-15 05:46
    关注

    The parse error occurs because you are trying register a function as error handler, but actually want to register a class method.

    Register the error handler like this instead:

    class ErrorHandling 
    {
        /* Error Handling Function */
        public static function bbmNotice($errNo, $errStr, $errFile, $errLine)
        {
            $error_msg = "Custom PHP Notice : " . $errNo . "
    ";
            $error_msg .= "Message : " . $errStr . "
    ";
            $error_msg .= "Location : " . $errFile . "
    ";
            $error_msg .= "Line Number : " . $errLine . "
    ";
    
            /* Error Logging in General error_log File*/
            error_log($error_msg, 0);
        }
    }
    
    
    set_error_handler("ErrorHandling::bbmNotice", E_USER_NOTICE);
    

    For reference, see:

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化