laravel 请求和响应日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27138316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:28:41  来源:igfitidea点击:

laravel request and response logging

logginglaravel

提问by Ulterior

I am trying to catch an issue regarding a certain laravel response, so I am using the after filter to log all activity, but I cannot figure out how to dump request and response information to the log.

我试图捕捉有关某个 laravel 响应的问题,因此我使用 after 过滤器来记录所有活动,但我无法弄清楚如何将请求和响应信息转储到日志中。

App::after(
    function ($request, $response) {
        Log::info('access.log', array('request' => $request->headers, 'response' => $response->headers));
    }
);

This code doesnt give out status code information for response, which I am mostly interested.

这段代码没有给出响应的状态码信息,这是我最感兴趣的。

Is there a way to see what comes out on the final stage of passing information to web server? Something like this?

有没有办法查看在将信息传递到 Web 服务器的最后阶段会发生什么?像这样的东西?

HTTP/1.1 200 OK
Date: Tue, 25 Nov 2014 22:35:17 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.4.34-1+deb.sury.org~lucid+1
Cache-Control: no-cache, max-age=0
Expires: Tue, 25 Nov 2014 22:35:17 GMT
Content-Type: application/json
Via: 1.1 localhost:8080
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Length: 59

{"success":true,"result":{"min":5.7,"mean":9.7,"max":14.2}}

回答by jedrzej.kurylo

While status code is finally returned in the headers, at this stage you won't find it in the $headers attribute. You can still get the status code by calling $response->getStatusCode()

虽然状态代码最终在标头中返回,但在此阶段您不会在 $headers 属性中找到它。您仍然可以通过调用$response->getStatusCode()来获取状态码

回答by davejal

Although this is an old post, I recently wanted to see all incoming requests and all outgoing responses in Laravel.

虽然这是一篇旧帖子,但我最近想在 Laravel 中查看所有传入请求和所有传出响应。

This packagesupports both laravel 5 and 5.1, so I'm assuming you could also use it.

这个包支持 laravel 5 和 5.1,所以我假设你也可以使用它。

To install:

安装:

Composer

Add prettus/laravel-request-logger to the "require" section of your composer.json file.

"prettus/laravel-request-logger": "1.0.*" Run composer update to get the latest version of the package.

or Run

composer require prettus/laravel-request-logger 

direct in your terminal

Laravel

In your config/app.php add 'Prettus\RequestLogger\Providers\LoggerServiceProvider' to the end of the providers array:

'providers' => array(
...,
'Prettus\RequestLogger\Providers\LoggerServiceProvider', ), 

Publish Configuration

php artisan vendor:publish --provider="Prettus\RequestLogger\Providers\LoggerServiceProvider" Configuration

In your config/request-logger.phpfile, you can change configuration for logger

'logger' => [

'enabled'   => true,

'handlers'  => ['Prettus\RequestLogger\Handler\HttpLoggerHandler'],

'file'      => storage_path("logs/http.log"),

'level'     => 'info',

'format'    => 'common' ]

作曲家

将 prettus/laravel-request-logger 添加到 composer.json 文件的“require”部分。

"prettus/laravel-request-logger": "1.0.*" Run composer update to get the latest version of the package.

或运行

composer require prettus/laravel-request-logger 

直接在你的终端

Laravel

在您的 config/app.php 中,将 'Prettus\RequestLogger\Providers\LoggerServiceProvider' 添加到 providers 数组的末尾:

'providers' => array(
...,
'Prettus\RequestLogger\Providers\LoggerServiceProvider', ), 

发布配置

php artisan vendor:publish --provider="Prettus\RequestLogger\Providers\LoggerServiceProvider" Configuration

在您的config/request-logger.php文件中,您可以更改记录器的配置

'logger' => [

'enabled'   => true,

'handlers'  => ['Prettus\RequestLogger\Handler\HttpLoggerHandler'],

'file'      => storage_path("logs/http.log"),

'level'     => 'info',

'format'    => 'common' ]

Examples:

例子:

{method} {full-url}

[2015-04-03 00:00:00] local.INFO: GET http://prettus.local/user/1?param=lorem["REQUEST"] {method} {full-url} {remote-addr} {port}

[2015-04-03 00:00:00] local.INFO: GET http://prettus.local/user/1?param=lorem192.168.10.1 80 ["REQUEST"] {method} {root} {url} {full-url} {path} {decoded-path} {remote-addr} {format} {scheme} {port} {query-string}

[2015-04-03 00:00:00] local.INFO: GET http://prettus.localhttp://prettus.local/user/1http://prettus.local/user/1?param=loremuser/1 user/1 192.168.10.1 html http 80 param=lorem ["REQUEST"] [{status}] HTTP:{http-version} {content}

[2015-04-03 00:00:00] local.INFO: [200] HTTP:1.1 {"id":1,"name":"Anderson Andrade", "email":"[email protected]"} ["RESPONSE"]

{方法} {完整网址}

[2015-04-03 00:00:00] local.INFO: GET http://prettus.local/user/1?param=lorem["REQUEST"] {method} {full-url} {remote-addr} {港口}

[2015-04-03 00:00:00] local.INFO: GET http://prettus.local/user/1?param=lorem192.168.10.1 80 ["REQUEST"] {method} {root} {url} {full-url} {path} {decoded-path} {remote-addr} {format} {scheme} {port} {query-string}

[2015-04-03 00:00:00] local.INFO: GET http://prettus.local http://prettus.local/user/1 http://prettus.local/user/1?param=loremuser/1 user/1 192.168.10.1 html http 80 param=lorem ["REQUEST"] [{status}] HTTP:{http-version} {content}

[2015-04-03 00:00:00] local.INFO: [200] HTTP:1.1 {"id":1,"name":"Anderson Andrade", "email":"[email protected]"} [“回复”]

回答by Dmitriy Lezhnev

Along with dumping request/response to log you can try this serviceto get nice UI.

除了将请求/响应转储到日志之外,您还可以尝试使用此服务来获得漂亮的 UI。