简体   繁体   中英

insert all errors in website into mysql database (Maintain log errors in database)

i am searching for the code or script that is helpfull to insert all errors into mysql database.i do not know about it ,even not a little bit.

give me any idea about it.

how can i do this means insert all erros in database and the display them in my php page.

PHP lets you specify you own error handler - set_error_handler() .

function error_handler($errno, $errstr, $errfile, $errline) {
    global $db; //naughty use of global. You could re-create your database connection here instead.

    switch ($errno) {
        case E_NOTICE:
        case E_USER_NOTICE:
            $error = 'Notice';
            break;
        case E_WARNING:
        case E_USER_WARNING:
            $error = 'Warning';
            break;
        case E_ERROR:
        case E_USER_ERROR:
            $error = 'Fatal Error';
            break;
        default:
            $error = 'Unknown';
            break;
    }
    $db->query('INSERT INTO ...'); // create error log here

    return true;
}

// Error Handler
set_error_handler('error_handler');

The parameters used in my custom function above have the same names as the manual:

$errno The first parameter, errno, contains the level of the error raised, as an integer.

$errstr The second parameter, errstr, contains the error message, as a string.

$errfile The third parameter is optional, errfile, which contains the filename that the error was raised in, as a string.

$errline The fourth parameter is optional, errline, which contains the line number the error was raised at, as an integer.

Using these parameters you should be able to determine what the error was, what file it was in, and what line it occured on.

i would use something like this to log custom error, but you can modify it around to store all encountered errors.

class Logger extends PDO{
    private $_message;
    public function __construct(){
        parent::__construct("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password);//make sure these matches your database configs
        return null;
    }

    public function setMessage($message){
        $this->_message = $message;
        return $this;

    }

    public function saveMessage(){

        if (!isset($this->_message)){
            die('Error Message has not been set');
        }
        $sql = 'INSERT INTO erros_table error_message,date_created';
        $sql.= 'VALUES (:error_message,:date_created)';

        $query = $this->prepare($sql);
        $query->execute([':error_message' => $this->_message,':date_created' => date('y-m-d')]);

        return null;
    }
}

//now you will do something like this
//you can use this custom class anywhere 




try{

    //execute any code here
    //and make sure to throw an error upon any code fail
    //its up to you to throw exceptions on your custom code, but i advice you so

}catch(Exception $e){
    $logger = new Logger();


    $logger->setMessage($e->getMessage())->saveMessage();


}

You're question it's too generic. The main problem here is how to track errors and it's not clear if you want to track client side errors (javascript), server side errors (PHP) or transactions errors (MySQL). In case you want to track all of these, you can develop you're own error handlers. For example, for javascript I use this snippet of code:

    window.jsErrors = [];
    window.onerror = function(errorMessage, url, lineNumber) {
      err = { "host" : window.location.host, "url" : url,
              "error": errorMessage, "line" : lineNumber,
              "user": "John" };
      window.jsErrors[window.jsErrors.length] = err;
      notify(err);
    } 

The notify funcion makes an ajax request. For PHP, take a look at this . For Java, take a look at Log4J DB appender

You should have to do like this for error hanling in mysql.

function throw_ex($er){  
  throw new Exception($er);  
}  
try {  
$q = mysql_query("Insert into test (col1,col2,col3) VALUES ('blah','blah','blah')") or throw_ex(mysql_error());  
}  
catch(exception $e) {
  echo "ex: ".$e; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM