Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

In example below, we're making a call to an end-point and log the details of our request with onKernelRequest event listener.


Listeners.yml


services:
application_backend.event_listener.kernel_request:
class: Application\BackendBundle\EventListener\KernelRequestListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
arguments: [@logger]

KernelRequestListener.php


namespace Application\BackendBundle\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class KernelRequestListener
{
private $logger;
private $log = [];

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

public function onKernelRequest(GetResponseEvent $event)
{
$this->log['Event Name'] = $event->getName();
$this->log['Request Type V1'] = $event->isMasterRequest() ? 'Master' : 'Sub';
$this->log['Request Type V2'] = $event->getRequestType() ? 'Master' : 'Sub';
$this->log['Request Method'] = $event->getRequest()->getMethod();
$this->log['Request Format'] = $event->getRequest()->getRequestFormat();
$this->log['Base URL'] = $event->getRequest()->getBaseUrl();
$this->log['URI Path Info'] = $event->getRequest()->server->get('PATH_INFO');
$this->log['URI Query String'] = $event->getRequest()->getQueryString();
$this->log['Full URI'] = $event->getRequest()->getUri();
$this->log['Is Method Correct'] = $event->getRequest()->isMethod('GET') ? 'Correct method' : 'Incorrect method';
$this->log['Is URI Age Param Set'] = $event->getRequest()->attributes->get('age') ? 'Yes' : 'No';
$this->log['Controller'] = $event->getRequest()->attributes->get('_controller');
$this->log['Router'] = $event->getRequest()->attributes->get('_route');
$this->log['Client IP'] = $event->getRequest()->getClientIp();
$this->log['Router Parameters'] = json_encode($event->getRequest()->attributes->get('_route_params'));

$this->logger->info(json_encode($this->log));
}
}

Test


We're calling this end-point: http://football.local/app_dev.php/backend/user?page=1&limit=2


Content of dev.log file


[2015-07-04 20:57:44] app.INFO: {"Event Name":"kernel.request","Request Type V1":"Master","Request Type V2":"Master","Request Method":"GET","Request Format":"html","Base URL":"\/app_dev.php","URI Path Info":"\/backend\/user","URI Query String":"limit=2&page=1","Full URI":"http:\/\/football.local\/app_dev.php\/backend\/user?limit=2&page=1","Is Method Correct":"Correct method","Is URI Age Param Set":"No","Controller":"application_backend.controller.user:listAction","Router":"application_backend_user_list","Client IP":"127.0.0.1","Router Parameters":"[]"} [] []

Result


{
"Event Name": "kernel.request",
"Request Type V1": "Master",
"Request Type V2": "Master",
"Request Method": "GET",
"Request Format": "html",
"Base URL": "\/app_dev.php",
"URI Path Info": "\/backend\/user",
"URI Query String": "limit=2&page=1",
"Full URI": "http:\/\/football.local\/app_dev.php\/backend\/user?limit=2&page=1",
"Is Method Correct": "Correct method",
"Is URI Age Param Set": "No",
"Controller": "application_backend.controller.user:listAction",
"Router": "application_backend_user_list",
"Client IP": "127.0.0.1",
"Router Parameters": "[]"
}