Hook for Query Log and Redirect Instruction

-1

I'm using hook for queries log. However when I use redirect instead of load->view , the db->queries array is empty. I need to continue using the redirect statement. Can someone help me?

I'm using this template :

$hook['post_controller'] = array(     // 'post_controller' indicated execution of hooks after controller is finished
    'class' => 'Db_log',             // Name of Class
    'function' => 'logQueries',     // Name of function to be executed in from Class
    'filename' => 'db_log.php',    // Name of the Hook file
    'filepath' => 'hooks'         // Name of folder where Hook file is stored
);
class Db_log {

var $CI;

function __construct() {
    $this->CI = & get_instance(); // Create instance of CI
}
function logQueries() {

    $filepath = APPPATH . 'logs/QueryLog-' . date('Y-m-d') . '.php'; // Filepath. File is created in logs folder with name QueryLog
    $handle = fopen($filepath, "a+"); // Open the file

    $times = $this->CI->db->query_times; // We get the array of execution time of each query that got executed by our application(controller)

    foreach ($this->CI->db->queries as $key => $query) { // Loop over all the queries  that are stored in db->queries array
        $sql = $query . " \n Tempo de execução:" . $times[$key]; // Write it along with the execution time
        fwrite($handle, $sql . "\n\n");
    } 
    fclose($handle); // Close the file
}

The controller, instead of using $this->view("unimed") I'm using redirect(base_url().'unimed');

What I realized is that when using Redirect, it loses (or restarts) the whole controller .. and hence the values of the array $this->db->queries is empty I need to continue using redirect for the following reason: when using a load->view() the url continues being the last one that was passed. And with the redirect the url comes clean eg with

load->view("unimed") 

after saving a record

the url comes from: localhost / profit / manager / unimed / save /? id = & par =

Eg:

redirect(base_url().'unimed')

After saving a record to url comes localhost / profit / manager / unimed

    
asked by anonymous 16.02.2018 / 18:31

1 answer

1

[RESOLVED] I did as follows: I created a new item in config.php:

$ config ['log_file'] = TRUE and I changed the DB_driver by including the following statement:

    $ci = get_instance();
    if($ci->config->item('gera_arquivo_log')===TRUE and $this->save_queries === TRUE):
        Check::gravaFileLog($sql, $time_end - $time_start);
    endif;

I created a static method in my Check class:

public static function gravaFileLog($query, $time) {
    $filepath = APPPATH . 'logs/QueryLog-' . date('Y-m-d') . '.php'; // Filepath. File is created in logs folder with name QueryLog
    $handle = fopen($filepath, "a+"); // Open the file

    $sql = $query . " \n Tempo de execução:" . $time; // Write it along with the execution time
    fwrite($handle, $sql . "\n\n");
    fclose($handle); // Close the file
}

If someone has a more appropriate solution I appreciate it.

    
16.02.2018 / 21:22