Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP log caught exception

PHP only logs uncaught exceptions. I'd like to also log all of my caught exceptions.

Example 1

try {
    $book->getBook();
} catch( Exception $e ) {
    error_log( $e );
    $error = 'A problem occurred getting your book'
}

This works fine, but I'd prefer not to have to keep writing error_log all over the place.

So instead I've extended the Exception class like so:

Example 2

class ExceptionLog extends Exception {
    public function __construct( $message, $code = 0, Exception $previous = null ) {
        error_log( $this );
        parent::__construct($message, $code, $previous);
    }
}

I can then do:

try {
    $book->getBook();
} catch( ExceptionLog $e ) {
    $error = 'A problem occurred getting your book'
}

The one issue here is that the message which is logged is slightly different. In the first example the log entry is:

[01-Jan-2016 19:24:51 Europe/London] PHP Fatal error:  Uncaught exception 'Exception' with message 'Could not get book' in book.php:39

In the second example the message is omitted:

[01-Jan-2016 19:24:51 Europe/London] exception 'ExceptionLog' in book.php:39

Is the only way to access the properties of the parent Exception class and build the error log string manually?

like image 980
MrCarrot Avatar asked May 07 '26 02:05

MrCarrot


1 Answers

Have you noticed that your custom error message is never being used?

There are two reasons for this: in your 'ExceptionLog' class constructor, you are logging the error before calling the parent 'Exception' class constructor, and you never provide your custom error message to the 'ExceptionLog' class constructor.

Your ExceptionLog class should look like this:

class ExceptionLog extends Exception {
  public function __construct($message, $code = 0, Exception $previous = null) {
    parent::__construct($message, $code, $previous);
    error_log($this);
  }
}

Then, in your 'Book' class, you have your method 'getBook()', which throws your custom error (note that I am explicitly throwing the error for demonstration purposes):

class Book {
  public function getBook() {
    throw new ExceptionLog('A problem occurred getting your book');
  }
}

See how you pass your custom error message to the 'ExceptionLog' class constructor? Then you can create an instance of the 'Book' class:

$book = new Book();

And change your try/catch to the following:

try {
  $book->getBook();
} catch (ExceptionLog $e) {
  //Custom error message is already defined
  //but you can still take other actions here
}

Which should produce an error similar to what I saw in my 'php_error.log' file:

[01-Jan-2016 21:45:28 Europe/Berlin] exception 'ExceptionLog' with message 'A problem occurred getting your book' in /Applications/MAMP/htdocs/php_exception_test/index.php:13
like image 118
trevor Avatar answered May 08 '26 15:05

trevor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!