How to escape a recursive process of saving error log from a database - PHP?

Asked

Viewed 28 times

0

I have a class that saves the log in a database based on any transaction error on that basis.

The fact is that the process of saving this log tbm is a transaction and in a hypothetical scenario any process could generate an error, and then this process would trigger the process of saving the error and this new process tbm would generate an error calling itself. This process would have no end.

Here is the code of catch:

 catch (PDOException $e) {     
                self::$exceptionObjc = $e;  
                self::saveLogMsgInDb(["exceptionObjc"=>self::$exceptionObjc,"sql"=>self::$sql]);
                self::$arrCatchConnResult = self::saveLogMsg(["exceptionObjc"=>self::$exceptionObjc,"sql"=>self::$sql]);
                self::$conn = null;
                if (self::$die) {
                    $msg = self::$arrCatchConnResult["displayMsgHTML"];
                    die($msg);
                }
            }

Here is the code of saveLogInDB:

public static function saveLogMsgInDb($saveLogsParam = NULL) {              

       $excepObjc = $saveLogsParam['exceptionObjc'] ;

       $sql = $saveLogsParam['sql'];
       $logMsg = $excepObjc->getMessage();   
       $StackTrace = json_encode($excepObjc->getTrace());  
       $exceptionObjc = json_encode($excepObjc);  
       //$StackTrace = addslashes(json_encode($excepObjc->getTrace()));  
       //$exceptionObjc = addslashes(json_encode($excepObjc)); 

        $arrSql=[
            "db_Logs"=>[
                     "msg"=>[$logMsg,PDO::PARAM_STR], 
                     "sql"=>[$sql,PDO::PARAM_STR],           
                     "StackTrace"=>[$StackTrace,PDO::PARAM_STR],
                     "exceptionObjc"=>[$exceptionObjc,PDO::PARAM_STR]
            ]
        ];



        self::set_values($arrSql); 
        //die();

    }

A possible solution would be to use a die() within saveLogMsgInDb() but this would make the site stop.

My question is what would be an approach that would escape that recursive process without breaking the web site process?

  • I’d need more information to respond better. But with the data I have, if the errors are recorded in a table of their own, or with a single field, I would look for that value inside the catch and if I had it it is because it was an attempt to write an error (which also gave error) and I would not try to write again.

  • And how to escape without breaking the process, ie without the die()?

No answers

Browser other questions tagged

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