使用钩子查询登录代码igniter [英] Query log in codeigniter using hook

查看:151
本文介绍了使用钩子查询登录代码igniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在项目中创建查询日志.所以我创建了一个post_controller钩子.它将所有已执行的查询保存在文本文件和数据库中.但是它仅适用于SELECT查询.我知道这是重复的问题,但是经过大量搜索,我找不到解决方案.

I need to create a query log in my project. So I created a post_controller hook. It saves all the executed queries in both a text file and a database. But it works only for SELECT queries. I know it is repeated question, but after a lot of search, I couldn't find solution.

这是我的代码:

config/hooks.php:

config/hooks.php:

$hook['post_controller'] = array(
    'class' => 'LogQueryHook',
    'function' => 'log_queries',
    'filename' => 'log_queries.php',
    'filepath' => 'hooks'
);

hooks/log_queries.php

hooks/log_queries.php

class LogQueryHook {

function log_queries() {   
    $CI =& get_instance();             
    $times = $CI->db->query_times;
    //$dbs    = array();
    $output = NULL;     
    $queries = $CI->db->queries;
    //print_r($queries);
    if (count($queries) == 0){
        $output .= "no queries\n";
    }else{
        foreach ($queries as $key=>$query){
            $took = round(doubleval($times[$key]), 3);
            $CI->db->query('INSERT INTO queryLog_tbl(`query`, `executedTime`, `timeTaken`, `executedBy`) VALUES ("'.$query.'", "'.date('Y-m-d h:i:s').'", "'.$took.'","'.$CI->session->userdata('UserID').'")');
            $output .= $query . "\n";                
            $output .= "===[took:{$took}]\n\n";
        }

    }

    $CI->load->helper('file');
    if ( ! write_file(APPPATH  . "/logs/queries.log.txt", $output, 'a+')){
         log_message('debug','Unable to write query the file');
    }   
}
}

并在我的config.php中启用了钩子:$config['enable_hooks'] = TRUE;

and hooks enabled in my config.php : $config['enable_hooks'] = TRUE;

推荐答案

在执行任何修改查询(插入,更新或删除查询)后,您需要检查内部重定向.如果您在修改查询后放置任何重定向语句,则它将取代钩子执行.

You need to check your internal redirection after any modification query(Insert, Update or delete query) executed. If you put any redirect statement after modification query then it will overtake hook execution.

您可以通过覆盖system/database/DB_driver.php

创建库并从相关控制器中调用它.

Create library and call it from relevant controllers.

这篇关于使用钩子查询登录代码igniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆