Add request logs

This commit is contained in:
Alexander Pankratov 2023-10-03 23:51:08 +02:00
parent 1e96b4ede3
commit 163bcbd826
3 changed files with 102 additions and 5 deletions

View File

@ -0,0 +1,79 @@
<?php
namespace TelegramApiServer\Server;
use Amp\Http\Server\Middleware;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler;
use Amp\Http\Server\Response;
use Psr\Log\LoggerInterface as PsrLogger;
use Psr\Log\LogLevel;
class AccessLoggerMiddleware implements Middleware
{
public function __construct(
private readonly PsrLogger $logger,
) {
}
public function handleRequest(Request $request, RequestHandler $requestHandler): Response
{
$method = $request->getMethod();
$uri = (string)$request->getUri();
$protocolVersion = $request->getProtocolVersion();
$remote = Server::getClientIp($request);
$context = [
'method' => $method,
'uri' => $uri,
'client' => $remote,
];
try {
$response = $requestHandler->handleRequest($request);
} catch (\Throwable $exception) {
$this->logger->warning(
\sprintf(
'Client exception for "%s %s" HTTP/%s %s',
$method,
$uri,
$protocolVersion,
$remote
),
$context
);
throw $exception;
}
$status = $response->getStatus();
$reason = $response->getReason();
$context = [
'request' => $context,
'response' => [
'status' => $status,
'reason' => $reason,
],
];
$level = $status < 400 ? LogLevel::INFO : LogLevel::NOTICE;
$this->logger->log(
$level,
\sprintf(
'"%s %s" %d "%s" HTTP/%s %s',
$method,
$uri,
$status,
$reason,
$protocolVersion,
$remote
),
$context
);
return $response;
}
}

View File

@ -47,11 +47,14 @@ class Router
private function setRoutes(): void
{
$authorization = new Authorization();
$apiHandler = stackMiddleware(ApiController::getRouterCallback(ApiExtensions::class), $authorization);
$systemApiHandler = stackMiddleware(SystemController::getRouterCallback(SystemApiExtensions::class), $authorization);
$eventsHandler = stackMiddleware(EventsController::getRouterCallback($this->server), $authorization);
$logHandler = stackMiddleware(LogController::getRouterCallback($this->server), $authorization);
$middlewares = [
new AccessLoggerMiddleware(Logger::getInstance()),
new Authorization()
];
$apiHandler = stackMiddleware(ApiController::getRouterCallback(ApiExtensions::class), ...$middlewares);
$systemApiHandler = stackMiddleware(SystemController::getRouterCallback(SystemApiExtensions::class), ...$middlewares);
$eventsHandler = stackMiddleware(EventsController::getRouterCallback($this->server), ...$middlewares);
$logHandler = stackMiddleware(LogController::getRouterCallback($this->server), ...$middlewares);
foreach (['GET', 'POST'] as $method) {
$this->router->addRoute($method, '/api/{method}[/]', $apiHandler);

View File

@ -6,6 +6,7 @@ use Amp;
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\Driver\ConnectionLimitingServerSocketFactory;
use Amp\Http\Server\Driver\DefaultHttpDriverFactory;
use Amp\Http\Server\Request;
use Amp\Http\Server\SocketHttpServer;
use Amp\Socket\InternetAddress;
use Amp\Sync\LocalSemaphore;
@ -89,4 +90,18 @@ class Server
return $config;
}
public static function getClientIp(Request $request): string
{
$remote = $request->getClient()->getRemoteAddress()->toString();
$hostArray = explode(':', $remote);
if (count($hostArray) >= 2) {
$port = (int)array_pop($hostArray);
if ($port > 0 && $port <= 65353) {
$remote = implode(':', $hostArray);
}
}
return $remote;
}
}