Recently I installed Laravel 5.7 and enabled slack logging using config/logging.php file. It is working but the content of the message sent to slack is not enough. Can anyone guide me how to customize the message sent to slack?
Recently I installed Laravel 5.7 and enabled slack logging using config/logging.php file. It is working but the content of the message sent to slack is not enough. Can anyone guide me how to customize the message sent to slack?
On config/logging.php you should have an entry for slack with the following entries
'slack' => [
'driver' => 'slack',
'url' => /* SLACK URL */,
'username' => /* SLACK USERNAME */,
'emoji' => /* SLACK EMOJI */,
'level' => 'error', //Change this to the level required
'short' => true //This will generate a short error message to Slack
],
With the short parameter set to true I'm getting the Context of the exception.
This relates to the parameters provided to the function createSlackDriver in Illuminate/Log/LogManager.php (more info in GitHub Laravel 5.6, or to the same function but different line in Laravel 5.7)
You can't get line number from stack trace, how do you know which one to get from trace. And slack does not allow html content to let out whole stack trace normalised into html format. If you are looking for getting notified of errors with proper stack trace, use Mail. Use similar code like below in your app\Exception\Handler.php and you will be notified by email on every exception, you can include multiple emails too.
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Mail;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
use App\Mail\ExceptionOccured;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
if ($this->shouldReport($exception)) {
$this->sendEmail($exception); // sends an email
}
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}
public function sendEmail(Exception $exception)
{
try {
$e = FlattenException::create($exception);
$handler = new SymfonyExceptionHandler();
$html = $handler->getHtml($e);
Mail::to('sdfsdfsdf@gmail.com')->send(new ExceptionOccured($html));
} catch (Exception $ex) {
dd($ex);
}
}
}
Configure your mail.