Friday, December 16, 2011

Logging request parameters in CodeIgniter

The application I'm currently working on provides services to mobile devices.  As such, I need to constantly check what values the mobile devices are POST-ing to the application.  Instead of adding a call to all functions in the controllers to perform this task, I decided to implement a hook in the application.

Hooks are supported in CodeIgniter (more info here).  Here's my hook class to perform the logging:

class Logger {
   
    private $CI;
   
    public function __construct() {
        $this->CI =& get_instance();
    }
   
    public function request_logger() {
        $uri = $this->CI->uri->uri_string();
       
            $params = trim(print_r($this->CI->input->post(), TRUE));
       
        log_message('info', '==============');
        log_message('info', 'URI: ' . $uri);
        log_message('info', '--------------');
        log_message('info', $params);
        log_message('info', '==============');
    }
}
To enable hooks, you'll need to edit the config/hooks.php file.  Here's the content of the file to enable the Logger class above:
$hook['post_controller_constructor'] = array(
                                'class'    => 'Logger',
                                'function' => 'request_logger',
                                'filename' => 'Logger.php',
                                'filepath' => 'hooks'
                            );

I chose to use "post_controller_constructor" as it is called before any methods in the controller class is executed.

Output below shows how the output looks like from the application logs:
INFO  - 2011-12-15 05:09:24 --> URI: services/promos/add_comment
INFO  - 2011-12-15 05:09:24 --> Array
(
    [param1] => value1
    [id] => dj3243hasdgasdg
    [msg] => Testing testing
)

8 comments:

  1. After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience. Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in
    python course in pune
    python course in chennai
    python Training in Bangalore

    ReplyDelete
  2. Read all the information that i've given in above article. It'll give u the whole idea about it.
    Data Science Interview questions and answers
    Data Science Tutorial


    ReplyDelete
  3. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune | rpa course in bangalore
    rpa training in chennai

    ReplyDelete
  4. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  5. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information.

    aws training in bangalore

    aws courses in bangalore

    aws classes in bangalore

    aws training institute in bangalore

    aws course syllabus

    best aws training

    aws training centers

    ReplyDelete