PHP code example of mtownsend / laravel-request-response-logger

1. Go to this page and download the library: Download mtownsend/laravel-request-response-logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mtownsend / laravel-request-response-logger example snippets


/*
 * Package Service Providers...
 */
Mtownsend\RequestResponseLogger\Providers\RequestResponseLoggerServiceProvider::class,

protected $routeMiddleware = [
    ...
    'log.requests.responses' => \Mtownsend\RequestResponseLogger\Middleware\LogRequestsAndResponses::class,
];

Route::post('/some/route', SomeController::class)->middleware(['log.requests.responses']);

protected $middlewareGroups = [
    ...
    'api' => [
        ...
        \Mtownsend\RequestResponseLogger\Middleware\LogRequestsAndResponses::class,
    ],
];

protected $middleware = [
    ...
    \Mtownsend\RequestResponseLogger\Middleware\LogRequestsAndResponses::class,
];

return [

    /**
     * The model used to manage the database table for storing request and response logs.
     */
    'logging_model' => \Mtownsend\RequestResponseLogger\Models\RequestResponseLog::class,

    /**
     * When logging requests and responses, should the logging action be
     * passed off to the queue (true) or run synchronously (false)?
     */
    'logging_should_queue' => false,

    /**
     * If stored json should be transformed into an array when retrieved from the database.
     * Set to `false` to receive as a regular php object.
     */
    'get_json_values_as_array' => true,

    /**
     * The class responsible for determining if a request should be logged.
     * 
     * Out of the box options are:
     * Mtownsend\RequestResponseLogger\Support\Logging\LogAll::class,
     * Mtownsend\RequestResponseLogger\Support\Logging\LogClientErrorsOnly::class,
     * Mtownsend\RequestResponseLogger\Support\Logging\LogSuccessOnly::class,
     */
    'should_log_handler' => \Mtownsend\RequestResponseLogger\Support\Logging\LogAll::class,

];

protected function schedule(Schedule $schedule)
{
    ...
    $schedule->command('request-response-logger:clean')->quarterly();
}



namespace App\Support\Logging;

use Illuminate\Http\Request;
use Mtownsend\RequestResponseLogger\Support\Logging\Contracts\ShouldLogContract;

class LogExternalRequests implements ShouldLogContract
{
    public $request;
    public $response;

    public function __construct(Request $request, $response)
    {
        $this->request = $request;
        $this->response = $response;
    }

    /**
     * Return a truth-y value to log the request and response.
     * Return false-y value to skip logging.
     * 
     * @return bool
     */
    public function shouldLog(): bool
    {
        // Custom logic goes here...
    }
}

[
    //...
    'should_log_handler' => \App\Support\Logging\LogExternalRequests::class,
]

use Mtownsend\RequestResponseLogger\Models\RequestResponseLog;

// Get every logged item with an http response code of 2XX:
RequestResponseLog::successful()->get();

// Get every logged item with an http response code that ISN'T 2XX:
RequestResponseLog::failed()->get();



namespace App\Models;

use Mtownsend\RequestResponseLogger\Models\RequestResponseLog as BaseRequestResponseLog;

class RequestResponseLog extends BaseRequestResponseLog
{
    //
}

'logging_model' => \App\Models\RequestResponseLog::class,
`
php artisan vendor:publish --provider="Mtownsend\RequestResponseLogger\Providers\RequestResponseLoggerServiceProvider"
`
php artisan migrate
`
php artisan request-response-logger:clean