feat : ajout de la lecture des logs symfony et docker (#3)
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
Reviewed-on: #3 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #3.
This commit is contained in:
57
src/State/SymfonyLogProvider.php
Normal file
57
src/State/SymfonyLogProvider.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\LogOutput;
|
||||
use App\Repository\EnvironmentRepository;
|
||||
use App\Repository\LogFileRepository;
|
||||
use App\Service\LogService;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final readonly class SymfonyLogProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EnvironmentRepository $environmentRepository,
|
||||
private LogFileRepository $logFileRepository,
|
||||
private LogService $logService,
|
||||
private RequestStack $requestStack,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): LogOutput
|
||||
{
|
||||
$envId = $uriVariables['id'] ?? null;
|
||||
$logFileId = $uriVariables['logFileId'] ?? null;
|
||||
|
||||
$environment = $envId ? $this->environmentRepository->find($envId) : null;
|
||||
if (null === $environment) {
|
||||
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $envId));
|
||||
}
|
||||
|
||||
$logFile = $logFileId ? $this->logFileRepository->find($logFileId) : null;
|
||||
if (null === $logFile || $logFile->getEnvironment()?->getId() !== $environment->getId()) {
|
||||
throw new NotFoundHttpException(sprintf('Log file "%s" not found.', $logFileId));
|
||||
}
|
||||
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
$lines = (int) ($request?->query->get('lines', '100') ?? 100);
|
||||
$level = $request?->query->get('level');
|
||||
|
||||
$content = $this->logService->getSymfonyLog(
|
||||
$logFile->getPath(),
|
||||
$lines,
|
||||
$level,
|
||||
);
|
||||
|
||||
$dto = new LogOutput();
|
||||
$dto->content = $content;
|
||||
$dto->lines = $lines;
|
||||
$dto->source = sprintf('symfony:%s', $logFile->getLabel());
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user