Dan
Dan

Reputation: 224

Laravel Log User Agent Data in Stack Trace

I am currently using the default configuration for laravel error logging.

We are experience some errors on our production site which is logging information to a file. The full stack trace is in the file.

Is there anyway I can add user level data such as an IP address and User Agent to all stack traces? I imagine there's a way to extend the logging function, but I can't figure out where to look.

Upvotes: 2

Views: 930

Answers (2)

Bram van Dalen
Bram van Dalen

Reputation: 31

As of 5.4 there is a discrete function to get the User Agent on the request object and global request function, so you can use $request->userAgent() or request()->userAgent() as a shorthand to the answer from Azeame.

Upvotes: 0

Azeame
Azeame

Reputation: 2401

Use this to add what you need to this to the App\Exceptions\Handler.php file.

<?php

namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{

/**
 * Get the default context variables for logging.
 *
 * @return array
 */
protected function context()
{
    return array_merge(parent::context(), [
        'ip_address' => request()->ip(),
        'user_agent' => request()->header('user-agent'),
    ]);
}

This should add what you are looking for to all of your stack traces.

Upvotes: 5

Related Questions