fix: plain logging
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user