duanjia4817 2019-02-14 11:38
浏览 77

db get和view文件的日志消息

I want to provide the custom error log to debug a internal server error in codeignitor.

I have used the try and catch to catch any error in the lines of code but no success.Generic Logs are being generated(from system folder),if I am querying some wrong table.When I checked the database query builder file, I found that errors are being handled by the internal file but it is not throwing any Exception or error(script is safely ending after generating the log and displaying error).

try{ 
    $data['tags'] = $_SESSION['tags'];
    $data['accId'] = isset($_POST['accId']) ? $_POST['accId'] : '';
    $data['categorySelectionList']=$_SESSION['categorySelectionList'];
    $data['accountSelectionList'] = $_SESSION['accountSelectionList'];
    $_SESSION['transDescList'] = '';
    $this->load->model('Transactions_model');
    $data['transDescList'] = !empty($_SESSION['transDescList']) 
        ? $_SESSION['transDescList'] 
        :  $this->Transactions_model->getTransDescList($this->uid,$_SESSION['tbl_transactions']);
    $this->load->view('frontend/accounts/addTransaction', $data);
}catch(Error $e){
    log_message('error', 'There is a error by the user ');
} 

Is there any way to produce the custom log from my file like userid, few session data without affecting the core files.

Note: Although I have achieve this with the help of ajax request.If the request fails i simply make another request to generate the custom log from xhr.responseText.But I want to know if there is any way to achieve this without affecting the core files

  • 写回答

1条回答 默认 最新

  • duanqiongdu9916 2019-02-14 12:29
    关注

    To log your error in codeigniter crate core/MY_Exceptions.php file to handle customer error.

    Here is Demo of core/MY_Exceptions.php file

    class MY_Exceptions extends CI_Exceptions{
    
    
    /*
     * Class constructor
     *
     * @return  void
     */
    public function __construct()
    {
        $this->ob_level = ob_get_level();
        /* Note: Do not log messages from this constructor.*/
    }
    
    
    /**
     * Exception Logger
     *
     * Logs PHP generated error messages
     *
     * @param   int $severity   Log level
     * @param   string  $message    Error message
     * @param   string  $filepath   File path
     * @param   int $line       Line number
     * @return  json response
     */
    public function log_exception($severity, $message, $filepath, $line)
    {
        /*
         *  default log this
         *  and user will get a message of server error
         */
        $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
        log_message('error', 'Severity: '.$severity.' --> '.$message.' '.$filepath.' '.$line);
    
        $response = array(
            'status' => 'Error',
            'error' => '1',
            'message' => 'Server Error ! Please contact administrator !'
        );
        echo json_encode($response);exit;
    }
    
    
    /**
     * 404 Error Handler
     *
     * @uses    CI_Exceptions::show_error()
     *
     * @param   string  $page       Page URI
     * @param   bool    $log_error  Whether to log the error
     * @return  json response
     */
    public function show_404($page = '', $log_error = TRUE)
    {
        if (is_cli())
        {
            $heading = 'Not Found';
            $message = 'The controller/method pair you requested was not found.';
        }
        else
        {
            $heading = '404 Page Not Found';
            $message = 'The page you requested was not found.';
        }
    
        /*
         *  default log this
         *  and user will get a message of server error
         */
        if ($log_error)
        {
            log_message('error', $heading.': '.$page);
        }
    
        $response = array(
            'status' => 'Error',
            'error' => '1',
            'message' => $message
        );
        echo json_encode($response);exit;
    }
    
    
    /**
     * General Error Page
     *
     * Takes an error message as input (either as a string or an array)
     * and displays it using the specified template.
     *
     * @param   string      $heading    Page heading
     * @param   string|string[] $message    Error message
     * @param   string      $template   Template name
     * @param   int     $status_code    (default: 500)
     *
     * @return  json response
     */
    public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {
        /*
         *  default log this
         *  and user will get a message of server error
         */
        log_message( 'error', print_r($heading,true).': '. print_r($message,true) );
    
        $response = array(
            'status' => 'Error',
            'error' => '1',
            'message' => 'Server Error ! Please contact administrator !'
        );
        echo json_encode($response);exit;
    }
    
    
    public function show_exception($exception)
    {
        /*
         *  default log this
         *  and user will get a message of server error
         */
        log_message( 'error', $exception->getMessage() );
    
        $response = array(
            'status' => 'Error',
            'error' => '1',
            'message' => 'Server Error ! Please contact administrator !'
        );
        echo json_encode($response);exit;
    
    }
    
    
    /**
     * Native PHP error handler
     *
     * @param   int $severity   Error level
     * @param   string  $message    Error message
     * @param   string  $filepath   File path
     * @param   int $line       Line number
     * @return  json response
     */
    public function show_php_error($severity, $message, $filepath, $line)
    {
        /*
         *  default log this
         *  and user will get a message of server error
         */
        $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
        log_message('error', 'Severity: '.$severity.' --> '.$message.' '.$filepath.' '.$line);
    
        $response = array(
            'status' => 'Error',
            'error' => '1',
            'message' => 'Server Error ! Please contact administrator !'
        );
        echo json_encode($response);exit;
    }
    

    }

    You can log your error and set response as you wish.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大