使用 laravel 日志实施松弛

Implement slack with laravel logs

我只是为 Laravel 中的日志实现 Slack,如下所示:

logging.php

'default' => env('LOG_CHANNEL', 'slack'),

'slack' => [
    'driver' => 'slack',
    'url' => env('LOG_SLACK_WEBHOOK_URL', 'https://hooks.slack.com/services/xxxx/xx'),
    'username' => 'Laravel Log',
    'emoji' => ':boom:',
    'level' => env('LOG_LEVEL', 'critical'),
],

Handler.php中我只是添加:

public function report(Throwable $exception)
{
    Log::channel('slack')->critical($exception);
} 

每当我在我的应用程序中访问任何路线 link 时,我都会在 slack 中遇到此错误!

Symfony\Component\HttpKernel\Exception\NotFoundHttpException in /Users/Ali/Documents/Sites/asu-admin/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php:43
Stack trace:
#0 /Users/Ali/Documents/Sites/asu-admin/vendor/laravel/framework/src/Illuminate/Routing/CompiledRouteCollection.php(144): Illuminate\Routing\AbstractRouteCollection->handleMatchedRoute(Object(Illuminate\Http\Request), NULL)
#1 /Users/Ali/Documents/Sites/asu-admin/vendor/laravel/framework/src/Illuminate/Routing/Router.php(647): Illuminate\Routing\CompiledRouteCollection->match(Object(Illuminate\Http\Request))
#2 /Users/Ali/Documents/Sites/asu-admin/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))

我只是清除了路线,route:cache 和所有相关的东西,但仍然,每次访问任何路线,这个异常都会被推到松懈!

事实是,这些例外情况可能一直存在。这是 Laravel 的内置错误处理程序的摘录:

protected $internalDontReport = [
        AuthenticationException::class,
        AuthorizationException::class,
        HttpException::class,
        HttpResponseException::class,
        ModelNotFoundException::class,
        MultipleRecordsFoundException::class,
        RecordsNotFoundException::class,
        SuspiciousOperationException::class,
        TokenMismatchException::class,
        ValidationException::class,
    ];

这用于

    protected function shouldntReport(Throwable $e)
    {
        $dontReport = array_merge($this->dontReport, $this->internalDontReport);

        return ! is_null(Arr::first($dontReport, function ($type) use ($e) {
            return $e instanceof $type;
        }));
    }

因此,为了让您以同样的方式正确报告异常,您需要执行以下操作:

public function report(Throwable $exception)
{
    if ($this->shoudntReport($exception) { return; }
    Log::channel('slack')->critical($exception);
} 

作为旁注,404 异常一直在发生。当我加载一个页面时,浏览器经常会尝试不同的东西,比如加载一个 favicon 或一个页面 manifest.json 来寻找 search.xml 和诸如此类的各种东西,有时因为我们在我们的模板中添加了元数据然后忘记了添加实际文件和其他时间,因为浏览器试图对此“聪明”。

如果要消除这些异常,首先要弄清楚是哪个页面没有找到。它可能不是路由操作本身,否则你会从响应中知道。