29 lines
783 B
PHP
29 lines
783 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
final class AuditLog
|
|
{
|
|
public function __construct(private string $path = Settings::AUDIT_LOG)
|
|
{
|
|
}
|
|
|
|
/** @param array<string,string|int|bool> $fields */
|
|
public function record(string $action, string $user, array $fields = []): void
|
|
{
|
|
$dir = dirname($this->path);
|
|
if (!is_dir($dir)) {
|
|
@mkdir($dir, 0700, true);
|
|
}
|
|
unset($fields['body']);
|
|
$line = json_encode([
|
|
'ts' => date('c'),
|
|
'action' => $action,
|
|
'user' => $user,
|
|
'fields' => $fields,
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
if ($line !== false) {
|
|
@file_put_contents($this->path, $line . "\n", FILE_APPEND | LOCK_EX);
|
|
}
|
|
}
|
|
}
|