简体   繁体   中英

laravel request and response logging

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.

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?

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}}

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()

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

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

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.php file, you can change configuration for logger

 '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=lorem 192.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=lorem user/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":"contato@andersonandra.de"} ["RESPONSE"]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM