8 Commits

Author SHA1 Message Date
gitea-actions
b9712643de chore: bump version to v0.1.24
Some checks failed
Auto Tag Develop / tag (push) Successful in 5s
Build & Push Docker Image / build (push) Failing after 12s
2026-04-08 07:11:01 +00:00
e954402959 fix : install docker-compose-plugin in prod Dockerfile
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
Deploy scripts use `docker compose` (Compose V2 plugin) which is not
included in docker.io package.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 09:10:33 +02:00
gitea-actions
98d9032068 chore: bump version to v0.1.23
All checks were successful
Auto Tag Develop / tag (push) Successful in 5s
Build & Push Docker Image / build (push) Successful in 53s
2026-04-07 12:38:52 +00:00
5f6277d412 feat : update Malio UI + CLAUDE.md
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
2026-04-07 14:38:42 +02:00
gitea-actions
8fb71e6370 chore: bump version to v0.1.22
All checks were successful
Auto Tag Develop / tag (push) Successful in 5s
Build & Push Docker Image / build (push) Successful in 31s
2026-04-07 10:16:54 +00:00
e128b45caa fix : affichage log docker et symfony
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
2026-04-07 12:16:45 +02:00
gitea-actions
d331ef4577 chore: bump version to v0.1.21
All checks were successful
Auto Tag Develop / tag (push) Successful in 5s
Build & Push Docker Image / build (push) Successful in 31s
2026-04-07 10:01:09 +00:00
b769abdbe1 feat : ajout de la lecture des logs symfony et docker (#3)
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>
2026-04-07 10:01:01 +00:00
20 changed files with 611 additions and 39 deletions

View File

@@ -31,6 +31,10 @@ frontend/services/dto/ # Types TypeScript
frontend/i18n/locales/ # Fichiers de traduction
```
## Composants UI
La librairie `@malio/layer-ui` fournit les composants de formulaire et d'action. La documentation complète des props, events et exemples d'utilisation se trouve dans `frontend/node_modules/@malio/layer-ui/COMPONENTS.md`. Toujours s'y référer avant d'utiliser un composant Malio.
## Commandes
```bash

View File

@@ -1,2 +1,2 @@
parameters:
app.version: '0.1.20'
app.version: '0.1.24'

View File

@@ -26,6 +26,7 @@ services:
- ./LOG:/var/www/html/LOG
- uploads_data:/var/www/html/var/uploads
- /var/run/docker.sock:/var/run/docker.sock
- ${HOST_APPS_PATH}:/mnt/apps
extra_hosts:
- "host.docker.internal:host-gateway"
depends_on:

View File

@@ -0,0 +1,113 @@
<script setup lang="ts">
const { t } = useI18n()
const props = withDefaults(defineProps<{
modelValue: boolean
title: string
content: string
loading?: boolean
showLevelFilter?: boolean
}>(), {
loading: false,
showLevelFilter: false,
})
const emit = defineEmits<{
(e: 'update:modelValue', value: boolean): void
(e: 'refresh', lines: number, level: string): void
}>()
const selectedLines = ref(100)
const selectedLevel = ref('')
const lineOptions = [50, 100, 500, 1000]
const copied = ref(false)
function refresh() {
emit('refresh', selectedLines.value, selectedLevel.value)
}
async function copyLogs() {
if (!props.content) return
await navigator.clipboard.writeText(props.content)
copied.value = true
setTimeout(() => { copied.value = false }, 2000)
}
watch(() => props.modelValue, (open) => {
if (open) {
copied.value = false
refresh()
}
})
</script>
<template>
<AppModal
:model-value="modelValue"
@update:model-value="emit('update:modelValue', $event)"
max-width="2xl"
>
<template #title>{{ title }}</template>
<div class="flex items-center justify-between mb-4">
<div class="flex items-center gap-3">
<div class="flex items-center gap-2">
<label class="text-xs text-neutral-400">{{ t('logs.lines') }}</label>
<select
v-model="selectedLines"
class="rounded-md border border-neutral-300 px-2 py-1 text-sm"
@change="refresh"
>
<option v-for="n in lineOptions" :key="n" :value="n">{{ n }}</option>
</select>
</div>
<div v-if="showLevelFilter" class="flex items-center gap-2">
<label class="text-xs text-neutral-400">{{ t('logs.level') }}</label>
<select
v-model="selectedLevel"
class="rounded-md border border-neutral-300 px-2 py-1 text-sm"
@change="refresh"
>
<option value="">{{ t('logs.levelAll') }}</option>
<option value="ERROR">ERROR</option>
<option value="WARNING">WARNING</option>
<option value="INFO">INFO</option>
<option value="DEBUG">DEBUG</option>
</select>
</div>
<MalioButtonIcon
icon="mdi:refresh"
:aria-label="t('logs.refresh')"
icon-size="18"
@click="refresh"
/>
</div>
<MalioButtonIcon
:icon="copied ? 'mdi:check' : 'mdi:content-copy'"
:aria-label="t('logs.copy')"
icon-size="18"
:button-class="copied ? 'text-green-500' : ''"
@click="copyLogs"
/>
</div>
<pre
v-if="content"
class="max-h-96 overflow-auto rounded-lg bg-neutral-900 p-4 text-xs text-green-400 font-mono whitespace-pre-wrap"
>{{ content }}</pre>
<p v-else-if="!loading" class="text-center text-neutral-400 py-8">
{{ t('logs.noContent') }}
</p>
<template #footer>
<MalioButton
:label="t('applications.form.cancel')"
variant="tertiary"
@click="emit('update:modelValue', false)"
/>
</template>
</AppModal>
</template>

View File

@@ -2,9 +2,7 @@
<NuxtLink
:to="to"
class="group/link relative flex items-center transition-colors hover:text-primary-500"
:class="linkClasses"
:active-class="exact ? '' : activeClass"
:exact-active-class="exact ? activeClass : ''"
:class="[linkClasses, isActive ? activeClass : '']"
>
<Icon :name="icon" :size="sub ? '20' : '24'" class="flex-shrink-0" />
<span
@@ -33,6 +31,15 @@ const props = defineProps<{
exact?: boolean
}>()
const route = useRoute()
const isActive = computed(() => {
if (props.exact) {
return route.path === props.to
}
return route.path === props.to || route.path.startsWith(props.to + '/')
})
const activeClass = computed(() => {
if (props.collapsed) {
return '!text-primary-500 bg-primary-500/10'

View File

@@ -48,6 +48,17 @@
"logout": "Déconnexion réussie."
}
},
"logs": {
"docker": "Logs Docker",
"symfony": "Logs Symfony",
"lines": "Lignes",
"level": "Niveau",
"levelAll": "Tous",
"refresh": "Actualiser",
"noContent": "Aucun log disponible",
"copy": "Copier les logs",
"title": "Logs"
},
"dashboard": {
"title": "Dashboard",
"description": "Vue d'ensemble du SI",
@@ -56,7 +67,8 @@
"running": "En ligne",
"exited": "Arrete",
"restarting": "Redemarrage",
"not_found": "Introuvable"
"not_found": "Introuvable",
"unavailable": "Docker indisponible"
}
},
"applications": {
@@ -107,6 +119,7 @@
"deployScriptPath": "Chemin du script de deploiement",
"maintenanceFilePath": "Chemin du fichier de maintenance",
"pathHint": "Prefixe automatique : /mnt/apps",
"pathHintLog": "Chemin dans le container, ex : var/log/prod.log",
"appUrl": "URL de l'application",
"save": "Enregistrer",
"cancel": "Annuler"

View File

@@ -7,7 +7,7 @@
"name": "nuxt-app",
"hasInstallScript": true,
"dependencies": {
"@malio/layer-ui": "^1.2.2",
"@malio/layer-ui": "^1.2.3",
"@nuxt/icon": "^2.2.1",
"@nuxtjs/i18n": "^10.2.3",
"@nuxtjs/tailwindcss": "^6.14.0",
@@ -1668,9 +1668,9 @@
"license": "MIT"
},
"node_modules/@malio/layer-ui": {
"version": "1.2.2",
"resolved": "https://gitea.malio.fr/api/packages/MALIO-DEV/npm/%40malio%2Flayer-ui/-/1.2.2/layer-ui-1.2.2.tgz",
"integrity": "sha512-nV4FL19rYSiXqMDTUlAtp6AYdj7YiwpHbf7/usiOPj7llpjHIC3GmcOX0X7oQeOMTtSU1aKL8k8wn1bhptrHYg==",
"version": "1.2.3",
"resolved": "https://gitea.malio.fr/api/packages/MALIO-DEV/npm/%40malio%2Flayer-ui/-/1.2.3/layer-ui-1.2.3.tgz",
"integrity": "sha512-5nRnBzRkXfs3PfKwKl6sH2ikrmSK7lTifcd0TX1QZP3rFRVRTgcT6mrsrpsbR9PwI27OeCNm0X6d0Ii92Rq7Yg==",
"dependencies": {
"@nuxt/icon": "^2.2.1",
"@nuxtjs/tailwindcss": "^6.14.0",

View File

@@ -11,7 +11,7 @@
"build:dist": "nuxt generate && rm -rf dist && cp -R .output/public dist"
},
"dependencies": {
"@malio/layer-ui": "^1.2.2",
"@malio/layer-ui": "^1.2.3",
"@nuxt/icon": "^2.2.1",
"@nuxtjs/i18n": "^10.2.3",
"@nuxtjs/tailwindcss": "^6.14.0",

View File

@@ -6,6 +6,8 @@ import type { DeployResult } from '~/services/dto/deploy'
import { getAvailableTags, deploy } from '~/services/deploy'
import type { EnvironmentHealth } from '~/services/dto/dashboard'
import { getEnvironmentHealth } from '~/services/dashboard'
import type { LogOutput } from '~/services/dto/logs'
import { getDockerLogs, getSymfonyLog } from '~/services/logs'
const { t } = useI18n()
const route = useRoute()
@@ -29,6 +31,15 @@ const deployResult = ref<DeployResult | null>(null)
const healthByEnvId = ref<Record<number, EnvironmentHealth>>({})
const loadingHealth = ref(false)
// Log modals
const showLogModal = ref(false)
const logContent = ref('')
const logLoading = ref(false)
const logTitle = ref('')
const logEnvId = ref<number | null>(null)
const logFileId = ref<number | null>(null)
const logIsSymfony = ref(false)
// App edit modal
const showAppModal = ref(false)
const editForm = ref<ApplicationWrite>({ name: '', slug: '', registryImage: '', description: '', giteaUrl: '' })
@@ -226,6 +237,41 @@ function statusClass(status: string): string {
}
}
// Log functions
async function openDockerLogs(env: Environment) {
logEnvId.value = env.id!
logFileId.value = null
logIsSymfony.value = false
logTitle.value = `${t('logs.docker')}${env.name}`
logContent.value = ''
showLogModal.value = true
}
async function openSymfonyLog(env: Environment, lf: { id?: number, label: string }) {
logEnvId.value = env.id!
logFileId.value = lf.id!
logIsSymfony.value = true
logTitle.value = `${t('logs.symfony')}${lf.label}`
logContent.value = ''
showLogModal.value = true
}
async function refreshLogs(lines: number, level: string) {
if (!logEnvId.value) return
logLoading.value = true
try {
let result: LogOutput
if (logIsSymfony.value && logFileId.value) {
result = await getSymfonyLog(logEnvId.value, logFileId.value, lines, level || undefined)
} else {
result = await getDockerLogs(logEnvId.value, lines)
}
logContent.value = result.content
} finally {
logLoading.value = false
}
}
const envModalTitle = computed(() =>
editingEnvId.value ? t('environments.editButton') : t('environments.addButton')
)
@@ -334,6 +380,13 @@ onMounted(loadApplication)
</a>
</div>
<div class="flex gap-2">
<MalioButton
:label="t('logs.docker')"
variant="secondary"
icon-name="mdi:text-box-outline"
icon-position="left"
@click="openDockerLogs(env)"
/>
<MalioButton
:label="t('environments.deploy.button')"
icon-name="mdi:rocket-launch-outline"
@@ -358,9 +411,16 @@ onMounted(loadApplication)
<!-- Log files -->
<div v-if="env.logFiles.length" class="mt-4 border-t border-neutral-200 pt-3">
<p class="text-sm font-bold uppercase tracking-wider mb-2">{{ t('environments.logFiles.title') }}</p>
<div v-for="lf in env.logFiles" :key="lf.id" class="text-sm text-neutral-700 flex gap-2">
<span class="font-medium">{{ lf.label }} :</span>
<div v-for="lf in env.logFiles" :key="lf.id" class="text-sm text-neutral-700 flex gap-2 items-center">
<span class="font-medium">{{ lf.label }}</span>
<span class="font-mono text-neutral-400">{{ lf.path }}</span>
<MalioButtonIcon
icon="mdi:console"
:aria-label="lf.label"
variant="ghost"
icon-size="16"
@click="openSymfonyLog(env, lf)"
/>
</div>
</div>
@@ -519,7 +579,7 @@ onMounted(loadApplication)
<MalioInputText v-model="lf.label" :label="t('environments.logFiles.label')" groupClass="mt-0" inputClass="flex-1" required />
</div>
<div class="w-2/3">
<MalioInputText v-model="lf.path" :label="t('environments.logFiles.path')" groupClass="mt-0" inputClass="flex-[2]" :hint="t('environments.form.pathHint')" required />
<MalioInputText v-model="lf.path" :label="t('environments.logFiles.path')" groupClass="mt-0" inputClass="flex-[2]" :hint="`${t('environments.form.pathHint')}/${application?.slug ?? ''}`" required />
</div>
<div class="h-[46px] flex items-center">
<MalioButtonIcon
@@ -613,5 +673,15 @@ onMounted(loadApplication)
/>
</template>
</AppModal>
<!-- Log modal -->
<LogModal
v-model="showLogModal"
:title="logTitle"
:content="logContent"
:loading="logLoading"
:show-level-filter="logIsSymfony"
@refresh="refreshLogs"
/>
</div>
</template>

View File

@@ -20,18 +20,12 @@
v-model="username"
/>
<div>
<label class="text-sm font-semibold text-neutral-700" for="password">
Mot de passe
</label>
<input
id="password"
v-model="password"
type="password"
autocomplete="current-password"
class="mt-2 w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-base text-neutral-900 focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-secondary-500/20"
/>
</div>
<MalioInputPassword
v-model="password"
label="Mot de passe"
autocomplete="current-password"
inputClass="w-full"
/>
<MalioButton
label="Se connecter"

View File

@@ -0,0 +1,5 @@
type LogOutput = {
content: string
lines: number
source: string
}

15
frontend/services/logs.ts Normal file
View File

@@ -0,0 +1,15 @@
import type { LogOutput } from './dto/logs'
export function getDockerLogs(envId: number, lines: number = 100): Promise<LogOutput> {
return useApi().get<LogOutput>(`/environments/${envId}/logs/docker`, { lines }, {
toast: false,
})
}
export function getSymfonyLog(envId: number, logFileId: number, lines: number = 100, level?: string): Promise<LogOutput> {
const query: Record<string, any> = { lines }
if (level) query.level = level
return useApi().get<LogOutput>(`/environments/${envId}/logs/symfony/${logFileId}`, query, {
toast: false,
})
}

View File

@@ -7,3 +7,4 @@ POSTGRES_USER=root
POSTGRES_PASSWORD=root
POSTGRES_PORT=5436
XDEBUG_CLIENT_HOST=host.docker.internal
HOST_APPS_PATH=/home/user/workspace

View File

@@ -40,7 +40,7 @@ FROM php:8.4-fpm AS production
RUN apt-get update && apt-get install -y \
libicu-dev libpq-dev libpng-dev libzip-dev libxml2-dev \
nginx supervisor docker.io \
nginx supervisor docker.io docker-compose-plugin \
&& docker-php-ext-install -j$(nproc) intl pdo_pgsql zip gd opcache \
&& rm -rf /var/lib/apt/lists/*

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\State\DockerLogProvider;
use App\State\SymfonyLogProvider;
#[ApiResource(
operations: [
new Get(
uriTemplate: '/environments/{id}/logs/docker',
security: "is_granted('ROLE_ADMIN')",
provider: DockerLogProvider::class,
),
new Get(
uriTemplate: '/environments/{id}/logs/symfony/{logFileId}',
security: "is_granted('ROLE_ADMIN')",
provider: SymfonyLogProvider::class,
),
],
)]
final class LogOutput
{
public string $content = '';
public int $lines = 0;
public string $source = '';
}

View File

@@ -57,21 +57,21 @@ class AppFixtures extends Fixture
$sirh->setGiteaUrl('https://gitea.malio.fr/malio-dev/sirh');
$sirhProd = new Environment();
$sirhProd->setName('production');
$sirhProd->setContainerName('sirh-app');
$sirhProd->setDeployScriptPath('/home/m-tristan/workspace/SIRH/deploy/docker/deploy.sh');
$sirhProd->setMaintenanceFilePath('/home/m-tristan/workspace/SIRH/deploy/docker/maintenance.on');
$sirhProd->setName('Production');
$sirhProd->setContainerName('php-sirh-fpm');
$sirhProd->setDeployScriptPath('/SIRH/deploy/docker/deploy.sh');
$sirhProd->setMaintenanceFilePath('/SIRH/deploy/docker/maintenance.on');
$sirhProd->setAppUrl('http://sirh.malio-dev.fr');
$sirh->addEnvironment($sirhProd);
$sirhProdLog = new LogFile();
$sirhProdLog->setLabel('prod');
$sirhProdLog->setPath('/home/m-tristan/workspace/SIRH/var/log/prod.log');
$sirhProdLog->setLabel('dev');
$sirhProdLog->setPath('/SIRH/var/log/dev.log');
$sirhProd->addLogFile($sirhProdLog);
$sirhCronLog = new LogFile();
$sirhCronLog->setLabel('cron');
$sirhCronLog->setPath('/home/m-tristan/workspace/SIRH/var/log/cron.log');
$sirhCronLog->setPath('/SIRH/var/log/cron.log');
$sirhProd->addLogFile($sirhCronLog);
$manager->persist($sirh);
@@ -86,8 +86,8 @@ class AppFixtures extends Fixture
$lesstimeProd = new Environment();
$lesstimeProd->setName('production');
$lesstimeProd->setContainerName('lesstime-app');
$lesstimeProd->setDeployScriptPath('/home/m-tristan/workspace/lesstime/deploy/docker/deploy.sh');
$lesstimeProd->setMaintenanceFilePath('/home/m-tristan/workspace/lesstime/deploy/docker/maintenance.on');
$lesstimeProd->setDeployScriptPath('/lesstime/deploy/docker/deploy.sh');
$lesstimeProd->setMaintenanceFilePath('/lesstime/deploy/docker/maintenance.on');
$lesstimeProd->setAppUrl('http://lesstime.malio-dev.fr');
$lesstime->addEnvironment($lesstimeProd);
@@ -103,16 +103,16 @@ class AppFixtures extends Fixture
$inventoryProd = new Environment();
$inventoryProd->setName('production');
$inventoryProd->setContainerName('inventory-app');
$inventoryProd->setDeployScriptPath('/home/m-tristan/workspace/inventory/deploy/docker/deploy.sh');
$inventoryProd->setMaintenanceFilePath('/home/m-tristan/workspace/inventory/deploy/docker/maintenance.on');
$inventoryProd->setDeployScriptPath('/inventory/deploy/docker/deploy.sh');
$inventoryProd->setMaintenanceFilePath('/inventory/deploy/docker/maintenance.on');
$inventoryProd->setAppUrl('http://inventory.malio-dev.fr');
$inventory->addEnvironment($inventoryProd);
$inventoryRecette = new Environment();
$inventoryRecette->setName('recette');
$inventoryRecette->setContainerName('inventory-test-app');
$inventoryRecette->setDeployScriptPath('/home/m-tristan/workspace/inventory/deploy/docker/deploy-test.sh');
$inventoryRecette->setMaintenanceFilePath('/home/m-tristan/workspace/inventory/deploy/docker/maintenance-test.on');
$inventoryRecette->setDeployScriptPath('/inventory/deploy/docker/deploy-test.sh');
$inventoryRecette->setMaintenanceFilePath('/inventory/deploy/docker/maintenance-test.on');
$inventoryRecette->setAppUrl('http://inventory-test.malio-dev.fr');
$inventory->addEnvironment($inventoryRecette);

View File

@@ -8,11 +8,34 @@ use Symfony\Component\Process\Process;
final class DockerService
{
private ?bool $dockerAvailable = null;
private function isDockerAvailable(): bool
{
if (null === $this->dockerAvailable) {
$process = new Process(['which', 'docker']);
$process->setTimeout(5);
$process->run();
$this->dockerAvailable = $process->isSuccessful();
}
return $this->dockerAvailable;
}
/**
* @return array{status: string, image: string, version: string, startedAt: string}
*/
public function getContainerStatus(string $containerName): array
{
if (!$this->isDockerAvailable()) {
return [
'status' => 'unavailable',
'image' => '',
'version' => '',
'startedAt' => '',
];
}
$process = new Process([
'docker', 'inspect',
'--format', '{{.State.Status}}||{{.Config.Image}}||{{.State.StartedAt}}',
@@ -60,6 +83,15 @@ final class DockerService
*/
public function getContainerStats(string $containerName): array
{
if (!$this->isDockerAvailable()) {
return [
'cpuPercent' => 0.0,
'memoryUsage' => '',
'memoryLimit' => '',
'memoryPercent' => 0.0,
];
}
$process = new Process([
'docker', 'stats', '--no-stream',
'--format', '{{.CPUPerc}}||{{.MemUsage}}||{{.MemPerc}}',

179
src/Service/LogService.php Normal file
View File

@@ -0,0 +1,179 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Symfony\Component\Process\Process;
final readonly class LogService
{
public function __construct(
private AppPathResolver $pathResolver,
) {}
public function getDockerLogs(string $containerName, int $lines = 100, ?string $since = null): string
{
$check = new Process(['which', 'docker']);
$check->setTimeout(5);
$check->run();
if (!$check->isSuccessful()) {
return 'Docker CLI is not available in this environment.';
}
$args = ['docker', 'logs', '--tail', (string) $lines];
if (null !== $since) {
$args[] = '--since';
$args[] = $since;
}
$args[] = $containerName;
$process = new Process($args);
$process->setTimeout(10);
$process->run();
$output = $process->getOutput() . $process->getErrorOutput();
return $this->formatDockerOutput($output);
}
private function formatDockerOutput(string $output): string
{
$rawLines = explode("\n", trim($output));
$formatted = [];
foreach ($rawLines as $line) {
if ('' === $line) {
continue;
}
// Try parsing as Symfony monolog JSON format
$parsed = $this->parseSymfonyLogLine($line);
if (null !== $parsed) {
if ('doctrine' === $parsed['channel']) {
continue;
}
$formatted[] = sprintf('[%s] %s.%s: %s', $parsed['date'], $parsed['channel'], $parsed['level'], $parsed['message']);
continue;
}
// Try parsing as JSON log ({"message":"...","level":...})
$json = json_decode($line, true);
if (\is_array($json) && isset($json['message'])) {
$date = isset($json['datetime']) ? substr($json['datetime'], 0, 19) : '';
$date = str_replace('T', ' ', $date);
$channel = $json['channel'] ?? 'app';
$level = $json['level_name'] ?? 'INFO';
if ('doctrine' === $channel) {
continue;
}
$formatted[] = sprintf('[%s] %s.%s: %s', $date, $channel, $level, $json['message']);
continue;
}
// Keep raw lines that don't match any format (nginx access logs, etc.)
$formatted[] = $line;
}
return implode("\n", $formatted);
}
public function getSymfonyLog(string $containerName, string $logPath, int $lines = 100, ?string $level = null): string
{
$check = new Process(['which', 'docker']);
$check->setTimeout(5);
$check->run();
if (!$check->isSuccessful()) {
// Fallback: try reading from filesystem (dev mode)
$localPath = $this->pathResolver->resolve($logPath);
if (!file_exists($localPath)) {
return sprintf('Log file not found: %s (Docker CLI unavailable, local path: %s)', $logPath, $localPath);
}
$readLines = (null !== $level && '' !== $level) ? $lines * 5 : $lines;
$process = new Process(['tail', '-n', (string) $readLines, $localPath]);
$process->setTimeout(10);
$process->run();
return $this->formatSymfonyOutput($process->getOutput(), $lines, $level);
}
// Read more lines than requested to compensate for filtering
$readLines = (null !== $level && '' !== $level) ? $lines * 5 : $lines;
$process = new Process(['docker', 'exec', $containerName, 'tail', '-n', (string) $readLines, $logPath]);
$process->setTimeout(10);
$process->run();
if (!$process->isSuccessful()) {
return sprintf('Error reading log: %s', trim($process->getErrorOutput()));
}
return $this->formatSymfonyOutput($process->getOutput(), $lines, $level);
}
private function formatSymfonyOutput(string $output, int $lines, ?string $level): string
{
$rawLines = explode("\n", trim($output));
$formatted = [];
foreach ($rawLines as $line) {
if ('' === $line) {
continue;
}
$parsed = $this->parseSymfonyLogLine($line);
if (null === $parsed) {
continue;
}
// Skip noisy channels
if ('doctrine' === $parsed['channel']) {
continue;
}
if (null !== $level && '' !== $level && !str_contains(strtoupper($parsed['level']), strtoupper($level))) {
continue;
}
$formatted[] = sprintf('[%s] %s.%s: %s', $parsed['date'], $parsed['channel'], $parsed['level'], $parsed['message']);
}
// Keep only the last N lines after filtering
$formatted = \array_slice($formatted, -$lines);
return implode("\n", $formatted);
}
/**
* @return array{date: string, level: string, channel: string, message: string}|null
*/
private function parseSymfonyLogLine(string $line): ?array
{
// Standard Symfony monolog format: [2026-04-03T15:33:19.304937+02:00] channel.LEVEL: message {context} []
if (preg_match('/^\[([^\]]+)\]\s+(\w+)\.(\w+):\s+(.+)$/', $line, $matches)) {
$date = substr($matches[1], 0, 19); // Trim to YYYY-MM-DDTHH:MM:SS
$message = $matches[4];
// Remove JSON context at the end: {"key":"value"} []
$message = preg_replace('/\s*\{.*\}\s*\[\]\s*$/', '', $message) ?? $message;
// Remove trailing []
$message = preg_replace('/\s*\[\]\s*$/', '', $message) ?? $message;
return [
'date' => str_replace('T', ' ', $date),
'level' => $matches[3],
'channel' => $matches[2],
'message' => $message,
];
}
return null;
}
}

View File

@@ -0,0 +1,49 @@
<?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\Service\LogService;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
final readonly class DockerLogProvider implements ProviderInterface
{
public function __construct(
private EnvironmentRepository $environmentRepository,
private LogService $logService,
private RequestStack $requestStack,
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): LogOutput
{
$id = $uriVariables['id'] ?? null;
$environment = $id ? $this->environmentRepository->find($id) : null;
if (null === $environment) {
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $id));
}
$request = $this->requestStack->getCurrentRequest();
$lines = (int) ($request?->query->get('lines', '100') ?? 100);
$since = $request?->query->get('since');
$content = $this->logService->getDockerLogs(
$environment->getContainerName(),
$lines,
$since,
);
$dto = new LogOutput();
$dto->content = $content;
$dto->lines = $lines;
$dto->source = sprintf('docker:%s', $environment->getContainerName());
return $dto;
}
}

View File

@@ -0,0 +1,58 @@
<?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(
$environment->getContainerName(),
$logFile->getPath(),
$lines,
$level,
);
$dto = new LogOutput();
$dto->content = $content;
$dto->lines = $lines;
$dto->source = sprintf('symfony:%s', $logFile->getLabel());
return $dto;
}
}