gpt4 book ai didi

Attaching a unique identifier to laravel request(将唯一标识符附加到LARAVEL请求)

转载 作者:bug小助手 更新时间:2023-10-25 19:26:13 25 4
gpt4 key购买 nike



Is there a package for laravel that ads a unique identifier to each request in order to use it also for logs?

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



For example: I would know that request-id as12121-1212s-121 had an error and I could look into logs for any errors.

例如:我知道请求id as12121-1212s-121有错误,我可以在日志中查找任何错误。



That request-id would be seen in the UI and I could debug when getting a printscreen with the error from the client

该请求ID将显示在用户界面中,当从客户端获取带有错误的PrintScreen时,我可以进行调试


更多回答

Are you wanting to attach a unique ID to every single request? Or just errors?

您是否希望为每个请求附加一个唯一的ID?或者只是错误?

To every single request. This id will be referred to also in logs and stuff. I would like to do this from laravel. Other solution would be to have a proxy in front of my webserver that ads a header like X-Unique-ID

满足每一个请求。这个id也将在日志和东西中被引用。我想从拉拉维尔开始做这个。另一种解决方案是在我的Web服务器前面设置一个代理,它会通告一个标题,比如X-Unique-ID

For now I found thisp package that modifiers the request from thhe midldleware: github.com/lara-middleware/request-id/blob/master/src/… then I could use something like Log::getMonolog()->pushProcessor });

现在,我发现了这个修改中间件请求的isp包:github.com/lara-middleware/request-id/blob/master/src/…然后我可以使用类似Log::getMonolog()->presProcessor}的代码);

优秀答案推荐

You can use $request->fingerprint()

您可以使用$REQUEST->指纹()



This prints a uniqueid from your request and you can track it

这将打印您的请求的唯一ID,您可以对其进行跟踪



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()]
)));
}


If you need a unique id per request you can assign a uuid to the request via global middleware.

如果每个请求需要唯一的ID,您可以通过全球中间件为请求分配一个UUID。


For example:

例如:


<?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);
}
}



This package might help

这个套餐可能会有帮助



Laravel Request Logger

Laravel请求记录器


更多回答

"Get a unique fingerprint for the request / route / IP address."

“获取请求/路由/IP地址的唯一指纹。”

This is not unique "per request", if you hit the same uri, with the same method, etc it will return the same value. There is no time or session component

这不是唯一的“每个请求”,如果您使用相同的URI、相同的方法等等,它将返回相同的值。没有时间或会话组件

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com