saqueib
3 years ago

Setting up monolog to with slack, How can I get error log with requested url etc.

Posted 3 years ago by saqueib

I have checked the official repo of monolog and put together below code, its working greate and I am getting response in Slack. But the response are very verbose, How Can i get simple response, without Stack Trace.

And as suggested on doc I have added $monolog->pushProcessor(new WebProcessor($_SERVER)); but its not giving any information about requested URL and server.


class AppServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if ($this->app->environment('production')) {
            // Get The Logger
            $monolog = Log::getMonolog();

    // **********************************************************************************************
    // I have tried official WebProcessor to get url, but its not giving me anything
    // **********************************************************************************************

            $monolog->pushProcessor(new WebProcessor($_SERVER));

            $monolog->pushProcessor(function ($record) {
                $record['extra']['session_id'] = Cookie::get(config('session.cookie'));
                $record['extra']['request_id'] = Session::get('request_id');
                return $record;
            });

            $slackHandler = new SlackHandler(env('SLACK_TOKEN'), '#sss-sslogs', 'sss-log', true, ':warning:', Logger::ERROR);
               
    // **********************************************************************************************
    // Setup Line Formatter but no luck 
    // **********************************************************************************************

            // the default date format is "Y-m-d H:i:s"
            $dateFormat = "Y n j, g:i a";
            // the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
            $output = "%datetime% > %level_name% > %message% %context% %extra%\n";
            // finally, create a formatter
            $formatter = new LineFormatter($output, $dateFormat);
            $slackHandler->setFormatter($formatter);

            $monolog->pushHandler($slackHandler);
        }
    }

Above code is giving me below response in Slack channel

exception 'Illuminate\Database\Eloquent\ModelNotFoundException' with message 'No query results for model [App\User].' in /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:151
Stack trace:
#0 /home/vagrant/Code/App/app/Http/Controllers/PortfolioController.php(30): Illuminate\Database\Eloquent\Builder->firstOrFail()
#1 [internal function]: App\Http\Controllers\PorofileController->show('users')
#2 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(246): call_user_func_array(Array, Array)
#3 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(162): Illuminate\Routing\Controller->callAction('show', Array)
#4 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(107): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\PortfolioController), Object(Illuminate\Routing\Route), 'show')
#5 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#6 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#7 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#8 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(101): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#9 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(108): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#10 /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(67): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\PortfolioController), Object(I…

Level
----------------
ERROR

Where is the requested URL and session data??

How can I get only below part with requested URL, I am not interested in Stack trace:

exception 'Illuminate\Database\Eloquent\ModelNotFoundException' with message 'No query results for model [App\User].' in /home/vagrant/Code/App/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:151

Please sign in or create an account to participate in this conversation.