Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}}
like image 820
Ulterior Avatar asked Nov 25 '14 22:11

Ulterior


2 Answers

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

like image 97
jedrzej.kurylo Avatar answered Oct 21 '22 20:10

jedrzej.kurylo


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":"[email protected]"} ["RESPONSE"]

like image 1
davejal Avatar answered Oct 21 '22 20:10

davejal