AzuraCast/src/Controller/Api/Admin/LogsAction.php

122 lines
3.6 KiB
PHP
Raw Normal View History

<?php
2021-07-19 05:53:45 +00:00
declare(strict_types=1);
2022-06-19 22:28:31 +00:00
namespace App\Controller\Api\Admin;
2022-06-19 22:28:31 +00:00
use App\Controller\Api\Traits\HasLogViewer;
use App\Environment;
use App\Exception;
use App\Http\Response;
use App\Http\ServerRequest;
use App\Service\ServiceControl;
use Psr\Http\Message\ResponseInterface;
2022-06-19 22:28:31 +00:00
final class LogsAction
{
2022-06-19 22:28:31 +00:00
use HasLogViewer;
2021-04-23 05:24:12 +00:00
public function __construct(
private readonly Environment $environment,
private readonly ServiceControl $serviceControl,
2021-04-23 05:24:12 +00:00
) {
}
public function __invoke(
ServerRequest $request,
2022-06-19 22:28:31 +00:00
Response $response,
?string $log = null
): ResponseInterface {
2022-06-19 22:28:31 +00:00
$logPaths = $this->getGlobalLogs();
2022-06-19 22:28:31 +00:00
if (null === $log) {
$router = $request->getRouter();
return $response->withJson(
[
'logs' => array_map(
function (string $key, array $row) use ($router) {
$row['key'] = $key;
$row['links'] = [
'self' => $router->named(
2022-06-19 22:28:31 +00:00
'api:admin:log',
[
'log' => $key,
]
),
];
return $row;
},
array_keys($logPaths),
array_values($logPaths)
),
]
);
}
if (!isset($logPaths[$log])) {
throw new Exception('Invalid log file specified.');
}
2022-06-19 22:28:31 +00:00
return $this->streamLogToResponse(
$request,
$response,
2022-06-19 22:28:31 +00:00
$logPaths[$log]['path'],
$logPaths[$log]['tail'] ?? true
);
}
/**
2021-07-19 05:53:45 +00:00
* @return array<string, array>
*/
private function getGlobalLogs(): array
{
$tempDir = $this->environment->getTempDirectory();
$logPaths = [];
$logPaths['azuracast_log'] = [
'name' => __('AzuraCast Application Log'),
'path' => $tempDir . '/app-' . gmdate('Y-m-d') . '.log',
'tail' => true,
];
if ($this->environment->isDocker()) {
$langServiceLog = __('Service Log: %s (%s)');
foreach ($this->serviceControl->getServiceNames() as $serviceKey => $serviceName) {
$logPath = $tempDir . '/service_' . $serviceKey . '.log';
if (is_file($logPath)) {
$logPaths['service_' . $serviceKey] = [
'name' => sprintf($langServiceLog, $serviceKey, $serviceName),
'path' => $logPath,
'tail' => true,
];
}
}
} else {
$logPaths['nginx_access'] = [
'name' => __('Nginx Access Log'),
'path' => $tempDir . '/access.log',
'tail' => true,
];
$logPaths['nginx_error'] = [
'name' => __('Nginx Error Log'),
'path' => $tempDir . '/error.log',
'tail' => true,
];
$logPaths['php'] = [
'name' => __('PHP Application Log'),
'path' => $tempDir . '/php_errors.log',
'tail' => true,
];
$logPaths['supervisord'] = [
'name' => __('Supervisord Log'),
'path' => $tempDir . '/supervisord.log',
'tail' => true,
];
}
return $logPaths;
}
}