最近我安装了Laravel 5.7并使用config/logging.php
文件启用了松弛日志记录。它正在工作,但发送到松弛的消息内容是不够的。任何人都可以指导我如何自定义发送到松弛的消息?
在config/logging.php
上你应该有一个松弛的条目与以下条目
'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
],
将short参数设置为true,我将获得异常的Context。
这涉及在createSlackDriver
(Illuminate/Log/LogManager.php
或more info in GitHub Laravel 5.6)中提供给函数same function but different line in Laravel 5.7的参数。
您无法从堆栈跟踪中获取行号,您如何知道从跟踪中获取哪一行。并且松弛不允许html内容将整个堆栈跟踪规范化为html格式。如果您希望通过正确的堆栈跟踪获得错误通知,请使用Mail。在app \ Exception \ Handler.php中使用类似下面的代码,每次例外都会通过电子邮件通知您,您也可以包含多封电子邮件。
<?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('[email protected]')->send(new ExceptionOccured($html));
} catch (Exception $ex) {
dd($ex);
}
}
}
配置你的邮件。