zfcampus / zf-console

Create console applications in PHP
BSD 3-Clause "New" or "Revised" License
64 stars 25 forks source link

ExceptionHandler::attachListener(callable $listener) : void added #47

Open vaclavvanik opened 5 years ago

vaclavvanik commented 5 years ago

As in Zend\Stratigility\Middleware\ErrorHandler I added attachListener method to ExceptionHandler. Listeners are triggered when an exception is caught and provide ability to log exception.

Attach listeners using ExceptionHandler::attachListener():

$exceptionHandler->attachListener(function (Throwable $throwable) use ($logger) {
    $message = sprintf(
        '[%s] %s',
        date('Y-m-d H:i:s'),
        $throwable->getMessage()
    );
    $logger->error($message);
});

Tests are added but I don't know how to test triggering listners because exception handler calls php exit. :-/