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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料