PHP code example of knovator / httplogger

1. Go to this page and download the library: Download knovator/httplogger 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/ */

    

knovator / httplogger example snippets


return [

   /*
       * Filter out body fields which will never be logged.
       */
       'except'      => [
           'password',
           'password_confirmation',
       ],
   
       /* Default log channel.*/
       'log_channel' => 'custom_log',
   
   
        /* logged user columns */
       'action_by_columns' => [
           'id',
           'first_name',
           'last_name',
           'email',
           'phone'
       ],

];

// in `app/Http/Kernel.php`

protected $middleware = [
    // ...
    
    \Knovator\HttpLogger\Middleware\HttpLoggerMiddleware::class
];

// in `config/logging.php`

    'custom_log' => [
            'driver' => 'daily',
            'path'   => env('HTTP_LOGGER_FILE_NAME') ? storage_path('logs/' . env('HTTP_LOGGER_FILE_NAME') . '.log') : storage_path('logs/laravel.log'),
        ],

// Example implementation from `\knovator\logger\src\LogNonGetRequests`

public function shouldLogRequest(Request $request): bool
{
   return in_array(strtolower($request->method()), ['post', 'put', 'patch', 'delete']);
}

// Example implementation from `\knovator\http-logger\src\DefaultLogWriter`

 public function logRequest(Request $request) {
        $fileNames = [];
        $method = strtoupper($request->getMethod());
        $uri = $request->getPathInfo();
        $bodyAsJson = json_encode($this->input($request, config('http-logger.except')));
        $message = "{$method} {$uri} - Action From: {$this->clientInformation($request)} - Body: {$bodyAsJson}";
        $this->uploadedFiles($request->files, $fileNames);

        if (!empty($fileNames)) {
            $message .= " - Files: " . json_encode($fileNames);
        }

        if (auth()->guard('api')->check()) {
            $user = auth()->guard('api')->user()->first(config('http-logger.action_by_columns'))
                          ->toArray();
            $userBody = json_encode($user);
            $message .= " - Action By: {$userBody}";
        }
        

        $channel = config('http-logger.log_channel');

        Log::channel($channel)->info($message);
    }
bash
php artisan vendor:publish --tag=config