【问题标题】:How to log every GET And POST data in Codeigniter?如何在 Codeigniter 中记录每个 GET 和 POST 数据?
【发布时间】:2016-05-12 05:48:16
【问题描述】:

我有一个服务器,许多 IOS 和 Android 客户端在该服务器上发送 HTTP 请求,并根据该请求向它们发送一些响应。

现在我想将所有传入请求存储在我的日志中,以便有助于调试。我还想存储我的服务器发回的响应。

我在服务器上收到的请求:

$data = $this->license_m->array_from_get(array('request','device_id','product','software_version','platform_os','platform_model','platform'));

响应服务器回馈

 $response = array(
                     'response_code' =>200 ,
                         'request'=> $this->input->get('request'),
                     'device_id'=> $this->input->get('device_id'),
                     'allowed_hours'=> $remaining_hours,
                     'product'=>'mlc',
                     'url'=>NULL
                 );

                return $this->output
                ->set_content_type('Content-Type: application/json')
                ->set_output(json_encode($response));

现在我想将所有这些数据记录到我的 codeigniter 的日志中。该怎么做?

我试过这个:

log_message('debug',print_r($data,TRUE));

但这会保存请求数组或响应数组,而不是两者都保存

我的执照类别 :

<?php

/**
*
*/
class License extends Admin_Controller
{

 public function __construct()
 {
     parent::__construct();

     $this->load->model('license_m');
 }

 public function index($id = NULl)
 {

     $req = $this->input->get('request');



     if($req =="trial")
     {
        $request = array(
         'request' => $this->input->get('request'),
         'device_id' => $this->input->get('device_id'),
         'launch_date'=> $this->input->get('launch_date'),
         'allowed_hours'=>$this->input->get('trial_usage'),

         'product'=>$this->input->get('product'),
         'software_version'=> $this->input->get('software_version'),
         'platform_os'=> $this->input->get('platform_os'),
         'platform'=> $this->input->get('platform'),
         'platform_model'=> $this->input->get('platform_model')
             );



        /* checking if device id exists */
          $data = $this->license_m->array_from_get(array('request','device_id','product','software_version','platform_os','platform_model','platform'));

  $this->db->where('device_id',$data['device_id']);
  $this->db->where('request','activation');

  $query = $this->db->get('activation');


 $response = array(
                             'response_code' =>200 ,
                             'device_id'=> $this->input->get('device_id'),
                             'expiry_date'=> '20201231-235959',
                             'product'=>'mlc',
                             'url'=>NULL ,
                             'request'=> $this->input->get('request')


                             );


                             return $this->output
                             ->set_content_type('Content-Type: application/json')
                             ->set_output(json_encode($response));

【问题讨论】:

  • 在codeigniter中使用钩子概念
  • 您是否尝试过查看Apachenginx 日志,这些日志可能已经包含您要查找的信息? apache 日志通常位于 /var/log/ 文件夹中。
  • @VairaMuthu 钩子将如何帮助我?
  • @PrahladYeri 但我不希望每个 apache 日志我只想保存一些可用于调试我的应用程序的特定信息
  • @Rajan 我的意思是您的 apache 日志可能已经存储了这些信息,默认情况下,大多数生产服务器上的日志记录是打开的。你试过检查吗?

标签: php codeigniter logging


【解决方案1】:

您可以覆盖 CI_Output

class MY_Output extends CI_Output {
    public function _display($output = '') {
        log_message('debug', '[Request: '.@json_encode(get_instance()->get_post(null)).'][Response: '.@json_encode($output).']');

        return parent::_display($output);
    }
}

【讨论】:

  • 嘿,谢谢您的回答,但我该如何使用它?我的意思是我的 json 输出现在在某个名为 License 的类中,我将如何记录它的数据?
  • @Rajan 好吧,我需要看看这个许可证类
  • @Rajan 抱歉,但我看不到你在哪里输出 json
  • @Rajan 在我看来,您的许可证控制器类正在使用 Codeigniter 的 CI_Output 类来编写您的响应,$this->output ->set_content_type('Content-Type: application/json') - >set_output(json_encode($response)) 在内部,CI_Output 最终将调用它的 _display 方法,并且由于您正在覆盖它,因此您将获得所有输出
  • 我将如何将这些请求和响应数组记录到日志中
猜你喜欢
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多