Skip to content

[close #10] - add specialized loggers #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
html
bin
tests/foo.log
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 1.1

- [close #10] - added specialized loggers

# Version 1.0

- [close #1] - added missing unit tests
11 changes: 11 additions & 0 deletions UPGRADE-1.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ Introduced new interface to talk with Symfony requests and responses.

- logSymfonyRequest
- logSymfonyResponse
- logRequestObject

## `Refresh command`

`./refresh` command now checkout master branch, pull latest master branch and remove all merged branches.

## `Sensorario\Develog\Logger\*` classes

New specific logger have been introduced.

- `Sensorario\Develog\Logger\HttpLogger`
- `Sensorario\Develog\Logger\SymfonyLogger`

## `Sensorario\Develog\Request\HttpRequestObject`

Is a wrapper of `$_SERVER` var.
6 changes: 6 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Upgrade from 1.x

## `Sensorario\Develog\Logger`

Has been removed and replaces with.

- `Sensorario\Develog\Logger\HttpLogger`
- `Sensorario\Develog\Logger\SymfonyLogger`
15 changes: 15 additions & 0 deletions src/Sensorario/Develog/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

/** @deprecate */
class Logger extends PsrLogger implements
\Sensorario\Develog\SymfonyLoggerInterface
{
Expand Down Expand Up @@ -67,6 +68,20 @@ public function logRequest(Request $request)
$this->writeLog("> ");
}

public function logRequestObject(HttpRequestObject $request) : void
{
$this->writeLog(
"> " . $request->getHttpVerb() .
" " . $request->getRequestUri() .
" HTTP/1.1"
);

$this->writeLog("> Host: " . $request->getRemoteAddress());
$this->writeLog("> User-Agent: " . $request->getUserAgent());
$this->writeLog("> Accept: " . $request->getAccept());
$this->writeLog("> ");
}

public function logResponse(Response $response)
{
$this->writeLog("< HTTP/1.1 200 OK");
Expand Down
57 changes: 57 additions & 0 deletions src/Sensorario/Develog/Logger/AbstractLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Sensorario\Develog\Logger;

abstract class AbstractLogger
{
protected $handler;

protected $logFile;

public function getHandler()
{
if (!$this->handler) {
if (!$this->logFile) {
throw new \RuntimeException(
'Oops! Any log file defined'
);
}

$this->handler = fopen($this->logFile, 'a+');
}

return $this->handler;
}

public function setLogFile($logFile) : void
{
$this->logFile = $logFile;
}

public function getLogFile() : string
{
return $this->logFile;
}

public function __destruct()
{
if ($this->handler) {
fclose($this->getHandler());
}
}

public function getTime() : string
{
$now = new \DateTime('now');
$format = '[Y-m-d H:i:s]';

return $now->format($format);
}

protected function writeLog($message, $level = 'INFO') : void
{
$level = strtoupper($level);
$message = $this->getTime() . " log.$level $message\n";
fwrite($this->getHandler(), $message);
}
}
37 changes: 37 additions & 0 deletions src/Sensorario/Develog/Logger/HttpLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Sensorario\Develog\Logger;

use Sensorario\Develog\Request\HttpRequestObject;

final class HttpLogger extends AbstractLogger
{
public function logRequest(HttpRequestObject $request) : void
{
$this->writeLog(
"> " . $request->getHttpVerb() .
" " . $request->getRequestUri() .
" HTTP/1.1"
);

$this->writeLog("> Host: " . $request->getRemoteAddress());
$this->writeLog("> User-Agent: " . $request->getUserAgent());
$this->writeLog("> Accept: " . $request->getAccept());
$this->writeLog("> ");
}

public function logResponse(Response $response) : void
{
$this->writeLog("< HTTP/1.1 200 OK");
$this->writeLog("< Date: " . (new \DateTime('now'))->format('D, d M Y H:i:s'));
$this->writeLog("< Content-Type: applicarion/json");
$this->writeLog("<");
$lines = explode("\n", $response->getContent());

foreach ($lines as $line) {
$this->writeLog($line);
}

$this->writeLog("");
}
}
7 changes: 7 additions & 0 deletions src/Sensorario/Develog/Logger/LoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Sensorario\Develog\Logger;

interface LoggerInterface
{
}
22 changes: 22 additions & 0 deletions src/Sensorario/Develog/Logger/SymfonyLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Sensorario\Develog\Logger;

use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

final class SymfonyLogger extends AbstractLogger
{
public function logSymfonyRequest(SymfonyRequest $request) : void
{
$this->writeLog(
'> ' . $request->server->get('REQUEST_METHOD') .
' ' . $request->server->get('REQUEST_URI')
);
}

public function logSymfonyResponse(SymfonyResponse $response) : void
{
$this->writeLog("< " . $response->getContent());
}
}
60 changes: 60 additions & 0 deletions src/Sensorario/Develog/Request/HttpRequestObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Sensorario\Develog\Request;

final class HttpRequestObject
{
const REQUEST_METHOD = 'REQUEST_METHOD';
const REQUEST_URI = 'REQUEST_URI';
const REMOTE_ADDR = 'REMOTE_ADDR';
const HTTP_USER_AGENT = 'HTTP_USER_AGENT';
const HTTP_ACCEPT = 'HTTP_ACCEPT';

private $params;

private function __construct(array $params)
{
$this->params = $params;
}

public static function fromArray(array $params)
{
return new self($params);
}

public static function handleRequest()
{
return new self([
self::REQUEST_METHOD=> $_SERVER['REQUEST_METHOD'],
self::REQUEST_URI => $_SERVER['REQUEST_URI'],
self::REMOTE_ADDR => $_SERVER['REMOTE_ADDR'],
self::HTTP_USER_AGENT => $_SERVER['HTTP_USER_AGENT'],
self::HTTP_ACCEPT => $_SERVER['HTTP_ACCEPT'],
]);
}

public function getHttpVerb()
{
return $this->params[self::REQUEST_METHOD];
}

public function getRequestUri()
{
return $this->params[self::REQUEST_URI];
}

public function getRemoteAddress()
{
return $this->params[self::REMOTE_ADDR];
}

public function getUserAgent()
{
return $this->params[self::HTTP_USER_AGENT];
}

public function getAccept()
{
return $this->params[self::HTTP_ACCEPT];
}
}
18 changes: 18 additions & 0 deletions src/Sensorario/Develog/Request/RequestInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Sensorario\Develog\Request;

interface RequestInterface
{
public static function handleRequest();

public function getHttpVerb();

public function getRequestUri();

public function getRemoteAddress();

public function getUserAgent();

public function getAccept();
}