Skip to content

Commit

Permalink
LoggingMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar authored and Milan Felix Šulc committed Oct 5, 2018
1 parent 6695166 commit 677871a
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The middlewares / relay conception is a strong pattern with many benefits.
- [BasicAuthMiddleware](#basicauthmiddleware)
- [BuilderMiddleware](#buildermiddleware)
- [EnforceHttpsMiddleware](#enforcehttpsmiddleware)
- [LoggingMiddleware](#loggingmiddleware)
- [PresenterMiddleware](#presentermiddleware)
- [SecurityMiddleware](#securitymiddleware)
- [TracyMiddleware](#tracymiddleware)
Expand Down Expand Up @@ -269,6 +270,17 @@ middleware:
- Contributte\Middlewares\EnforceHttpsMiddleware
```

#### `LoggingMiddleware`

Log uri for each request.
Also removes password from that uri for security reasons.

```yaml
middleware:
middlewares:
- Contributte\Middlewares\LoggingMiddleware($psr3Logger)
```

#### `PresenterMiddleware`

This middleware simulates original nette application behaviours. It converts Psr7Request to `Nette\Application\Request`
Expand Down
30 changes: 30 additions & 0 deletions src/LoggingMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Contributte\Middlewares;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;

class LoggingMiddleware implements IMiddleware
{

/** @var LoggerInterface */
private $logger;

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

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
{
$uri = $request->getUri();
[$user, $password] = explode(':', $uri->getUserInfo());
$loggableUri = (string) $uri->withUserInfo($user, ''); // Remove password for security reasons
$this->logger->info($loggableUri);

return $next($request, $response);
}

}
124 changes: 124 additions & 0 deletions tests/cases/LoggingMiddleware.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php declare(strict_types = 1);

/**
* Test: LoggingMiddleware
*/

use Contributte\Middlewares\LoggingMiddleware;
use Contributte\Psr7\Psr7ResponseFactory;
use Contributte\Psr7\Psr7ServerRequestFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Tester\Assert;

require_once __DIR__ . '/../bootstrap.php';

test(function (): void {
$logger = new class implements LoggerInterface
{

/** @var string */
private $url = '';

/**
* @param string $message
* @param mixed[] $context
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function emergency($message, array $context = []): void
{
}

/**
* @param string $message
* @param mixed[] $context
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function alert($message, array $context = []): void
{
}

/**
* @param string $message
* @param mixed[] $context
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function critical($message, array $context = []): void
{
}

/**
* @param string $message
* @param mixed[] $context
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function error($message, array $context = []): void
{
}

/**
* @param string $message
* @param mixed[] $context
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function warning($message, array $context = []): void
{
}

/**
* @param string $message
* @param mixed[] $context
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function notice($message, array $context = []): void
{
}

/**
* @param string $message
* @param mixed[] $context
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function info($message, array $context = []): void
{
$this->url = $message;
}

/**
* @param string $message
* @param mixed[] $context
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function debug($message, array $context = []): void
{
}

/**
* @param string $message
* @param mixed[] $context
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function log($level, $message, array $context = []): void
{
}

public function get(): string
{
return $this->url;
}

};

$response = Psr7ResponseFactory::fromGlobal();
$middleware = new LoggingMiddleware($logger);
$response = $middleware(
Psr7ServerRequestFactory::fromSuperGlobal()->withNewUri('https://user:password@example.com/foo/bar'),
$response,
function (ServerRequestInterface $psr7Request, ResponseInterface $psr7Response): ResponseInterface {
return $psr7Response;
}
);

Assert::same('https://user@example.com/foo/bar', $logger->get());
});

0 comments on commit 677871a

Please sign in to comment.