2018-11-27 16:03:14 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
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;
|
2018-11-27 16:03:14 +00:00
|
|
|
|
2022-06-19 22:28:31 +00:00
|
|
|
use App\Controller\Api\Traits\HasLogViewer;
|
2020-12-03 04:18:06 +00:00
|
|
|
use App\Environment;
|
2020-05-06 23:33:52 +00:00
|
|
|
use App\Exception;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2022-11-17 15:13:41 +00:00
|
|
|
use App\Service\ServiceControl;
|
2018-11-27 16:03:14 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
2022-06-19 22:28:31 +00:00
|
|
|
final class LogsAction
|
2018-11-27 16:03:14 +00:00
|
|
|
{
|
2022-06-19 22:28:31 +00:00
|
|
|
use HasLogViewer;
|
|
|
|
|
2021-04-23 05:24:12 +00:00
|
|
|
public function __construct(
|
2022-11-17 15:13:41 +00:00
|
|
|
private readonly Environment $environment,
|
|
|
|
private readonly ServiceControl $serviceControl,
|
2021-04-23 05:24:12 +00:00
|
|
|
) {
|
2018-11-27 16:03:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:27:28 +00:00
|
|
|
public function __invoke(
|
|
|
|
ServerRequest $request,
|
2022-06-19 22:28:31 +00:00
|
|
|
Response $response,
|
|
|
|
?string $log = null
|
2022-05-22 00:27:28 +00:00
|
|
|
): ResponseInterface {
|
2022-06-19 22:28:31 +00:00
|
|
|
$logPaths = $this->getGlobalLogs();
|
2018-11-27 16:03:14 +00:00
|
|
|
|
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'] = [
|
2022-11-06 20:42:50 +00:00
|
|
|
'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.');
|
2018-11-27 16:03:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 22:28:31 +00:00
|
|
|
return $this->streamLogToResponse(
|
|
|
|
$request,
|
2021-01-05 05:51:48 +00:00
|
|
|
$response,
|
2022-06-19 22:28:31 +00:00
|
|
|
$logPaths[$log]['path'],
|
|
|
|
$logPaths[$log]['tail'] ?? true
|
2021-01-05 05:51:48 +00:00
|
|
|
);
|
2018-11-27 16:03:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 22:19:31 +00:00
|
|
|
/**
|
2021-07-19 05:53:45 +00:00
|
|
|
* @return array<string, array>
|
2020-10-14 22:19:31 +00:00
|
|
|
*/
|
2022-05-22 07:39:00 +00:00
|
|
|
private function getGlobalLogs(): array
|
2018-11-27 16:03:14 +00:00
|
|
|
{
|
2020-12-03 04:18:06 +00:00
|
|
|
$tempDir = $this->environment->getTempDirectory();
|
2019-09-12 05:31:01 +00:00
|
|
|
$logPaths = [];
|
2018-11-27 16:03:14 +00:00
|
|
|
|
2019-09-12 05:31:01 +00:00
|
|
|
$logPaths['azuracast_log'] = [
|
2018-11-27 16:03:14 +00:00
|
|
|
'name' => __('AzuraCast Application Log'),
|
2021-01-05 05:51:48 +00:00
|
|
|
'path' => $tempDir . '/app-' . gmdate('Y-m-d') . '.log',
|
2018-11-27 16:03:14 +00:00
|
|
|
'tail' => true,
|
|
|
|
];
|
|
|
|
|
2022-07-03 05:43:00 +00:00
|
|
|
if ($this->environment->isDocker()) {
|
2022-11-17 15:13:41 +00:00
|
|
|
$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,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2022-07-03 05:43:00 +00:00
|
|
|
} else {
|
2019-09-12 05:31:01 +00:00
|
|
|
$logPaths['nginx_access'] = [
|
2018-11-27 16:03:14 +00:00
|
|
|
'name' => __('Nginx Access Log'),
|
2019-09-12 05:31:01 +00:00
|
|
|
'path' => $tempDir . '/access.log',
|
2018-11-27 16:03:14 +00:00
|
|
|
'tail' => true,
|
|
|
|
];
|
2019-09-12 05:31:01 +00:00
|
|
|
$logPaths['nginx_error'] = [
|
2018-11-27 16:03:14 +00:00
|
|
|
'name' => __('Nginx Error Log'),
|
2019-09-12 05:31:01 +00:00
|
|
|
'path' => $tempDir . '/error.log',
|
2018-11-27 16:03:14 +00:00
|
|
|
'tail' => true,
|
|
|
|
];
|
2019-09-12 05:31:01 +00:00
|
|
|
$logPaths['php'] = [
|
2018-11-27 16:03:14 +00:00
|
|
|
'name' => __('PHP Application Log'),
|
2019-09-12 05:31:01 +00:00
|
|
|
'path' => $tempDir . '/php_errors.log',
|
2018-11-27 16:03:14 +00:00
|
|
|
'tail' => true,
|
|
|
|
];
|
2019-09-12 05:31:01 +00:00
|
|
|
$logPaths['supervisord'] = [
|
2018-11-27 16:03:14 +00:00
|
|
|
'name' => __('Supervisord Log'),
|
2019-09-12 05:31:01 +00:00
|
|
|
'path' => $tempDir . '/supervisord.log',
|
2018-11-27 16:03:14 +00:00
|
|
|
'tail' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-09-12 05:31:01 +00:00
|
|
|
return $logPaths;
|
2018-11-27 16:03:14 +00:00
|
|
|
}
|
|
|
|
}
|