Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jmadlena's avatar

Create unique request ID and bind to app

Hi,

I'm working on improving the logging in my app. Basically, I want to be able to see all logs associated with a single request.

In Laravel 5.3 is there an existing unique ID that is generated for each request? If not, is there a recommended way to create a unique ID and then bind it to the app in a way that I can access anywhere?

Also, if you have any tips about inserting this ID into every log (without having to do so manually) let me know!

Thanks!

0 likes
10 replies
kevinvdburgt's avatar

I am not sure if laravel generate's an unique id for each request, but what about combining the timestamp, route and ip address (should be pretty unique right?).

clay's avatar

One option would be to create a middleware that just adds a 'request_id' to the request.

request()->request->add(['request_id' => uniqid()]);

then depending on how you're handling logging, just call $request->request_id'; not sure if uniqid() would be sufficient or not in production, but it is a timestamp based unique id.

you could bind it in the service container using a service provider:

        $this->app->bind('request_id', function ($app) {            
            return request()->get('request_id');
        });

then you could access it and add it to your logs with resolve('request_id') if you're using Laravel 5.3 or with app()->make('request_id') or app('request_id') in earlier versions.

1 like
clay's avatar

Also, Nginx and Apache have modules, that when enabled, will add a unique id to each request. This id will be added to the $_SERVER super global. Do some googling and you should be able to find a lot of info.

jmadlena's avatar

Thanks for all of the suggestions! I think clay's option of using middleware and a service provider is a good way to do this without a lot of fuss.

Thanks for the help, everyone!

tomschlick's avatar

I would suggest using ramsey/uuid for the request id.

martin-juul's avatar

If anyone else in the future comes across this post, the safest way to add the id is on the attributes property on the request instance. Otherwise it'll be added as a $_POST parameter, which is unsafe, as a user could set it - or you could be using the parameter name yourself unknowingly.

Instead add it to the attributes property on the request instance, as that's meant for adding custom attributes. :)

Middleware:

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $request->attributes->add(['request_id' => Uuid::uuid6()->toString()]);

        return $next($request);
    }

Service provider:

public function boot(): void
{
        $this->app->bind('request_id', static function ($app) {
            return request()->attributes->get('request_id');
        });
}
5 likes
jmadlena's avatar
jmadlena
OP
Best Answer
Level 4

Just in case someone comes across this answer years later, the Laravel 10 documentation describes how to do this very simply: https://laravel.com/docs/10.x/logging#contextual-information

It's similar in that you use middleware to generate the request ID, but using Log::withContext you can directly add the request ID to all subsequent logs. So no need to register a service provider and call it manually every time you log something.

2 likes
Snapey's avatar

@sadekd think you need to update readme.md to at least say what the package does.

1 like

Please or to participate in this conversation.