Search code examples
phplaravelslack

Implement slack with laravel logs


I just Implement Slack for logs in Laravel as follow:

in 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'),
],

In Handler.php I just add:

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

whenever I visit any route link in my app I got this error in 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))

I just clear route clear and route:cache and everything related but still, every visit to any route this exception pushed to slack!


Solution

  • The reality of the matter is those exceptions were probably always there. Here's an excerpt of the built-in error handler of Laravel:

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

    and this is used in

        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;
            }));
        }
    

    therefore in order for you to properly report exceptions the same way you need to do something like:

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

    As a sidenote 404 exceptions happen all the time. When I load a page the browser will often try different things out like loading a favicon or a page manifest.json of finding the search.xml and various things like this sometimes because we add the metadata in our template and then forget to add the actual files and other times because the browser tries to be "smart" about it.

    If you want to eliminate these exceptions you need to first figure out which page is not found. It probably isn't the route action itself otherwise you'd know from the response.