fix: plain logging
All checks were successful
Build Test / build (pull_request) Successful in 16s
JS Unit Tests / test (pull_request) Successful in 15s
PHP Unit Tests / test (pull_request) Successful in 44s

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-05-06 12:24:53 -04:00
parent bc28e9c307
commit a7a94f93d8
3 changed files with 32 additions and 8 deletions

View File

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

View File

@@ -20,10 +20,8 @@ class PlainFileLogger implements LoggerInterface
*/
public function __construct(string $logDir, string $channel = 'app')
{
if (!is_dir($logDir)) {
@mkdir($logDir, 0775, true);
}
$this->logFile = rtrim($logDir, '/') . '/' . $channel . '.log';
$this->ensureWritablePath();
}
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
{
$this->ensureWritablePath();
$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');
$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