Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace errors into a file #288

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Moatilliatta
Copy link

I've been into a requirement from my job, and I think this works well and I suppose that are other colleagues that would use this functions so they save all the error data related to MySQLi so here is my contribution to this excellent library

methods:

Main functions to generate the error from querys:

Properties:
//Boolean variable to set the errorTrack method
protected $errTrack = false;
//String variable to set the route where the file will be created
protected $routeTrack;
//String variable to set the catched error
protected $errMsg = '';

Function that help us to populate our file with all the data recolected from errorTrack function

/**
* Internal function to create a .txt file to save all the log
* collected from an specific query
* @param [type] $data Data that is going to be written in the file
*/

private function createLog($data){ 

    $actual = getcwd();

    $file = $this->routeTrack."mysqli_errors.txt";

    $fh = fopen($file, 'a') or die("Can't open/create file");
    fwrite($fh,$data);
    fclose($fh);

}

/**
* Save errors into a file
* @param bool $enabled Allow create file to save errors
* @param string $route Set a route to create this file
*/

public function setErrTrack($enabled,$route=""){
    $this->errTrack = $enabled;

    // "/var/www/html/inc/libs/";
    $this->routeTrack = $route;
}

/**
* Reset states after an execution
*
* @return object Returns the current instance.
*/

protected function reset()
{
    if ($this->traceEnabled)
        $this->trace[] = array ($this->_lastQuery, (microtime(true) - $this->traceStartQ) , $this->_traceGetCaller());

    //Added some code to this method
    if($this->errTrack && $this->_mysqli->error != NULL){

        //Put the MySQLi errno in order to complement the error info
        $this->errMsg = date("Y-m-d h:i:s ").'MySQLi errno: '.
                    $this->_mysqli->errno.' - '.
                    $this->_mysqli->error." \nQuery: ".
                    $this->_lastQuery."\n\n";

        $this->createLog($this->errMsg);

    }

...

}

Example of use

/**
* Execute sql sentence with parameters
* @param string $sql SQL sentence
* @param array $params All parameters necessary for the query
*
* @return int Result
* */

public static function execute($sql,$params){

    //Set the errorTrack method
    self::getInstance()->setErrTrack(true);

    //Execute Query
    $result = self::getInstance()->rawQuery($sql, $params);     

    //Return result
    return $result;        
}

This methods and properties have been tested with INSERT,UPDATE,DELETE sentences working OK.

I've been into a requirement from my job, and i think this works well and I suppose that are other colleagues that would use this functions so they save all the error data related to MySQLi so here is my contribution to this excellent library

methods:

Main function to generate the error from querys:
errorTrack($insertData = null,$case_err="")

Function that help us to populate our file with all the data recolected from errorTrack function
private function createLog($data)

Properties: 
   //Boolean variable to set the errorTrack method
    protected $errTrack = false;
   //String variable to set the route where the file will be created
    protected $routeTrack;

Example of use



 /**
     * Execute sql sentence with parameters
     * @param string $sql SQL sentence
     * @param array $params All parameters necessary for the query
     * 
     * @return int Result
     * */
    public static function execute($sql,$params){
        
        //Set the errorTrack method
        self::getInstance()->setErrTrack(true);

        //Execute Query
        $result = self::getInstance()->rawQuery($sql, $params);     

        //Return result
        return $result;        
    }

This methods and properties have been tested with INSERT,UPDATE,DELETE sentences working OK.
Now the query shows all the parameters in its place, insted of ? symbols in the query.
It was showing the mysqli->error twice! Now it is corrected
@pollux
Copy link

pollux commented Oct 26, 2017

I think it's nice that you think about that, but logging is something for a new class.
You're still able to get errors/information with the following methods to log them outside of the database
class:

getLastErrno() getLastError() getLastQuery()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants