3

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?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.