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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真