Laravel8记录请求和返回日志

1,127次阅读
没有评论

相关环境:PHP7.4 + Laravel8.83.8

日志记录是项目必不可少的,他可以帮助我们排查一些问题,那这节我们就以接口请求与接口返回的记录作为重点。
项目里面的接口很多,我们不可能在每个方法里面人工的加 Log::info () 这种,这样太累也不好维护。所以我是使用中间件来做的:
第一步新建一个中间件

php artisan make:middleware AccessLog
便会在 app\Http\Middleware 下面生成 AccessLog.php,代码如下:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class AccessLog
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$traceId = md5(time() . mt_rand(1, 1000000));
// 记录请求信息
$requestMessage = ‘traceId’ => $traceId, ‘url’ => $request->url(), ‘method’ => $request->method(), ‘ip’ => $request->ips(), ‘headers’ => $request->header(‘Authorization’), ‘params’ => $request->all() ;
Log::info(“请求信息:”, $requestMessage);

    $respone = $next($request);
    $responeData = [
        'traceId' => $traceId,
        'respone' => json_decode($respone->getContent(), true) ?? ""
    ];

    Log::info("返回信息:", $responeData);

    return $respone;
}

}
以 $respone = $next ($request); 为界限,上面是请求下面是返回。
第二步配置全局路由
在 app\Http\Kernel.php 里的 $middleware 数组加入 AccessLog:

<?php

namespace App\Http;

use App\Http\Middleware\AccessLog;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
/**
* The application’s global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
AccessLog::class, //全局请求返回日志记录
];
这样所有请求都会进入这个 AccessLog 里面。

测试一下:
控制器代码:

<?php

namespace App\Http\Admin\User;

use App\Http\Admin\Controller;

class UserController extends Controller
{
public function detail()
{
$id = request()->input(“id”);
$data = UserAdmin::find($id, [‘id’, ‘mobile’]);
return response()->json([
‘code’ => 0,
‘message’ => “success”,
‘data’ => $data
]);
}
}
日志打印如下:

补充:
如果代码出现异常,我们发现日志会把这异常也记录下来,模拟一下,查个不存在的字段:

<?php

namespace App\Http\Admin\User;

use App\Http\Admin\Controller;

class UserController extends Controller
{
public function detail()
{
$id = request()->input(“id”);
$data = UserAdmin::find($id, [‘id’, ‘test’]); //不存在的test字段
return response()->json([
‘code’ => 0,
‘message’ => “success”,
‘data’ => $data
]);
}
}
这个时候去看日志,除了请求返回日志外,还有一个很长很长的异常日志【图片只截取一小部分,返回日志在下面没有截取出来】:

那如果你不想打印这个异常日志,也可以实现,在 app\Exceptions\Handler.php 里面有个 $dontReport 数组,他就是配置不会被记录到日志文件的异常类型数组。我们看异常日志,他是通过 Illuminate\Database\QueryException 类抛出的,那我们在 $dontReport 加上他:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
* 不会被记录到日志文件的异常类型数组
*
* @var array>
*/
protected $dontReport = [
\Illuminate\Database\QueryException::class,
];

/**
 * A list of the inputs that are never flashed for validation exceptions.
 *
 * @var array<int, string>
 */
protected $dontFlash = [
    'current_password',
    'password',
    'password_confirmation',
];

/**
 * Register the exception handling callbacks for the application.
 *
 * @return void
 */
public function register()
{
    $this->reportable(function (Throwable $e) {
        //
    });
}

public function render($request, Throwable $e)
{
    return response()->json([
        'code' => $e->getCode() ?? 1,
        'file' => $e->getFile(),
        'line' => $e->getLine(),
        'message' => $e->getMessage() ?? "error!",
        'data' => []
    ]);
}

}
再次运行接口,发现异常日志就不会记录了

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 10 月
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
钉钉又发新版本!把 AI 搬进每一次对话和会议

钉钉又发新版本!把 AI 搬进每一次对话和会议

钉钉又发新版本!把 AI 搬进每一次对话和会议 梦晨 2025-12-11 15:33:51 来源:量子位 A...
5天连更5次,可灵AI年末“狂飙式”升级

5天连更5次,可灵AI年末“狂飙式”升级

5天连更5次,可灵AI年末“狂飙式”升级 思邈 2025-12-10 14:28:37 来源:量子位 让更大规...
商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1 十三 2025-12-15 14:13:14 ...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
读懂2025中国AI走向!公司×产品×人物×方案,最值得关注的都在这里了

读懂2025中国AI走向!公司×产品×人物×方案,最值得关注的都在这里了

读懂2025中国AI走向!公司×产品×人物×方案,最值得关注的都在这里了 衡宇 2025-12-10 12:3...
5天连更5次,可灵AI年末“狂飙式”升级

5天连更5次,可灵AI年末“狂飙式”升级

5天连更5次,可灵AI年末“狂飙式”升级 思邈 2025-12-10 14:28:37 来源:量子位 让更大规...
戴尔 x OpenCSG,推出⾯向智能初创企业的⼀体化 IT 基础架构解决方案

戴尔 x OpenCSG,推出⾯向智能初创企业的⼀体化 IT 基础架构解决方案

戴尔 x OpenCSG,推出⾯向智能初创企业的⼀体化 IT 基础架构解决方案 十三 2025-12-10 1...
九章云极独揽量子位三项大奖:以“一度算力”重构AI基础设施云格局

九章云极独揽量子位三项大奖:以“一度算力”重构AI基础设施云格局

九章云极独揽量子位三项大奖:以“一度算力”重构AI基础设施云格局 量子位的朋友们 2025-12-10 18:...
乐奇Rokid这一年,一路狂飙不回头

乐奇Rokid这一年,一路狂飙不回头

乐奇Rokid这一年,一路狂飙不回头 梦瑶 2025-12-10 20:41:15 来源:量子位 梦瑶 发自 ...