mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-06 19:19:19 +02:00
add structured logging for admin/partage form submissions + migration system
- AppLogger: JSON-line logger in storage/logs/form-submissions.log - Logs submissions (admin + partage) with IP, UA, thesis ID, author - Logs errors with context (post keys, share slug) - Migration runner (app/migrations/run.php) handles schema drift - 001_add_objet_column.sql fixes production DB missing 'objet' column - ThesisCreateController::getIdentifier() helper for logging
This commit is contained in:
75
app/src/AppLogger.php
Normal file
75
app/src/AppLogger.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Structured application logger for form submissions.
|
||||
*
|
||||
* Writes JSON-lines to a log file in storage/logs/.
|
||||
* Each entry contains: timestamp, source (admin|partage), action,
|
||||
* status (success|error), context (IP, UA, thesis ID, error message, etc.).
|
||||
*/
|
||||
class AppLogger
|
||||
{
|
||||
private string $logDir;
|
||||
private string $logFile;
|
||||
|
||||
public function __construct(?string $logDir = null)
|
||||
{
|
||||
$this->logDir = $logDir ?? (defined('STORAGE_ROOT') ? STORAGE_ROOT . '/logs' : __DIR__ . '/../storage/logs');
|
||||
|
||||
if (!is_dir($this->logDir)) {
|
||||
mkdir($this->logDir, 0755, true);
|
||||
}
|
||||
|
||||
$this->logFile = $this->logDir . '/form-submissions.log';
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a successful thesis submission.
|
||||
*
|
||||
* @param string $source 'admin' or 'partage'
|
||||
* @param int $thesisId
|
||||
* @param string $identifier e.g. "2025-003"
|
||||
* @param string $authorName
|
||||
* @param array $extras Additional context (e.g. share link slug)
|
||||
*/
|
||||
public function logSubmission(string $source, int $thesisId, string $identifier, string $authorName, array $extras = []): void
|
||||
{
|
||||
$this->write(array_merge([
|
||||
'source' => $source,
|
||||
'action' => 'submit',
|
||||
'status' => 'success',
|
||||
'thesis_id' => $thesisId,
|
||||
'identifier' => $identifier,
|
||||
'author' => $authorName,
|
||||
], $extras));
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a failed thesis submission.
|
||||
*
|
||||
* @param string $source
|
||||
* @param string $errorMessage
|
||||
* @param array $extras
|
||||
*/
|
||||
public function logError(string $source, string $errorMessage, array $extras = []): void
|
||||
{
|
||||
$this->write(array_merge([
|
||||
'source' => $source,
|
||||
'action' => 'submit',
|
||||
'status' => 'error',
|
||||
'error' => $errorMessage,
|
||||
], $extras));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a structured log line.
|
||||
*/
|
||||
private function write(array $entry): void
|
||||
{
|
||||
$entry['timestamp'] = date('c');
|
||||
$entry['ip'] = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
|
||||
$entry['user_agent'] = $_SERVER['HTTP_USER_AGENT'] ?? '';
|
||||
|
||||
$line = json_encode($entry, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
|
||||
error_log($line, 3, $this->logFile);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user