<?php
namespace App\Logging;
use Psr\Log\AbstractLogger;
use Stringable;
final class FileLogger extends AbstractLogger {
public function __construct(private string $path) {}
public function log($level, string|Stringable $message, array $context = []): void {
// PSR-3 placeholder substitution: "{key}" → $context['key']
$msg = (string)$message;
foreach ($context as $k => $v) {
if (!is_array($v) && (!is_object($v) || method_exists($v, '__toString'))) {
$msg = str_replace('{' . $k . '}', (string)$v, $msg);
}
}
$entry = json_encode([
'ts' => date('c'),
'level' => (string)$level,
'msg' => $msg,
'ctx' => $context,
]) . PHP_EOL;
file_put_contents($this->path, $entry, FILE_APPEND | LOCK_EX);
}
}
$log = new FileLogger('/var/log/myapp.log');
$log->info('user {id} signed in', ['id' => 42]);
Create a free account and build your private vault. Share publicly whenever you want.