Search code examples
phpexceptionerror-log

Php Custom Exceptions with error_log()


I am testing an Custom Exception class LoggedException responsible for logging the message.

But its not working as expected, my directory structure for the log file is logs/exceptions.log

I have implemented certain checks for the existence of file and even the check for the implementation of error_log(), which tells the file is written but when I open exceptions.log there is nothing in there and the message I want to write is which thrown.

class LoggedException extends exception{

    public function __construct($message,$code=0,$file='logs/exceptions.log')
    {
        if(file_exists($file)){
                if(error_log($this->getMessage(),0,$file)){
                    echo "<br/>File Wirtten error message: ".$this->getMessage();
                } else {
                    echo"<br/>cannot write";
                }
        } else {
            echo "</br>No such file there";
        }
    }

}



class someClass{
    private $prop="on";
    public function checkState($device){
        if(($this->prop)=="on"){
            throw new LoggedException("The $device is ON");
        }
        else{
            echo ("THE $device IS OFF");
        }
    }
}

$bulb=new SomeClass();
try{
    $bulb->checkState("bulb");
}
catch(LoggedException $e){
        echo "Exception message: ".$e->getMessage();
}

Browser Display: enter image description here

exceptions.log:(also not complete) enter image description here


Solution

  • Check the manual for the proper way to extend a class Extending Exceptions

    First: You are missing a parameter on your class constructor

    public function __construct($message,$code=0,$file='logs/exceptions.log')
    

    The third parameter should be Exception $previous = null like this:-

    public function __construct($message, $code = 0, 
                                Exception $previous = null, 
                                $file='./logs/exceptions.log')
    

    Second:

    you do not call parent::__construct($message, $code, $previous); before doing your own code in the __construct method of your class.

    So the Exception class is not initialised properly, but you attempt to use its properties, which of course are not yet loaded.

    Third:

    with the option set to 0 in the error_log() function call, the message will go to the standard PHP error log and not the one you intended.

    So change 0 to 3 and it will go to the file you specify in param 3.

    Fourth:

    You need to add your own Newlines manually to the messages written to the log.

    Change your code to

    class LoggedException extends Exception{
    
        public function __construct($message, $code = 0, Exception $previous = null, $file='./logs/exceptions.log')
        {
            parent::__construct($message, $code, $previous);
    
            if(file_exists($file)){
                if(error_log($this->getMessage() . PHP_EOL, 3, $file)){
                    echo "<br/>File Wirtten error message: ".$this->getMessage().PHP_EOL;
                } else {
                    echo"<br/>cannot write";
                }
            } else {
                echo "</br>No such file there";
            }
        }
    
    }
    

    And with any luck it will do roughly what you intended.