How to create a custom log in php

1

Well, I wanted some help in tracking a particular error on my site.

I'll explain the operation first.

1st - I have the following select

$consulta = $conexao->query("select rua from endereco where id_cadastro = '3'");
$resultado = mysqli_fetch_object($consulta);

$rua = $resultado->rua;

For some error in the database the registration of this register is missing in the table endereco and so I will have the following error in php:

PHP Notice:  Trying to get property of non-object in cadastro.php on line 4

The ero occurs in line $rua = $resultado->rua , because as it did not find the result in the database it did not get it in the variable $resultado .

A simple way to solve this would be to check if it found the result and then put the value in the $rua variable. But as I said this is a database error, and it is information that could not be missing.

Is there any way to customize the php log? Whenever it identifies the error non-object , so I can create my own log and find the database with problems.

    
asked by anonymous 20.07.2017 / 13:46

1 answer

2

This class was built in 2011 or 2012, so it would not use in another project only serves as an understanding to handle the errors:

<?php
    header('Content-Type: text/html; charset=iso-8859-1');
    include_once ($_SERVER["DOCUMENT_ROOT"]."/Base/AutoLoad.class.php");

    class MyException extends Exception{
        private $AutoLoad;
        private $ConstantsExceptions;

        public function __construct($message, $code = 0, $line, $file){
           $this->file = $file;
           parent::__construct($message, $code);
        }

       public function CreateMessage(){
          $XML = new XML("status","ISO-8859-1","1.0");
          $XML->startElement("erro");
          $XML->text("Codigo do Erro: ".$this->code);
          $XML->endElement();
          $XML->startElement("texto");
          $XML->text("Mensagem do Erro: ".$this->message);
          $XML->endElement();
          $XML->startElement("arquivo");
          $XML->text("Arquivo do Erro: ".$this->file);
          $XML->endElement();
          $XML->startElement("linha");
          $XML->text("Linha do Erro: ".$this->line);
          $XML->endElement();
          $XML->endElement();
          $XML->WriteXML();
       }

       private function ErrorType(){
              $errorType = array (
                                   E_ERROR => 'ERROR',
                                   E_WARNING => 'WARNING',
                                   E_PARSE => 'PARSING ERROR',
                                   E_NOTICE => 'NOTICE',
                                   E_CORE_ERROR => 'CORE ERROR',
                                   E_CORE_WARNING => 'CORE WARNING',
                                   E_COMPILE_ERROR => 'COMPILE ERROR',
                                   E_COMPILE_WARNING => 'COMPILE WARNING',
                                   E_USER_ERROR => 'USER ERROR',
                                   E_USER_WARNING => 'USER WARNING',
                                   E_USER_NOTICE => 'USER NOTICE',
                                   E_STRICT => 'STRICT NOTICE',
                                   E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR'
                          );
           return $errorType;
        }

        public function GetNomeErro(){
             if(array_key_exists($this->getCode(), $this->ErrorType())){
                 $err = $errorType[$errno];
             }else{
                $err = 'CAUGHT EXCEPTION';
             }
             return $err;
         }

         public function customFunction(){
            $this->WriteErrorLog();
            $this->CreateMessage();
            exit();
         }

         private function Timestamp(){
            return time();
         }

         public function GetError(){
             return $this->message;
         }

        private function WriteErrorLog(){
            $this->ConstantsExceptions = new ConstantsExceptions();
            if($this->ConstantsExceptions->GetStartLog()  == true){
                $log = new WriteFileExceptions($this,$this->ConstantsExcptions);
                $log->ConstructLog();
            }
        }
    }
?>

I found an example call using try and catch :

<?php
class Conexao{
    static private $instance;
    static public $dbi;

    private function __construct(){
        try{
           if(!self::$dbi = mysql_connect(DB_HOSTI, DB_USERNAMEI, DB_PASSWORDI)){
               throw new MyException(mysql_error(),mysql_errno(),"", end(explode("/", $_SERVER['PHP_SELF'])));
           }
           try{
               if(!mysql_select_db(DB_DATABASEI)){
                   throw new MyException(mysql_error(),mysql_errno(),"",end(explode("/",$_SERVER['PHP_SELF'])));
               }
           }catch(MyException $e){
               $e->customFunction();
           }
        }catch(MyException $e){
             $e->customFunction();
        }
    }

    public static function singleton(){
        if(!isset(self::$instance)){
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$dbi;
    }
}
?>
    
20.07.2017 / 14:36