Merge pull request 'fix: plain logging' (#51) from fix/plain-logging into main

Reviewed-on: #51
This commit was merged in pull request #51.
This commit is contained in:
2026-05-06 16:25:43 +00:00
3 changed files with 32 additions and 8 deletions

View File

@@ -57,15 +57,15 @@ return [
'per_tenant' => false, 'per_tenant' => false,
// ── file driver options ──────────────────────────────────────────────── // ── file driver options ────────────────────────────────────────────────
// Absolute path to the log directory. null = <project_root>/var/log // Absolute path to the log directory. null = <project_root>/var/logs
'path' => null, 'path' => null,
// Log channel — used as the filename without extension. // Log channel — used as the filename without extension.
// 'app' → var/log/app.jsonl (or var/log/tenant/{id}/app.jsonl when per_tenant = true) // 'app' → var/logs/app.jsonl (or var/logs/tenant/{id}/app.jsonl when per_tenant = true)
'channel' => 'app', 'channel' => 'app',
// ── syslog driver options ────────────────────────────────────────────── // ── syslog driver options ──────────────────────────────────────────────
// Identity tag passed to openlog(); visible in /var/log/syslog and journalctl -t ktrix // Identity tag passed to openlog(); visible in /var/logs/syslog and journalctl -t ktrix
'ident' => 'ktrix', 'ident' => 'ktrix',
// openlog() facility constant. Common values: LOG_USER, LOG_LOCAL0 … LOG_LOCAL7 // openlog() facility constant. Common values: LOG_USER, LOG_LOCAL0 … LOG_LOCAL7

View File

@@ -79,7 +79,7 @@ class LoggerFactory
$path = $logConfig['path'] ?? null; $path = $logConfig['path'] ?? null;
if ($path === null || $path === '') { if ($path === null || $path === '') {
$path = $projectDir . '/var/log'; $path = $projectDir . '/var/logs';
} }
return new FileLogger($path, $channel); return new FileLogger($path, $channel);

View File

@@ -20,10 +20,8 @@ class PlainFileLogger implements LoggerInterface
*/ */
public function __construct(string $logDir, string $channel = 'app') public function __construct(string $logDir, string $channel = 'app')
{ {
if (!is_dir($logDir)) {
@mkdir($logDir, 0775, true);
}
$this->logFile = rtrim($logDir, '/') . '/' . $channel . '.log'; $this->logFile = rtrim($logDir, '/') . '/' . $channel . '.log';
$this->ensureWritablePath();
} }
public function emergency($message, array $context = []): void { $this->log('emergency', $message, $context); } public function emergency($message, array $context = []): void { $this->log('emergency', $message, $context); }
@@ -37,12 +35,38 @@ class PlainFileLogger implements LoggerInterface
public function log($level, $message, array $context = []): void public function log($level, $message, array $context = []): void
{ {
$this->ensureWritablePath();
$dt = \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true))); $dt = \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true)));
$timestamp = $dt?->format('Y-m-d H:i:s.u') ?? date('Y-m-d H:i:s'); $timestamp = $dt?->format('Y-m-d H:i:s.u') ?? date('Y-m-d H:i:s');
$line = $timestamp . ' ' . $this->interpolate((string) $message, $context) . PHP_EOL; $line = $timestamp . ' ' . $this->interpolate((string) $message, $context) . PHP_EOL;
@file_put_contents($this->logFile, $line, FILE_APPEND | LOCK_EX); if (@file_put_contents($this->logFile, $line, FILE_APPEND | LOCK_EX) === false) {
error_log(sprintf('Failed to write to log file: %s', $this->logFile));
}
}
private function ensureWritablePath(): void
{
$logDir = dirname($this->logFile);
if (!is_dir($logDir)) {
@mkdir($logDir, 0777, true);
}
if (is_dir($logDir)) {
@chmod($logDir, 0777);
}
if (!file_exists($this->logFile)) {
@touch($this->logFile);
}
clearstatcache(true, $this->logFile);
if (file_exists($this->logFile)) {
@chmod($this->logFile, 0666);
}
} }
private function interpolate(string $message, array $context): string private function interpolate(string $message, array $context): string