How to create a custom log in php

Asked

Viewed 199 times

1

Bom wanted a help to be able to monitor a certain 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;

By some error in the database the record of this record is missing in the table endereco and so I will get the following error in php:

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

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

A simple way to solve this would be to check if he found the result and then put the value in the variable $rua. But like I said, it’s an error in the database, and it’s information that couldn’t possibly 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 locate the database with problems.

  • 2

    Create a Myexception class and extend the PHP Exception class within your class customize the errors : NOTICE, WARNINGS , ERROR and so on. I have a class like that here, I just don’t know where I put it. If you want I can look.

  • The following is the link to the documentation : https://secure.php.net/manual/en/language.exceptions.extending.php

  • It would be nice if you could pass me this class.

1 answer

2

This class was built in 2011 or 2012, therefore it would not use in another project only serves as understanding to treat 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 of a 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;
    }
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.