将唯一标识符附加到每个请求


Attaching a unique identifier to laravel request

是否有一个用于laravel的包,为每个请求添加唯一标识符,以便也用于日志?

例如:我知道请求id as12121-1212s-121有一个错误,我可以查看任何错误的日志。

该请求id将在UI中看到,当从客户端获得错误的打印屏幕时,我可以调试

可以使用$request->fingerprint()

从你的请求中打印一个唯一的,你可以跟踪它

public function fingerprint()
{
    if (! $route = $this->route()) {
        throw new RuntimeException('Unable to generate fingerprint. Route unavailable.');
    }
    return sha1(implode('|', array_merge(
        $route->methods(),
        [$route->getDomain(), $route->uri(), $this->ip()]
    )));
}

如果你需要一个唯一的id 每个请求你可以通过全局中间件分配一个uuid给请求。

例如:

<?php
namespace App'Http'Middleware;
use Closure;
use Illuminate'Http'Request;
use Illuminate'Support'Str;
class RequestUniqueId
{
    public function handle(Request $request, Closure $next)
    {
        $uuid = (string) Str::uuid();
        $request->headers->set('X-Request-ID', $uuid);
        return $next($request);
    }
}

这个包可能会有帮助

Laravel Request Logger