🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
创建文件**app/Http/Middleware/RequestLog.php** ``` <?php namespace App\Http\Middleware; use Closure; use Monolog\Logger; use Monolog\Handler\StreamHandler; class RequestLog { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $response = $next($request); $arr = parse_url($request->fullUrl()); $path = array_get($arr, 'path', ''); $log_file = 'logs/'.trim($path, '/').'/'.date("Y-m-d").'.log'; // create a log channel $log = new Logger('tracking'); $log->pushHandler(new StreamHandler(storage_path($log_file), Logger::INFO)); // add records to the log $log->info(json_encode([ 'path' => $path, 'method' => $request->method(), 'header' => $request->header(), 'request' => $request->all(), 'response' => $response->original, ], JSON_UNESCAPED_UNICODE)); return $response; } } ``` ## laravel 修改 修改app/Http/Kernel.php代码,给api增加中间件 \App\Http\Middleware\RequestLog::class ``` /** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], 'api' => [ 'throttle:60,1', 'bindings', \App\Http\Middleware\RequestLog::class, //新增加这一行代码 ], ]; ``` ## lumen修改 修改:bootstrap/app.php 中 ~~~ $app->middleware([ ~~~ ``` $app->middleware([ \App\Http\Middleware\CorsMiddleware::class, Modules\Core\Middleware\RemoveEmptyParams::class, Modules\Core\Middleware\HandleResponseMiddleware::class, Modules\Core\Middleware\AuthMiddleware::class, \App\Http\Middleware\RequestLog::class ]); ```