I think I understand, as the log event (illuminate.log)..
Route::get('/', function(){
Log::error('fail');
});
will be able to be listened to with
Log::listen(function($level, $message, $context)
{
dd($message); // fail
});
and this exception...
Route::get('/', function(){
return view('invalid');
});
will be caught with
public function report(Exception $e)
{
dd($e); // InvalidArgumentException "View [invalid] not found."
return parent::report($e);
}
and the parent::report is just writing to the log with the exception $this->log->error((string) $e); and not firing an event.
So your aim is send an email when either the logging event fires or exception thrown.
How about either putting your email code in EmailErrorNotification and then firing the event from both places.
php artisan handler:event EmailErrorNotification --event=ErrorHasOccurred
Log::listen(function($level, $message, $context)
{
event(new ErrorHasOccurred($message));
});
public function report(Exception $e)
{
event(new ErrorHasOccurred($e->getMessage()));
return parent::report($e);
}
or keep the email trigger in the listen closure and simply do this to fire the event
public function report(Exception $e)
{
if ($this->shouldntReport($e)) return;
\Log::error($e->getMessage(), [
'file' => $e->getFile(),
'line' => $e->getLine()
]);
return;
}
You could keep the return parent::report($e); if you obviously wanted the full exception saved too or pass it with the context
'trace' => $e->getTrace()