Saturday, December 17, 2016

Log / Track / Lookup every request & response in Laravel

Download example code from here

First create a php file under "App\Http\Middleware" named "LogAfterRequest.php" with following contents:


<?php
namespace App\Http\Middleware;

use Closure;

class LogAfterRequest
{

    public function handle($request, Closure $next)
    {
        $this->logRequest($request);
        return $next($request);
    }

    public function terminate($request, $response)
    {
        $this->logResponse($request, $response);
    }

    private function logResponse($request, $response)
    {
        $response = (array)$response;
        $txt = $this->objectToString($response);
        file_put_contents("logs/res_" . time() . "_" . rand(1, 100) . "_" . $this->makeLogFilename($request) . ".txt", $txt);
    }

    private function logRequest($request)
    {
        //$this->cleanLogDirectory();
        $log = json_encode((array)$request->server);
        $log = json_decode("{\"" . substr($log, 15))->parameters;
        $txt = "";
        foreach ($log as $k => $v) {
            $txt .= $k . "===" . str_replace("\n", "", $v) . "\r\n";
        }
        file_put_contents("logs/req_" . time() . "_" . rand(1, 100) . "_" . $this->makeLogFilename($request) . ".txt", $txt);
    }

    private function objectToString($obj, $prefix = "")
    {
        $txt = "";
        foreach ($obj as $k => $v) {
            $t = gettype($v);
            switch ($t) {
                case "object":
                case "array":
                    $k = $k . "\r\n";
                    $v = "\r\n" . $this->objectToString($v, $prefix . "\t");
                    break;
                default:
                    $v = str_replace("\n", "", $v);
            }
            $k = preg_replace("/[^a-zA-Z0-9]+/", "", $k);
            $txt .= $prefix . $k . "===" . $v . "\r\n";
        }
        return $txt;
    }

    private function cleanLogDirectory()
    {
        $files = scandir("logs");
        foreach ($files as $file) {
            if ($file != "." && $file != ".." && !is_dir($file) && file_exists($file)) {
                unlink($file);
            }
        }
    }

    private function makeLogFilename($request)
    {
        $log = json_encode((array)$request->server);
        $log = json_decode("{\"" . substr($log, 15))->parameters;
        $name = str_replace("/", "_", $log->REQUEST_URI);
        return preg_replace("/[^a-zA-Z0-9]+/", "_", $name);
    }
}

?>

and add the following line "\App\Http\Middleware\LogAfterRequest::class" to Karnel.php as follows: 


<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\LogAfterRequest::class
    ];

    protected $middlewareGroups = [
        'user' => [
            \App\Http\Middleware\LogAfterRequest::class,
            \App\Http\Middleware\LogAfterRequest2::class
        ]
    ];
}

?>

No comments:

Post a Comment