本文介绍了如何在laravel 5.2框架中记录每个响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码记录我的API的每个请求和响应,但是现在它不适用于Laravel 5.2.

I was using below code for logging each and every request and response for my API but now it's not working for Laravel 5.2.

我尝试使用 https://laravel.com/docs/5.2/middleware #terminable-middleware ,但不会成功.

I have tried to use https://laravel.com/docs/5.2/middleware#terminable-middleware but not succeed.

use Closure;
use Illuminate\Contracts\Routing\TerminableMiddleware;
use Illuminate\Support\Facades\Log;

class LogAfterRequest implements TerminableMiddleware {

    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        $logFile = 'log.txt';
        Log::useDailyFiles(storage_path().'/logs/'.$logFile);
        Log::info('app.requests', ['request' => $request->all(), 'response' => $response->getContent()]);
    }

}

有人可以建议我解决方案吗?

Can anyone suggest me the solution?

推荐答案

我已经找到了解决方案.问题是我在控制器方法中添加了"die",由于未执行终止功能,因此未生成日志.

I have got the solution. the issue was that i have added "die" in controller method due to which terminate function is not executing and so no log generated.

这篇关于如何在laravel 5.2框架中记录每个响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:10