[SOLVED] Read and Write Database Connection

#8
[eluser]majidmx[/eluser]
Hi my friend,

I don't log the queries' execution time in my log class, I just use it for debug purposes and also for getting more information on my application's details.
But if it helps here it is :

log_model.php :
Code:
<?php
class Log_model extends Model{
    function Log_model(){
        parent::Model();
        
        $this->dbr = $this->load->database('db_read', TRUE);
        $this->dbw = $this->load->database('db_write', TRUE);        
        
        $this->load->helper('date');
    }
    
    function insertLog($message_type = 'Error' , $message = '' ){
            
        $extraInfo        =    '';
        $extraInfo        .=    "\r\n\n\t\t -- REQUEST  --"."\r\n\n";
        $extraInfo        .=    var_export($_REQUEST ,true);
        
        $extraInfo        .=    "\r\n\n\t\t -- COOKIE  --"."\r\n\n";
        $extraInfo        .=    var_export($_COOKIE , true);
        
        $extraInfo        .=    "\r\n\n\t\t -- ENV  --"."\r\n\n";
        $extraInfo        .=    var_export($_ENV ,true);
        
        $extraInfo        .=    "\r\n\n\t\t -- SERVER  --"."\r\n\n";
        $extraInfo        .=    var_export($_SERVER ,true);
        
        if(!isset($_SERVER['HTTP_REFERER'])){
            $ref    =    'N/A';
        }else{
            $ref    =    $_SERVER['HTTP_REFERER'];
        }
        
        $db_sql    =     "INSERT INTO log SET ".
                    "log_type = ? , log_message = ? ".
                    " , log_date = CURDATE() , log_time = CURTIME() , log_ip = ? , log_browser = ? , log_referer = ? ".
                    ", log_uri = ? , log_extra_info = ? ";
        $db_val    =    array(
                            $message_type , $message ,
                            $this->input->ip_address() , $this->input->user_agent() , $ref
                            , $_SERVER['PHP_SELF']  , $extraInfo
                        );
        $db_result = $this->dbw->query($db_sql , $db_val);
    }
    
}
?>

and the library : mmx_log.php
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class MMX_Log{
    var $_ci;
    
    function MMX_Log(){        
        $this->_ci =& get_instance();
        $this->_ci->load->model("Log_model");
    }

    function Log($message = ''   , $message_type = 'Error' ){
        $this->_ci->Log_model->insertLog($message_type , $message);
    }
    
    function file_log($txt){
        if(is_array($txt)){
            $txtContent    =    var_export($txt , true);
        }else{
            $txtContent = $txt;    
        }        
        
        $txtContent .= "\r\n------------------------\r\n";
        
        $FileName        =    '/usr/www/virtual/saved_data.txt';
        if (!$handle = fopen($FileName, 'a')) {    
             exit;
        }        
        if (fwrite($handle, $txtContent) === FALSE) {
            exit;
        }        
        fclose($handle);                
    }    
}
?>

Actually you can get most of it's functionality by using the core CI log system, specially the file part, but as we don interpret the logs for special purposes it really helps.
Our logging system has its own fields which I caught them out as they don't apply here.

Thanks,
MajiD


Messages In This Thread
[SOLVED] Read and Write Database Connection - by El Forum - 11-13-2009, 06:22 AM
[SOLVED] Read and Write Database Connection - by El Forum - 11-16-2009, 01:06 PM
[SOLVED] Read and Write Database Connection - by El Forum - 12-21-2009, 01:53 AM
[SOLVED] Read and Write Database Connection - by El Forum - 12-21-2009, 02:06 AM
[SOLVED] Read and Write Database Connection - by El Forum - 01-13-2010, 12:29 AM
[SOLVED] Read and Write Database Connection - by El Forum - 01-13-2010, 12:20 PM
[SOLVED] Read and Write Database Connection - by El Forum - 01-14-2010, 10:28 PM
[SOLVED] Read and Write Database Connection - by El Forum - 01-15-2010, 01:49 AM
[SOLVED] Read and Write Database Connection - by El Forum - 01-15-2010, 02:23 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.