Skip to content

Commit

Permalink
Merge pull request #8 from uniplaces/NI/Improve-request-logger-events
Browse files Browse the repository at this point in the history
[RFC][NI] Update Request logger and minor fixes
  • Loading branch information
tlfbrito authored May 3, 2018
2 parents e7fb9a9 + aa7b82d commit c9dddec
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2,125 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea
/vendor
/phpunit.xml
composer.lock
79 changes: 32 additions & 47 deletions EventListener/RequestLoggerEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace Uniplaces\RequestLoggerBundle\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

/**
* RequestLoggerEventListener
Expand All @@ -17,11 +16,6 @@ final class RequestLoggerEventListener
*/
private $logger;

/**
* @var int
*/
private $requestStartTime;

/**
* @param LoggerInterface $logger
*/
Expand All @@ -31,47 +25,31 @@ public function __construct(LoggerInterface $logger)
}

/**
* @param GetResponseEvent $event
*/
public function onRequest(GetResponseEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
}

$this->requestStartTime = microtime(true);
}

/**
* @param PostResponseEvent $event
* @param FilterResponseEvent $event
*/
public function onTerminate(PostResponseEvent $event): void
public function onResponse(FilterResponseEvent $event): void
{
$latency = (int)((microtime(true) - $this->requestStartTime) * 1000);

$request = $event->getRequest();
$method = $request->getMethod();
$path = $request->getBasePath();
$contentType = $request->getContentType();
$clientIp = $request->getClientIp();
$userAgent = $request->headers->get('User-Agent');

$response = $event->getResponse();
$statusCode = $response->getStatusCode();
$message = \sprintf(
'Response %s for "%s %s"',
[$statusCode, $request->getMethod(), $request->getRequestUri()]
);

$queryString = $request->getQueryString();
$httpVersion = $request->getProtocolVersion();
$responseSize = (int)$response->headers->get('Content-Length');
$message = "{$clientIp} \"{$method} {$path}/{$queryString} {$httpVersion} {$statusCode} {$responseSize}\"";

$this->logRequest(
$statusCode,
$this->logResponse(
$message,
[
'method' => $method,
'method' => $request->getMethod(),
'path' => $path,
'uri' => $request->getRequestUri(),
'content-type' => $contentType,
'latency' => $latency,
'latency' => $this->getTime($request),
'client-ip' => $clientIp,
'status_code' => $statusCode,
'user-agent' => $userAgent
Expand All @@ -80,24 +58,31 @@ public function onTerminate(PostResponseEvent $event): void
}

/**
* @param int $statusCode
* @param string $message
* @param array $fields
* @param Request $request
*
* @return float|null
*/
private function logRequest(int $statusCode, string $message, array $fields): void
public function getTime(Request $request): ?float
{
if ($statusCode < Response::HTTP_BAD_REQUEST) {
$this->logger->info($message, $fields);

return;
if (!$request->server) {
return null;
}

if ($statusCode < Response::HTTP_INTERNAL_SERVER_ERROR) {
$this->logger->warning($message, $fields);
$startTime = $request->server->get(
'REQUEST_TIME_FLOAT',
$request->server->get('REQUEST_TIME')
);
$time = microtime(true) - $startTime;

return;
}
return round($time * 1000);
}

$this->logger->error($message, $fields);
/**
* @param string $message
* @param array $fields
*/
private function logResponse(string $message, array $fields): void
{
$this->logger->info($message, $fields);
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Uniplaces Request Logger Bundle
# Request Logger Bundle

[![StyleCI](https://styleci.io/repos/129761376/shield?branch=master)](https://styleci.io/repos/129761376)

Expand Down
17 changes: 8 additions & 9 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
services:
request_logger_bundle.request_logger:
class: Uniplaces\RequestLoggerBundle\EventListener\RequestLoggerEventListener
arguments: ["@logger"]
_defaults:
autowire: true
public: false
autoconfigure: true

Uniplaces\RequestLoggerBundle\EventListener\RequestLoggerEventListener:
tags:
-
name: kernel.event_listener
event: kernel.request
method: onRequest
-
name: kernel.event_listener
event: kernel.terminate
method: onTerminate
event: kernel.response
method: onResponse
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Symfony bundle to log information about the request like method, uri, latency",
"type": "bundle",
"require": {
"php": "^7.1.0",
"symfony/http-kernel": "^4.0",
"psr/log": "^1.0",
"symfony/config": "^4.0",
Expand Down
Loading

0 comments on commit c9dddec

Please sign in to comment.