jenssegers / laravel-rollbar

Rollbar error monitoring integration for Laravel projects
328 stars 98 forks source link

Integration with Auth::user() #23

Closed mrwaim closed 8 years ago

mrwaim commented 9 years ago

I'm interested in integrating this with Auth::user() to get the user email, id, etc.

Is there a reason this isn't included by default, either in the code or the documentation?

tonglil commented 9 years ago

Since Laravel 5 I have not been able to access any of the Facades in the config, so the library is unable to get and set the user email and id from there.

Unfortunately, the only place where the library will set the person context object is during instantiation in the service provider or explicitly passed as a contextual object to each log message.

The only workaround I can propose right now is to overwrite the service provider and replace the boot method with:

    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot()
    {
        $app = $this->app;

        // Listen to log messages.
        $app['log']->listen(function ($level, $message, $context) use ($app)
        {
            $context['person'] = [
                'id' => Auth::id(),
                'email' => Auth::check() ? Auth::user()->email : null,
            ];

            $app['rollbar.handler']->log($level, $message, $context);
        });
    }

Sadly it has to come to this, but I don't see where else the person context can be set as the addContext is protected

mrwaim commented 9 years ago

That actually looks good, I can use some version of that in my fork

charles-rumley commented 8 years ago

Check out PR #35, it provides a solution for this issue.