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?