Search code examples
phpcodeignitercodeigniter-3

Codeigniter hook is not executed


I created a hook for log all http requests in codeigniter 3 but no log is recorded after send a http request like POST or GET.

I enabled hook config in config/config.php :

$config['enable_hooks'] = TRUE;

and added hook in config/hooks.php :

$hook['post_controller_constructor'] = array(
    'class' => 'Http_request_logger',
    'function' => 'log_all',
    'filename' => 'http_request_logger.php',
    'filepath' => 'hooks',
    'params' => array()
);

And i created a hook file in application/hooks/http_request_logger.php :

<?php 

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Http_request_logger {

    public function log_all() {
        log_message('info', 'new request' . 'created from hook.');
        $CI = & get_instance();
        log_message('info', 'GET --> ' . var_export($CI->input->get(null), true));
        log_message('info', 'POST --> ' . var_export($CI->input->post(null), true));                
        log_message('info', '$_SERVER -->' . var_export($_SERVER, true));
    }

}

?>

But nothing in the log file application/logs folder.


Solution

  • After research, I realized that I have to make some changes in the config to write the log . config/config.php :

    $config['log_threshold'] = 4;
    
    
    $config['log_path'] = FCPATH . '/application/logs/';
    

    after change log_threshold and log_path my code works fine.