12. 日志和调试
日志和调试
⬆️ 转到顶部 ⬅️ 上一个 (Factories) ➡️ 下一个 (API)
日志记录参数
你可以使用 Log::info()
,或使用更短的 info()
额外参数信息,来了解更多发生的事情
Log::info('User failed to login.', ['id' => $user->id]);
更方便的DD
你可以在你的Eloquent句子或者任何集合结尾添加 ->dd()
,而不是使用 dd($result)
。
// 以前
$users = User::where('name', 'Taylor')->get();
dd($users);
// 现在
$users = User::where('name', 'Taylor')->get()->dd();
使用context日志
在最新的Laravel 8.49中:Log::withContext()
将帮助您区分不同请求之间的日志消息。
如果你创建了中间件并且设置了context,所有的长消息将包含在context中,你将会搜索更容易。
public function handle(Request $request, Closure $next)
{
$requestId = (string) Str::uuid();
Log::withContext(['request-id' => $requestId]);
$response = $next($request);
$response->header('request-id', $requestId);
return $response;
}
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。