首页 > 解决方案 > db 获取和查看文件的日志消息

问题描述

我想提供自定义错误日志来调试 codeigniter 中的内部服务器错误。

我已经使用 try 和 catch 来捕获代码行中的任何错误,但没有成功。如果我查询一些错误的表,则正在生成通用日志(从系统文件夹)。当我检查数据库查询构建器文件时,我发现内部文件正在处理错误,但它没有抛出任何异常或错误(脚本在生成日志并显示错误后安全结束)。

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 ');
} 

有什么方法可以从我的文件中生成自定义日志,例如用户 ID、少量会话数据而不影响核心文件。

注意:我在 ajax 请求的帮助下实现了这一点。如果请求失败,我只需发出另一个请求以从 xhr.responseText 生成自定义日志。

有没有办法在不影响核心文件的情况下实现这一点?

标签: phpcodeigniter

解决方案


要在 codeigniter 中记录您的错误,请创建core/MY_Exceptions.php文件以处理自定义错误。

这是 core/MY_Exceptions.php文件的演示

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;
}

}

您可以根据需要记录错误并设置响应。


推荐阅读