← Code-Übersicht

JsonLineLogger.php

Pfad: src/Infrastructure/Rechnung/JsonLineLogger.php

Ext: php

Größe: 1085 Bytes

Geändert: 2026-07-16T14:11:10+02:00

Frühere Version vom 2026-07-16T14:11:10+02:00 · zur aktuellen Fassung

<?php
declare(strict_types=1);

namespace Demo\Infrastructure\Rechnung;

use Demo\Domain\Rechnung\InvoiceLoggerPort;

/**
 * JSONL-Logger fuer strukturierte Laufausgaben.
 */
final class JsonLineLogger implements InvoiceLoggerPort
{
    /**
     * @param string $logPath Zielpfad.
     */
    public function __construct(
        private string $logPath
    ) {
    }

    /**
     * @inheritDoc
     */
    public function logStep(string $runId, string $step, string $status, array $context = []): void
    {
        $entry = [
            'time' => (new \DateTimeImmutable('now'))->format(DATE_ATOM),
            'runId' => $runId,
            'step' => $step,
            'status' => $status,
            'context' => $context,
        ];

        $dir = dirname($this->logPath);
        if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) {
            return;
        }

        file_put_contents(
            $this->logPath,
            json_encode($entry, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n",
            FILE_APPEND | LOCK_EX
        );
    }
}