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:

sai said...

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

haripriya said...

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


SRI said...

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

jvimala said...

Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
MATLAB TRAINING IN CHENNAI | Best MATLAB TRAINING Institute IN CHENNAI
EMBEDDED SYSTEMS TRAINING IN CHENNAI |Best EMBEDDED TRAINING Institute IN CHENNAI
MCSA / MCSE TRAINING IN CHENNAI |Best MCSE TRAINING Institute IN CHENNAI
CCNA TRAINING IN CHENNAI | Best CCNA TRAINING Institute IN CHENNAI
ANDROID TRAINING IN CHENNAI |Best ANDROID TRAINING Institute IN CHENNAI
Selenium Training in Chennai | Best Selenium Training in chennai
Devops Course Training in Chennai | Best Devops Training in Chennai

Priyanka said...

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

Training for IT and Software Courses said...

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

Training for IT and Software Courses said...

Excellent post for the people who really need information for this technology.

sap simplefinance training in bangalore

sap simplefinance courses in bangalore

sap simplefinance classes in bangalore

sap simplefinance training institute in bangalore

sap simplefinance course syllabus

best sap simplefinance training

sap simplefinance training centers

Pavithra Devi said...

This post is so interactive and informative.keep update more information...
PHP Training in Tambaram
PHP Training in Tambaram